xref: /csrg-svn/usr.bin/ar/misc.c (revision 46000)
1*46000Sbostic /*-
2*46000Sbostic  * Copyright (c) 1990 The Regents of the University of California.
3*46000Sbostic  * All rights reserved.
4*46000Sbostic  *
5*46000Sbostic  * This code is derived from software contributed to Berkeley by
6*46000Sbostic  * Hugh Smith at The University of Guelph.
7*46000Sbostic  *
8*46000Sbostic  * %sccs.include.redist.c%
9*46000Sbostic  */
10*46000Sbostic 
11*46000Sbostic #ifndef lint
12*46000Sbostic static char sccsid[] = "@(#)misc.c	5.1 (Berkeley) 01/17/91";
13*46000Sbostic #endif /* not lint */
14*46000Sbostic 
15*46000Sbostic #include <sys/param.h>
16*46000Sbostic #include <sys/signal.h>
17*46000Sbostic #include <sys/errno.h>
18*46000Sbostic #include <dirent.h>
19*46000Sbostic #include <stdio.h>
20*46000Sbostic #include "pathnames.h"
21*46000Sbostic #include "archive.h"
22*46000Sbostic 
23*46000Sbostic extern CHDR chdr;			/* converted header */
24*46000Sbostic extern char *archive;			/* archive name */
25*46000Sbostic char *tname = "temporary file";		/* temporary file "name" */
26*46000Sbostic 
27*46000Sbostic tmp()
28*46000Sbostic {
29*46000Sbostic 	extern char *envtmp;
30*46000Sbostic 	sigset_t set, oset;
31*46000Sbostic 	static int first;
32*46000Sbostic 	int fd;
33*46000Sbostic 	char path[MAXPATHLEN];
34*46000Sbostic 
35*46000Sbostic 	if (!first && !envtmp) {
36*46000Sbostic 		envtmp = getenv("TMPDIR");
37*46000Sbostic 		first = 1;
38*46000Sbostic 	}
39*46000Sbostic 
40*46000Sbostic 	if (envtmp)
41*46000Sbostic 		(void)sprintf(path, "%s/%s", envtmp, _NAME_ARTMP);
42*46000Sbostic 	else
43*46000Sbostic 		bcopy(_PATH_ARTMP, path, sizeof(_PATH_ARTMP));
44*46000Sbostic 
45*46000Sbostic 	sigemptyset(&set);
46*46000Sbostic 	sigaddset(&set, SIGHUP);
47*46000Sbostic 	sigaddset(&set, SIGINT);
48*46000Sbostic 	sigaddset(&set, SIGQUIT);
49*46000Sbostic 	sigaddset(&set, SIGTERM);
50*46000Sbostic 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
51*46000Sbostic 	if ((fd = mkstemp(path)) == -1)
52*46000Sbostic 		error(tname);
53*46000Sbostic         (void)unlink(path);
54*46000Sbostic 	(void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
55*46000Sbostic 	return(fd);
56*46000Sbostic }
57*46000Sbostic 
58*46000Sbostic /*
59*46000Sbostic  * files --
60*46000Sbostic  *	See if the current file matches any file in the argument list; if it
61*46000Sbostic  * 	does, remove it from the argument list.
62*46000Sbostic  */
63*46000Sbostic files(argv)
64*46000Sbostic 	char **argv;
65*46000Sbostic {
66*46000Sbostic 	register char **list;
67*46000Sbostic 
68*46000Sbostic 	for (list = argv; *list; ++list)
69*46000Sbostic 		if (compare(*list)) {
70*46000Sbostic 			for (; list[0] = list[1]; ++list);
71*46000Sbostic 			return(1);
72*46000Sbostic 		}
73*46000Sbostic 	return(0);
74*46000Sbostic }
75*46000Sbostic 
76*46000Sbostic char *
77*46000Sbostic rname(path)
78*46000Sbostic 	char *path;
79*46000Sbostic {
80*46000Sbostic 	register char *ind;
81*46000Sbostic 
82*46000Sbostic 	return((ind = rindex(path, '/')) ? ind + 1 : path);
83*46000Sbostic }
84*46000Sbostic 
85*46000Sbostic compare(dest)
86*46000Sbostic 	char *dest;
87*46000Sbostic {
88*46000Sbostic 	char *rname();
89*46000Sbostic 
90*46000Sbostic 	return(!strcmp(chdr.name, rname(dest)));
91*46000Sbostic }
92*46000Sbostic 
93*46000Sbostic badfmt()
94*46000Sbostic {
95*46000Sbostic 	errno = EFTYPE;
96*46000Sbostic 	error(archive);
97*46000Sbostic }
98*46000Sbostic 
99*46000Sbostic error(name)
100*46000Sbostic 	char *name;
101*46000Sbostic {
102*46000Sbostic 	(void)fprintf(stderr, "ar: %s: %s\n", name, strerror(errno));
103*46000Sbostic 	exit(1);
104*46000Sbostic }
105