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