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