Annotation of src/usr.bin/ar/extract.c, Revision 1.8
1.8 ! deraadt 1: /* $OpenBSD: extract.c,v 1.7 2004/09/07 09:41:43 miod 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: #include <sys/param.h>
37: #include <sys/time.h>
38: #include <sys/stat.h>
39:
40: #include <dirent.h>
41: #include <err.h>
42: #include <fcntl.h>
43: #include <stdio.h>
44: #include <string.h>
45: #include <unistd.h>
46:
47: #include "archive.h"
48: #include "extern.h"
49:
50: /*
51: * extract --
52: * Extract files from the named archive - if member names given only
53: * extract those members otherwise extract all members. If 'o' option
54: * selected modify date of newly created file to be same as archive
55: * members date otherwise date is time of extraction. Does not modify
56: * archive.
57: */
58: int
1.6 deraadt 59: extract(char **argv)
1.1 deraadt 60: {
61: char *file;
62: int afd, all, eval, tfd;
63: struct timeval tv[2];
64: struct stat sb;
65: CF cf;
66:
67: eval = 0;
68: tv[0].tv_usec = tv[1].tv_usec = 0;
69:
70: afd = open_archive(O_RDONLY);
71:
72: /* Read from an archive, write to disk; pad on read. */
73: SETCF(afd, archive, 0, 0, RPAD);
74: for (all = !*argv; get_arobj(afd);) {
75: if (all)
76: file = chdr.name;
77: else if (!(file = files(argv))) {
78: skip_arobj(afd);
79: continue;
80: }
81:
82: if (options & AR_U && !stat(file, &sb) &&
83: sb.st_mtime > chdr.date)
84: continue;
85:
1.4 denny 86: if (options & AR_CC) {
87: /* -C means do not overwrite existing files */
88: if ((tfd = open(file, O_WRONLY|O_CREAT|O_EXCL,
89: S_IWUSR)) < 0) {
90: skip_arobj(afd);
91: eval = 1;
92: continue;
93: }
94: } else {
95: if ((tfd = open(file, O_WRONLY|O_CREAT|O_TRUNC,
96: S_IWUSR)) < 0) {
97: warn("%s", file);
98: skip_arobj(afd);
99: eval = 1;
100: continue;
101: }
1.1 deraadt 102: }
103:
104: if (options & AR_V)
105: (void)printf("x - %s\n", file);
106:
107: cf.wfd = tfd;
108: cf.wname = file;
109: copy_ar(&cf, chdr.size);
110:
1.7 miod 111: if (fchmod(tfd, (mode_t)chdr.mode)) {
1.1 deraadt 112: warn("chmod: %s", file);
113: eval = 1;
114: }
115: if (options & AR_O) {
116: tv[0].tv_sec = tv[1].tv_sec = chdr.date;
1.3 deraadt 117: if (futimes(tfd, tv)) {
1.1 deraadt 118: warn("utimes: %s", file);
119: eval = 1;
120: }
121: }
122: (void)close(tfd);
123: if (!all && !*argv)
124: break;
125: }
126: close_archive(afd);
127:
128: if (*argv) {
129: orphans(argv);
130: return (1);
131: }
132: return (0);
133: }