xref: /csrg-svn/usr.bin/ar/misc.c (revision 47202)
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*47202Sbostic static char sccsid[] = "@(#)misc.c	5.2 (Berkeley) 03/10/91";
1346000Sbostic #endif /* not lint */
1446000Sbostic 
1546000Sbostic #include <sys/param.h>
1646000Sbostic #include <sys/errno.h>
17*47202Sbostic #include <signal.h>
1846000Sbostic #include <dirent.h>
19*47202Sbostic #include <unistd.h>
2046000Sbostic #include <stdio.h>
21*47202Sbostic #include "archive.h"
2246000Sbostic #include "pathnames.h"
2346000Sbostic 
2446000Sbostic extern CHDR chdr;			/* converted header */
2546000Sbostic extern char *archive;			/* archive name */
2646000Sbostic char *tname = "temporary file";		/* temporary file "name" */
2746000Sbostic 
2846000Sbostic tmp()
2946000Sbostic {
3046000Sbostic 	extern char *envtmp;
3146000Sbostic 	sigset_t set, oset;
3246000Sbostic 	static int first;
3346000Sbostic 	int fd;
3446000Sbostic 	char path[MAXPATHLEN];
3546000Sbostic 
3646000Sbostic 	if (!first && !envtmp) {
3746000Sbostic 		envtmp = getenv("TMPDIR");
3846000Sbostic 		first = 1;
3946000Sbostic 	}
4046000Sbostic 
4146000Sbostic 	if (envtmp)
4246000Sbostic 		(void)sprintf(path, "%s/%s", envtmp, _NAME_ARTMP);
4346000Sbostic 	else
4446000Sbostic 		bcopy(_PATH_ARTMP, path, sizeof(_PATH_ARTMP));
4546000Sbostic 
4646000Sbostic 	sigemptyset(&set);
4746000Sbostic 	sigaddset(&set, SIGHUP);
4846000Sbostic 	sigaddset(&set, SIGINT);
4946000Sbostic 	sigaddset(&set, SIGQUIT);
5046000Sbostic 	sigaddset(&set, SIGTERM);
5146000Sbostic 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
5246000Sbostic 	if ((fd = mkstemp(path)) == -1)
5346000Sbostic 		error(tname);
5446000Sbostic         (void)unlink(path);
5546000Sbostic 	(void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
5646000Sbostic 	return(fd);
5746000Sbostic }
5846000Sbostic 
5946000Sbostic /*
6046000Sbostic  * files --
6146000Sbostic  *	See if the current file matches any file in the argument list; if it
6246000Sbostic  * 	does, remove it from the argument list.
6346000Sbostic  */
6446000Sbostic files(argv)
6546000Sbostic 	char **argv;
6646000Sbostic {
6746000Sbostic 	register char **list;
6846000Sbostic 
6946000Sbostic 	for (list = argv; *list; ++list)
7046000Sbostic 		if (compare(*list)) {
7146000Sbostic 			for (; list[0] = list[1]; ++list);
7246000Sbostic 			return(1);
7346000Sbostic 		}
7446000Sbostic 	return(0);
7546000Sbostic }
7646000Sbostic 
7746000Sbostic char *
7846000Sbostic rname(path)
7946000Sbostic 	char *path;
8046000Sbostic {
8146000Sbostic 	register char *ind;
8246000Sbostic 
8346000Sbostic 	return((ind = rindex(path, '/')) ? ind + 1 : path);
8446000Sbostic }
8546000Sbostic 
8646000Sbostic compare(dest)
8746000Sbostic 	char *dest;
8846000Sbostic {
8946000Sbostic 	char *rname();
9046000Sbostic 
9146000Sbostic 	return(!strcmp(chdr.name, rname(dest)));
9246000Sbostic }
9346000Sbostic 
9446000Sbostic badfmt()
9546000Sbostic {
9646000Sbostic 	errno = EFTYPE;
9746000Sbostic 	error(archive);
9846000Sbostic }
9946000Sbostic 
10046000Sbostic error(name)
10146000Sbostic 	char *name;
10246000Sbostic {
10346000Sbostic 	(void)fprintf(stderr, "ar: %s: %s\n", name, strerror(errno));
10446000Sbostic 	exit(1);
10546000Sbostic }
106