xref: /original-bsd/usr.bin/ar/misc.c (revision d6e0261c)
1940f2c42Sbostic /*-
2077305dfSpendry  * Copyright (c) 1990, 1993, 1994
3d81290a3Sbostic  *	The Regents of the University of California.  All rights reserved.
4940f2c42Sbostic  *
5940f2c42Sbostic  * This code is derived from software contributed to Berkeley by
6940f2c42Sbostic  * Hugh Smith at The University of Guelph.
7940f2c42Sbostic  *
8940f2c42Sbostic  * %sccs.include.redist.c%
9940f2c42Sbostic  */
10940f2c42Sbostic 
11940f2c42Sbostic #ifndef lint
12*d6e0261cSbostic static char sccsid[] = "@(#)misc.c	8.4 (Berkeley) 04/27/95";
13940f2c42Sbostic #endif /* not lint */
14940f2c42Sbostic 
15940f2c42Sbostic #include <sys/param.h>
1646a6d61cSpendry 
17940f2c42Sbostic #include <dirent.h>
1846a6d61cSpendry #include <err.h>
1946a6d61cSpendry #include <errno.h>
2046a6d61cSpendry #include <signal.h>
21940f2c42Sbostic #include <stdio.h>
228ae9e470Sbostic #include <stdlib.h>
238ae9e470Sbostic #include <string.h>
2446a6d61cSpendry #include <unistd.h>
2546a6d61cSpendry 
26940f2c42Sbostic #include "archive.h"
278ae9e470Sbostic #include "extern.h"
2805df4ee1Sbostic #include "pathnames.h"
29940f2c42Sbostic 
30940f2c42Sbostic char *tname = "temporary file";		/* temporary file "name" */
31940f2c42Sbostic 
3246a6d61cSpendry int
tmp()33940f2c42Sbostic tmp()
34940f2c42Sbostic {
35940f2c42Sbostic 	extern char *envtmp;
36940f2c42Sbostic 	sigset_t set, oset;
37940f2c42Sbostic 	static int first;
38940f2c42Sbostic 	int fd;
39940f2c42Sbostic 	char path[MAXPATHLEN];
40940f2c42Sbostic 
41940f2c42Sbostic 	if (!first && !envtmp) {
42940f2c42Sbostic 		envtmp = getenv("TMPDIR");
43940f2c42Sbostic 		first = 1;
44940f2c42Sbostic 	}
45940f2c42Sbostic 
46940f2c42Sbostic 	if (envtmp)
47940f2c42Sbostic 		(void)sprintf(path, "%s/%s", envtmp, _NAME_ARTMP);
48940f2c42Sbostic 	else
4946a6d61cSpendry 		strcpy(path, _PATH_ARTMP);
50940f2c42Sbostic 
51963cfcf2Sbostic 	sigfillset(&set);
52940f2c42Sbostic 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
53940f2c42Sbostic 	if ((fd = mkstemp(path)) == -1)
54940f2c42Sbostic 		error(tname);
55940f2c42Sbostic         (void)unlink(path);
56963cfcf2Sbostic 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
57940f2c42Sbostic 	return (fd);
58940f2c42Sbostic }
59940f2c42Sbostic 
60940f2c42Sbostic /*
61940f2c42Sbostic  * files --
62940f2c42Sbostic  *	See if the current file matches any file in the argument list; if it
63940f2c42Sbostic  * 	does, remove it from the argument list.
64940f2c42Sbostic  */
658ae9e470Sbostic char *
files(argv)66940f2c42Sbostic files(argv)
67940f2c42Sbostic 	char **argv;
68940f2c42Sbostic {
6946a6d61cSpendry 	char **list, *p;
70940f2c42Sbostic 
71940f2c42Sbostic 	for (list = argv; *list; ++list)
72940f2c42Sbostic 		if (compare(*list)) {
738ae9e470Sbostic 			p = *list;
7446a6d61cSpendry 			for (; list[0] = list[1]; ++list)
7546a6d61cSpendry 				continue;
768ae9e470Sbostic 			return (p);
77940f2c42Sbostic 		}
788ae9e470Sbostic 	return (NULL);
798ae9e470Sbostic }
808ae9e470Sbostic 
818ae9e470Sbostic void
orphans(argv)828ae9e470Sbostic orphans(argv)
838ae9e470Sbostic 	char **argv;
848ae9e470Sbostic {
8546a6d61cSpendry 
868ae9e470Sbostic 	for (; *argv; ++argv)
8746a6d61cSpendry 		warnx("%s: not found in archive", *argv);
88940f2c42Sbostic }
89940f2c42Sbostic 
90940f2c42Sbostic char *
rname(path)91940f2c42Sbostic rname(path)
92940f2c42Sbostic 	char *path;
93940f2c42Sbostic {
9446a6d61cSpendry 	char *ind;
95940f2c42Sbostic 
9646a6d61cSpendry 	return ((ind = strrchr(path, '/')) ? ind + 1 : path);
97940f2c42Sbostic }
98940f2c42Sbostic 
9946a6d61cSpendry int
compare(dest)100940f2c42Sbostic compare(dest)
101940f2c42Sbostic 	char *dest;
102940f2c42Sbostic {
10346a6d61cSpendry 
104d5aa4dbdSbostic 	if (options & AR_TR)
1056f7408dbSbostic 		return (!strncmp(chdr.name, rname(dest), OLDARMAXNAME));
106940f2c42Sbostic 	return (!strcmp(chdr.name, rname(dest)));
107940f2c42Sbostic }
108940f2c42Sbostic 
1098ae9e470Sbostic void
badfmt()110940f2c42Sbostic badfmt()
111940f2c42Sbostic {
11246a6d61cSpendry 
113*d6e0261cSbostic 	errno = EFTYPE;
114*d6e0261cSbostic 	err(1, "%s", archive);
115940f2c42Sbostic }
116940f2c42Sbostic 
1178ae9e470Sbostic void
error(name)118940f2c42Sbostic error(name)
119940f2c42Sbostic 	char *name;
120940f2c42Sbostic {
12146a6d61cSpendry 
122*d6e0261cSbostic 	err(1, "%s", name);
123940f2c42Sbostic }
124