xref: /csrg-svn/usr.bin/ar/misc.c (revision 68966)
146000Sbostic /*-
266645Spendry  * Copyright (c) 1990, 1993, 1994
361906Sbostic  *	The Regents of the University of California.  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*68966Sbostic static char sccsid[] = "@(#)misc.c	8.4 (Berkeley) 04/27/95";
1346000Sbostic #endif /* not lint */
1446000Sbostic 
1546000Sbostic #include <sys/param.h>
1666567Spendry 
1766567Spendry #include <dirent.h>
1866567Spendry #include <err.h>
1966567Spendry #include <errno.h>
2047202Sbostic #include <signal.h>
2146000Sbostic #include <stdio.h>
2247230Sbostic #include <stdlib.h>
2347230Sbostic #include <string.h>
2466567Spendry #include <unistd.h>
2566567Spendry 
2647202Sbostic #include "archive.h"
2747230Sbostic #include "extern.h"
2846000Sbostic #include "pathnames.h"
2946000Sbostic 
3046000Sbostic char *tname = "temporary file";		/* temporary file "name" */
3146000Sbostic 
3266567Spendry int
tmp()3346000Sbostic tmp()
3446000Sbostic {
3546000Sbostic 	extern char *envtmp;
3646000Sbostic 	sigset_t set, oset;
3746000Sbostic 	static int first;
3846000Sbostic 	int fd;
3946000Sbostic 	char path[MAXPATHLEN];
4046000Sbostic 
4146000Sbostic 	if (!first && !envtmp) {
4246000Sbostic 		envtmp = getenv("TMPDIR");
4346000Sbostic 		first = 1;
4446000Sbostic 	}
4546000Sbostic 
4646000Sbostic 	if (envtmp)
4746000Sbostic 		(void)sprintf(path, "%s/%s", envtmp, _NAME_ARTMP);
4846000Sbostic 	else
4966567Spendry 		strcpy(path, _PATH_ARTMP);
5046000Sbostic 
5149893Sbostic 	sigfillset(&set);
5246000Sbostic 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
5346000Sbostic 	if ((fd = mkstemp(path)) == -1)
5446000Sbostic 		error(tname);
5546000Sbostic         (void)unlink(path);
5649893Sbostic 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
5766567Spendry 	return (fd);
5846000Sbostic }
5946000Sbostic 
6046000Sbostic /*
6146000Sbostic  * files --
6246000Sbostic  *	See if the current file matches any file in the argument list; if it
6346000Sbostic  * 	does, remove it from the argument list.
6446000Sbostic  */
6547230Sbostic char *
files(argv)6646000Sbostic files(argv)
6746000Sbostic 	char **argv;
6846000Sbostic {
6966567Spendry 	char **list, *p;
7046000Sbostic 
7146000Sbostic 	for (list = argv; *list; ++list)
7246000Sbostic 		if (compare(*list)) {
7347230Sbostic 			p = *list;
7466567Spendry 			for (; list[0] = list[1]; ++list)
7566567Spendry 				continue;
7666567Spendry 			return (p);
7746000Sbostic 		}
7866567Spendry 	return (NULL);
7946000Sbostic }
8046000Sbostic 
8147230Sbostic void
orphans(argv)8247230Sbostic orphans(argv)
8347230Sbostic 	char **argv;
8447230Sbostic {
8566567Spendry 
8647230Sbostic 	for (; *argv; ++argv)
8766567Spendry 		warnx("%s: not found in archive", *argv);
8847230Sbostic }
8947230Sbostic 
9046000Sbostic char *
rname(path)9146000Sbostic rname(path)
9246000Sbostic 	char *path;
9346000Sbostic {
9466567Spendry 	char *ind;
9546000Sbostic 
9666567Spendry 	return ((ind = strrchr(path, '/')) ? ind + 1 : path);
9746000Sbostic }
9846000Sbostic 
9966567Spendry int
compare(dest)10046000Sbostic compare(dest)
10146000Sbostic 	char *dest;
10246000Sbostic {
10366567Spendry 
10447602Sbostic 	if (options & AR_TR)
10566567Spendry 		return (!strncmp(chdr.name, rname(dest), OLDARMAXNAME));
10666567Spendry 	return (!strcmp(chdr.name, rname(dest)));
10746000Sbostic }
10846000Sbostic 
10947230Sbostic void
badfmt()11046000Sbostic badfmt()
11146000Sbostic {
11266567Spendry 
113*68966Sbostic 	errno = EFTYPE;
114*68966Sbostic 	err(1, "%s", archive);
11546000Sbostic }
11646000Sbostic 
11747230Sbostic void
error(name)11846000Sbostic error(name)
11946000Sbostic 	char *name;
12046000Sbostic {
12166567Spendry 
122*68966Sbostic 	err(1, "%s", name);
12346000Sbostic }
124