Annotation of src/usr.bin/ar/extract.c, Revision 1.7
1.7 ! miod 1: /* $OpenBSD: extract.c,v 1.6 2003/06/12 20:58:08 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: extract.c,v 1.5 1995/03/26 03:27:53 glass Exp $ */
3:
4: /*-
5: * Copyright (c) 1990, 1993, 1994
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Hugh Smith at The University of Guelph.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
1.5 millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
37: #if 0
38: static char sccsid[] = "@(#)extract.c 8.3 (Berkeley) 4/2/94";
39: #else
1.7 ! miod 40: static char rcsid[] = "$OpenBSD: extract.c,v 1.6 2003/06/12 20:58:08 deraadt Exp $";
1.1 deraadt 41: #endif
42: #endif /* not lint */
43:
44: #include <sys/param.h>
45: #include <sys/time.h>
46: #include <sys/stat.h>
47:
48: #include <dirent.h>
49: #include <err.h>
50: #include <fcntl.h>
51: #include <stdio.h>
52: #include <string.h>
53: #include <unistd.h>
54:
55: #include "archive.h"
56: #include "extern.h"
57:
58: /*
59: * extract --
60: * Extract files from the named archive - if member names given only
61: * extract those members otherwise extract all members. If 'o' option
62: * selected modify date of newly created file to be same as archive
63: * members date otherwise date is time of extraction. Does not modify
64: * archive.
65: */
66: int
1.6 deraadt 67: extract(char **argv)
1.1 deraadt 68: {
69: char *file;
70: int afd, all, eval, tfd;
71: struct timeval tv[2];
72: struct stat sb;
73: CF cf;
74:
75: eval = 0;
76: tv[0].tv_usec = tv[1].tv_usec = 0;
77:
78: afd = open_archive(O_RDONLY);
79:
80: /* Read from an archive, write to disk; pad on read. */
81: SETCF(afd, archive, 0, 0, RPAD);
82: for (all = !*argv; get_arobj(afd);) {
83: if (all)
84: file = chdr.name;
85: else if (!(file = files(argv))) {
86: skip_arobj(afd);
87: continue;
88: }
89:
90: if (options & AR_U && !stat(file, &sb) &&
91: sb.st_mtime > chdr.date)
92: continue;
93:
1.4 denny 94: if (options & AR_CC) {
95: /* -C means do not overwrite existing files */
96: if ((tfd = open(file, O_WRONLY|O_CREAT|O_EXCL,
97: S_IWUSR)) < 0) {
98: skip_arobj(afd);
99: eval = 1;
100: continue;
101: }
102: } else {
103: if ((tfd = open(file, O_WRONLY|O_CREAT|O_TRUNC,
104: S_IWUSR)) < 0) {
105: warn("%s", file);
106: skip_arobj(afd);
107: eval = 1;
108: continue;
109: }
1.1 deraadt 110: }
111:
112: if (options & AR_V)
113: (void)printf("x - %s\n", file);
114:
115: cf.wfd = tfd;
116: cf.wname = file;
117: copy_ar(&cf, chdr.size);
118:
1.7 ! miod 119: if (fchmod(tfd, (mode_t)chdr.mode)) {
1.1 deraadt 120: warn("chmod: %s", file);
121: eval = 1;
122: }
123: if (options & AR_O) {
124: tv[0].tv_sec = tv[1].tv_sec = chdr.date;
1.3 deraadt 125: if (futimes(tfd, tv)) {
1.1 deraadt 126: warn("utimes: %s", file);
127: eval = 1;
128: }
129: }
130: (void)close(tfd);
131: if (!all && !*argv)
132: break;
133: }
134: close_archive(afd);
135:
136: if (*argv) {
137: orphans(argv);
138: return (1);
139: }
140: return (0);
141: }