xref: /original-bsd/usr.bin/ar/replace.c (revision 7e5c8007)
1 /*-
2  * Copyright (c) 1990, 1993, 1994
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[] = "@(#)replace.c	8.3 (Berkeley) 04/02/94";
13 #endif /* not lint */
14 
15 #include <sys/param.h>
16 #include <sys/stat.h>
17 
18 #include <ar.h>
19 #include <dirent.h>
20 #include <err.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #include "archive.h"
27 #include "extern.h"
28 
29 /*
30  * replace --
31  *	Replace or add named members to archive.  Entries already in the
32  *	archive are swapped in place.  Others are added before or after
33  *	the key entry, based on the a, b and i options.  If the u option
34  *	is specified, modification dates select for replacement.
35  */
36 int
37 replace(argv)
38 	char **argv;
39 {
40 	char *file;
41 	int afd, curfd, errflg, exists, mods, sfd, tfd1, tfd2;
42 	struct stat sb;
43 	CF cf;
44 	off_t size, tsize;
45 
46 	errflg = 0;
47 	/*
48 	 * If doesn't exist, simply append to the archive.  There's
49 	 * a race here, but it's pretty short, and not worth fixing.
50 	 */
51 	exists = !stat(archive, &sb);
52 	afd = open_archive(O_CREAT|O_RDWR);
53 
54 	if (!exists) {
55 		tfd1 = -1;
56 		tfd2 = tmp();
57 		goto append;
58 	}
59 
60 	tfd1 = tmp();			/* Files before key file. */
61 	tfd2 = tmp();			/* Files after key file. */
62 
63 	/*
64 	 * Break archive into two parts -- entries before and after the key
65 	 * entry.  If positioning before the key, place the key at the
66 	 * beginning of the after key entries and if positioning after the
67 	 * key, place the key at the end of the before key entries.  Put it
68 	 * all back together at the end.
69 	 */
70 	mods = (options & (AR_A|AR_B));
71 	for (curfd = tfd1; get_arobj(afd);) {
72 		if (*argv && (file = files(argv))) {
73 			if ((sfd = open(file, O_RDONLY)) < 0) {
74 				errflg = 1;
75 				warn("%s", file);
76 				goto useold;
77 			}
78 			(void)fstat(sfd, &sb);
79 			if (options & AR_U && sb.st_mtime <= chdr.date)
80 				goto useold;
81 
82 			if (options & AR_V)
83 			     (void)printf("r - %s\n", file);
84 
85 			/* Read from disk, write to an archive; pad on write */
86 			SETCF(sfd, file, curfd, tname, WPAD);
87 			put_arobj(&cf, &sb);
88 			(void)close(sfd);
89 			skip_arobj(afd);
90 			continue;
91 		}
92 
93 		if (mods && compare(posname)) {
94 			mods = 0;
95 			if (options & AR_B)
96 				curfd = tfd2;
97 			/* Read and write to an archive; pad on both. */
98 			SETCF(afd, archive, curfd, tname, RPAD|WPAD);
99 			put_arobj(&cf, (struct stat *)NULL);
100 			if (options & AR_A)
101 				curfd = tfd2;
102 		} else {
103 			/* Read and write to an archive; pad on both. */
104 useold:			SETCF(afd, archive, curfd, tname, RPAD|WPAD);
105 			put_arobj(&cf, (struct stat *)NULL);
106 		}
107 	}
108 
109 	if (mods) {
110 		warnx("%s: archive member not found", posarg);
111                 close_archive(afd);
112                 return (1);
113         }
114 
115 	/* Append any left-over arguments to the end of the after file. */
116 append:	while (file = *argv++) {
117 		if (options & AR_V)
118 			(void)printf("a - %s\n", file);
119 		if ((sfd = open(file, O_RDONLY)) < 0) {
120 			errflg = 1;
121 			warn("%s", file);
122 			continue;
123 		}
124 		(void)fstat(sfd, &sb);
125 		/* Read from disk, write to an archive; pad on write. */
126 		SETCF(sfd, file,
127 		    options & (AR_A|AR_B) ? tfd1 : tfd2, tname, WPAD);
128 		put_arobj(&cf, &sb);
129 		(void)close(sfd);
130 	}
131 
132 	(void)lseek(afd, (off_t)SARMAG, SEEK_SET);
133 
134 	SETCF(tfd1, tname, afd, archive, NOPAD);
135 	if (tfd1 != -1) {
136 		tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR);
137 		(void)lseek(tfd1, (off_t)0, SEEK_SET);
138 		copy_ar(&cf, size);
139 	} else
140 		tsize = 0;
141 
142 	tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR);
143 	(void)lseek(tfd2, (off_t)0, SEEK_SET);
144 	cf.rfd = tfd2;
145 	copy_ar(&cf, size);
146 
147 	(void)ftruncate(afd, tsize + SARMAG);
148 	close_archive(afd);
149 	return (errflg);
150 }
151