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