xref: /csrg-svn/usr.bin/ar/misc.c (revision 47230)
146000Sbostic /*-
246000Sbostic  * Copyright (c) 1990 The Regents of the University of California.
346000Sbostic  * All rights reserved.
446000Sbostic  *
546000Sbostic  * This code is derived from software contributed to Berkeley by
646000Sbostic  * Hugh Smith at The University of Guelph.
746000Sbostic  *
846000Sbostic  * %sccs.include.redist.c%
946000Sbostic  */
1046000Sbostic 
1146000Sbostic #ifndef lint
12*47230Sbostic static char sccsid[] = "@(#)misc.c	5.5 (Berkeley) 03/11/91";
1346000Sbostic #endif /* not lint */
1446000Sbostic 
1546000Sbostic #include <sys/param.h>
1646000Sbostic #include <sys/errno.h>
1747202Sbostic #include <signal.h>
1846000Sbostic #include <dirent.h>
1947202Sbostic #include <unistd.h>
2046000Sbostic #include <stdio.h>
21*47230Sbostic #include <stdlib.h>
22*47230Sbostic #include <string.h>
2347202Sbostic #include "archive.h"
24*47230Sbostic #include "extern.h"
2546000Sbostic #include "pathnames.h"
2646000Sbostic 
2746000Sbostic extern CHDR chdr;			/* converted header */
2846000Sbostic extern char *archive;			/* archive name */
2946000Sbostic char *tname = "temporary file";		/* temporary file "name" */
3046000Sbostic 
3146000Sbostic tmp()
3246000Sbostic {
3346000Sbostic 	extern char *envtmp;
3446000Sbostic 	sigset_t set, oset;
3546000Sbostic 	static int first;
3646000Sbostic 	int fd;
3746000Sbostic 	char path[MAXPATHLEN];
3846000Sbostic 
3946000Sbostic 	if (!first && !envtmp) {
4046000Sbostic 		envtmp = getenv("TMPDIR");
4146000Sbostic 		first = 1;
4246000Sbostic 	}
4346000Sbostic 
4446000Sbostic 	if (envtmp)
4546000Sbostic 		(void)sprintf(path, "%s/%s", envtmp, _NAME_ARTMP);
4646000Sbostic 	else
4746000Sbostic 		bcopy(_PATH_ARTMP, path, sizeof(_PATH_ARTMP));
4846000Sbostic 
4946000Sbostic 	sigemptyset(&set);
5046000Sbostic 	sigaddset(&set, SIGHUP);
5146000Sbostic 	sigaddset(&set, SIGINT);
5246000Sbostic 	sigaddset(&set, SIGQUIT);
5346000Sbostic 	sigaddset(&set, SIGTERM);
5446000Sbostic 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
5546000Sbostic 	if ((fd = mkstemp(path)) == -1)
5646000Sbostic 		error(tname);
5746000Sbostic         (void)unlink(path);
5846000Sbostic 	(void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
5946000Sbostic 	return(fd);
6046000Sbostic }
6146000Sbostic 
6246000Sbostic /*
6346000Sbostic  * files --
6446000Sbostic  *	See if the current file matches any file in the argument list; if it
6546000Sbostic  * 	does, remove it from the argument list.
6646000Sbostic  */
67*47230Sbostic char *
6846000Sbostic files(argv)
6946000Sbostic 	char **argv;
7046000Sbostic {
7146000Sbostic 	register char **list;
72*47230Sbostic 	char *p;
7346000Sbostic 
7446000Sbostic 	for (list = argv; *list; ++list)
7546000Sbostic 		if (compare(*list)) {
76*47230Sbostic 			p = *list;
7746000Sbostic 			for (; list[0] = list[1]; ++list);
78*47230Sbostic 			return(p);
7946000Sbostic 		}
80*47230Sbostic 	return(NULL);
8146000Sbostic }
8246000Sbostic 
83*47230Sbostic void
84*47230Sbostic orphans(argv)
85*47230Sbostic 	char **argv;
86*47230Sbostic {
87*47230Sbostic 	for (; *argv; ++argv)
88*47230Sbostic 		(void)fprintf(stderr,
89*47230Sbostic 		    "ar: %s: not found in archive.\n", *argv);
90*47230Sbostic }
91*47230Sbostic 
9246000Sbostic char *
9346000Sbostic rname(path)
9446000Sbostic 	char *path;
9546000Sbostic {
9646000Sbostic 	register char *ind;
9746000Sbostic 
9846000Sbostic 	return((ind = rindex(path, '/')) ? ind + 1 : path);
9946000Sbostic }
10046000Sbostic 
10146000Sbostic compare(dest)
10246000Sbostic 	char *dest;
10346000Sbostic {
10447207Sbostic 	if (options & AR_S)
10547213Sbostic 		return(!strncmp(chdr.name, rname(dest), OLDARMAXNAME));
10646000Sbostic 	return(!strcmp(chdr.name, rname(dest)));
10746000Sbostic }
10846000Sbostic 
109*47230Sbostic void
11046000Sbostic badfmt()
11146000Sbostic {
11246000Sbostic 	errno = EFTYPE;
11346000Sbostic 	error(archive);
11446000Sbostic }
11546000Sbostic 
116*47230Sbostic void
11746000Sbostic error(name)
11846000Sbostic 	char *name;
11946000Sbostic {
12046000Sbostic 	(void)fprintf(stderr, "ar: %s: %s\n", name, strerror(errno));
12146000Sbostic 	exit(1);
12246000Sbostic }
123