xref: /csrg-svn/usr.bin/ranlib/misc.c (revision 46013)
1*46013Sbostic /*-
2*46013Sbostic  * Copyright (c) 1990 The Regents of the University of California.
3*46013Sbostic  * All rights reserved.
4*46013Sbostic  *
5*46013Sbostic  * This code is derived from software contributed to Berkeley by
6*46013Sbostic  * Hugh Smith at The University of Guelph.
7*46013Sbostic  *
8*46013Sbostic  * %sccs.include.redist.c%
9*46013Sbostic  */
10*46013Sbostic 
11*46013Sbostic #ifndef lint
12*46013Sbostic static char sccsid[] = "@(#)misc.c	5.1 (Berkeley) 01/18/91";
13*46013Sbostic #endif /* not lint */
14*46013Sbostic 
15*46013Sbostic #include <sys/param.h>
16*46013Sbostic #include <sys/signal.h>
17*46013Sbostic #include <sys/errno.h>
18*46013Sbostic #include <stdio.h>
19*46013Sbostic #include <stdlib.h>
20*46013Sbostic #include <string.h>
21*46013Sbostic #include "pathnames.h"
22*46013Sbostic 
23*46013Sbostic extern char *archive;			/* archive name */
24*46013Sbostic char *tname = "temporary file";		/* temporary file "name" */
25*46013Sbostic 
26*46013Sbostic tmp()
27*46013Sbostic {
28*46013Sbostic 	sigset_t set, oset;
29*46013Sbostic 	int fd;
30*46013Sbostic 	char path[MAXPATHLEN];
31*46013Sbostic 
32*46013Sbostic 	bcopy(_PATH_RANTMP, path, sizeof(_PATH_RANTMP));
33*46013Sbostic 
34*46013Sbostic 	sigemptyset(&set);
35*46013Sbostic 	sigaddset(&set, SIGHUP);
36*46013Sbostic 	sigaddset(&set, SIGINT);
37*46013Sbostic 	sigaddset(&set, SIGQUIT);
38*46013Sbostic 	sigaddset(&set, SIGTERM);
39*46013Sbostic 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
40*46013Sbostic 	if ((fd = mkstemp(path)) == -1)
41*46013Sbostic 		error(tname);
42*46013Sbostic         (void)unlink(path);
43*46013Sbostic 	(void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
44*46013Sbostic 	return(fd);
45*46013Sbostic }
46*46013Sbostic 
47*46013Sbostic void *
48*46013Sbostic emalloc(len)
49*46013Sbostic 	int len;
50*46013Sbostic {
51*46013Sbostic 	char *p;
52*46013Sbostic 
53*46013Sbostic 	if (!(p = malloc((u_int)len)))
54*46013Sbostic 		error(archive);
55*46013Sbostic 	return(p);
56*46013Sbostic }
57*46013Sbostic 
58*46013Sbostic char *
59*46013Sbostic rname(path)
60*46013Sbostic 	char *path;
61*46013Sbostic {
62*46013Sbostic 	register char *ind;
63*46013Sbostic 
64*46013Sbostic 	return((ind = rindex(path, '/')) ? ind + 1 : path);
65*46013Sbostic }
66*46013Sbostic 
67*46013Sbostic badfmt()
68*46013Sbostic {
69*46013Sbostic 	errno = EFTYPE;
70*46013Sbostic 	error(archive);
71*46013Sbostic }
72*46013Sbostic 
73*46013Sbostic error(name)
74*46013Sbostic 	char *name;
75*46013Sbostic {
76*46013Sbostic 	(void)fprintf(stderr, "ranlib: %s: %s\n", name, strerror(errno));
77*46013Sbostic 	exit(1);
78*46013Sbostic }
79