xref: /original-bsd/usr.bin/ar/misc.c (revision 51dd4909)
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.5 (Berkeley) 03/11/91";
13 #endif /* not lint */
14 
15 #include <sys/param.h>
16 #include <sys/errno.h>
17 #include <signal.h>
18 #include <dirent.h>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "archive.h"
24 #include "extern.h"
25 #include "pathnames.h"
26 
27 extern CHDR chdr;			/* converted header */
28 extern char *archive;			/* archive name */
29 char *tname = "temporary file";		/* temporary file "name" */
30 
31 tmp()
32 {
33 	extern char *envtmp;
34 	sigset_t set, oset;
35 	static int first;
36 	int fd;
37 	char path[MAXPATHLEN];
38 
39 	if (!first && !envtmp) {
40 		envtmp = getenv("TMPDIR");
41 		first = 1;
42 	}
43 
44 	if (envtmp)
45 		(void)sprintf(path, "%s/%s", envtmp, _NAME_ARTMP);
46 	else
47 		bcopy(_PATH_ARTMP, path, sizeof(_PATH_ARTMP));
48 
49 	sigemptyset(&set);
50 	sigaddset(&set, SIGHUP);
51 	sigaddset(&set, SIGINT);
52 	sigaddset(&set, SIGQUIT);
53 	sigaddset(&set, SIGTERM);
54 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
55 	if ((fd = mkstemp(path)) == -1)
56 		error(tname);
57         (void)unlink(path);
58 	(void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
59 	return(fd);
60 }
61 
62 /*
63  * files --
64  *	See if the current file matches any file in the argument list; if it
65  * 	does, remove it from the argument list.
66  */
67 char *
68 files(argv)
69 	char **argv;
70 {
71 	register char **list;
72 	char *p;
73 
74 	for (list = argv; *list; ++list)
75 		if (compare(*list)) {
76 			p = *list;
77 			for (; list[0] = list[1]; ++list);
78 			return(p);
79 		}
80 	return(NULL);
81 }
82 
83 void
84 orphans(argv)
85 	char **argv;
86 {
87 	for (; *argv; ++argv)
88 		(void)fprintf(stderr,
89 		    "ar: %s: not found in archive.\n", *argv);
90 }
91 
92 char *
93 rname(path)
94 	char *path;
95 {
96 	register char *ind;
97 
98 	return((ind = rindex(path, '/')) ? ind + 1 : path);
99 }
100 
101 compare(dest)
102 	char *dest;
103 {
104 	if (options & AR_S)
105 		return(!strncmp(chdr.name, rname(dest), OLDARMAXNAME));
106 	return(!strcmp(chdr.name, rname(dest)));
107 }
108 
109 void
110 badfmt()
111 {
112 	errno = EFTYPE;
113 	error(archive);
114 }
115 
116 void
117 error(name)
118 	char *name;
119 {
120 	(void)fprintf(stderr, "ar: %s: %s\n", name, strerror(errno));
121 	exit(1);
122 }
123