xref: /original-bsd/usr.bin/ar/extract.c (revision b4971bb3)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  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
12 static char sccsid[] = "@(#)extract.c	8.1 (Berkeley) 06/06/93";
13 #endif /* not lint */
14 
15 #include <sys/param.h>
16 #include <sys/time.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <errno.h>
20 #include <dirent.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include "archive.h"
25 #include "extern.h"
26 
27 extern CHDR chdr;			/* converted header */
28 extern char *archive;			/* archive name */
29 
30 /*
31  * extract --
32  *	Extract files from the named archive - if member names given only
33  *	extract those members otherwise extract all members.  If 'o' option
34  *	selected modify date of newly created file to be same as archive
35  *	members date otherwise date is time of extraction.  Does not modify
36  *	archive.
37  */
38 extract(argv)
39 	char **argv;
40 {
41 	register int afd, all, tfd;
42 	struct timeval tv[2];
43 	struct stat sb;
44 	CF cf;
45 	int eval;
46 	char *file;
47 
48 	eval = 0;
49 	tv[0].tv_usec = tv[1].tv_usec = 0;
50 
51 	afd = open_archive(O_RDONLY);
52 
53 	/* Read from an archive, write to disk; pad on read. */
54 	SETCF(afd, archive, 0, 0, RPAD);
55 	for (all = !*argv; get_arobj(afd);) {
56 		if (all)
57 			file = chdr.name;
58 		else if (!(file = files(argv))) {
59 			skip_arobj(afd);
60 			continue;
61 		}
62 
63 		if (options & AR_U && !stat(file, &sb) &&
64 		    sb.st_mtime > chdr.date)
65 			continue;
66 
67 		if ((tfd = open(file, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR)) < 0) {
68 			(void)fprintf(stderr, "ar: %s: %s.\n",
69 			    file, strerror(errno));
70 			skip_arobj(afd);
71 			eval = 1;
72 			continue;
73 		}
74 
75 		if (options & AR_V)
76 			(void)printf("x - %s\n", file);
77 
78 		cf.wfd = tfd;
79 		cf.wname = file;
80 		copy_ar(&cf, chdr.size);
81 
82 		if (fchmod(tfd, (short)chdr.mode)) {
83 			(void)fprintf(stderr, "ar: %s: chmod: %s\n",
84 			    file, strerror(errno));
85 			eval = 1;
86 		}
87 		if (options & AR_O) {
88 			tv[0].tv_sec = tv[1].tv_sec = chdr.date;
89 			if (utimes(file, tv)) {
90 				(void)fprintf(stderr, "ar: %s: utimes: %s\n",
91 				    file, strerror(errno));
92 				eval = 1;
93 			}
94 		}
95 		(void)close(tfd);
96 		if (!all && !*argv)
97 			break;
98 	}
99 	close_archive(afd);
100 
101 	if (*argv) {
102 		orphans(argv);
103 		return(1);
104 	}
105 	return(0);
106 }
107