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