xref: /original-bsd/usr.bin/ar/misc.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1990, 1993
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.1 (Berkeley) 06/06/93";
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 	sigfillset(&set);
50 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
51 	if ((fd = mkstemp(path)) == -1)
52 		error(tname);
53         (void)unlink(path);
54 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
55 	return(fd);
56 }
57 
58 /*
59  * files --
60  *	See if the current file matches any file in the argument list; if it
61  * 	does, remove it from the argument list.
62  */
63 char *
64 files(argv)
65 	char **argv;
66 {
67 	register char **list;
68 	char *p;
69 
70 	for (list = argv; *list; ++list)
71 		if (compare(*list)) {
72 			p = *list;
73 			for (; list[0] = list[1]; ++list);
74 			return(p);
75 		}
76 	return(NULL);
77 }
78 
79 void
80 orphans(argv)
81 	char **argv;
82 {
83 	for (; *argv; ++argv)
84 		(void)fprintf(stderr,
85 		    "ar: %s: not found in archive.\n", *argv);
86 }
87 
88 char *
89 rname(path)
90 	char *path;
91 {
92 	register char *ind;
93 
94 	return((ind = rindex(path, '/')) ? ind + 1 : path);
95 }
96 
97 compare(dest)
98 	char *dest;
99 {
100 	if (options & AR_TR)
101 		return(!strncmp(chdr.name, rname(dest), OLDARMAXNAME));
102 	return(!strcmp(chdr.name, rname(dest)));
103 }
104 
105 void
106 badfmt()
107 {
108 	errno = EFTYPE;
109 	error(archive);
110 }
111 
112 void
113 error(name)
114 	char *name;
115 {
116 	(void)fprintf(stderr, "ar: %s: %s\n", name, strerror(errno));
117 	exit(1);
118 }
119