4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / usr.bin / ar / misc.c
CommitLineData
834794c6 1/*-
4e6c9859
KB
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
834794c6
KB
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
4e6c9859 12static char sccsid[] = "@(#)misc.c 8.1 (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
24a28b45 49 sigfillset(&set);
834794c6
KB
50 (void)sigprocmask(SIG_BLOCK, &set, &oset);
51 if ((fd = mkstemp(path)) == -1)
52 error(tname);
53 (void)unlink(path);
24a28b45 54 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
834794c6
KB
55 return(fd);
56}
57
58/*
59 * files --
60 * See if the current file matches any file in the argument list; if it
61 * does, remove it from the argument list.
62 */
3f2e0c90 63char *
834794c6
KB
64files(argv)
65 char **argv;
66{
67 register char **list;
3f2e0c90 68 char *p;
834794c6
KB
69
70 for (list = argv; *list; ++list)
71 if (compare(*list)) {
3f2e0c90 72 p = *list;
834794c6 73 for (; list[0] = list[1]; ++list);
3f2e0c90 74 return(p);
834794c6 75 }
3f2e0c90
KB
76 return(NULL);
77}
78
79void
80orphans(argv)
81 char **argv;
82{
83 for (; *argv; ++argv)
84 (void)fprintf(stderr,
85 "ar: %s: not found in archive.\n", *argv);
834794c6
KB
86}
87
88char *
89rname(path)
90 char *path;
91{
92 register char *ind;
93
94 return((ind = rindex(path, '/')) ? ind + 1 : path);
95}
96
97compare(dest)
98 char *dest;
99{
4f81e05f 100 if (options & AR_TR)
5504c5d3 101 return(!strncmp(chdr.name, rname(dest), OLDARMAXNAME));
834794c6
KB
102 return(!strcmp(chdr.name, rname(dest)));
103}
104
3f2e0c90 105void
834794c6
KB
106badfmt()
107{
108 errno = EFTYPE;
109 error(archive);
110}
111
3f2e0c90 112void
834794c6
KB
113error(name)
114 char *name;
115{
116 (void)fprintf(stderr, "ar: %s: %s\n", name, strerror(errno));
117 exit(1);
118}