xref: /original-bsd/usr.bin/ar/replace.c (revision 6d57652c)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * 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	5.3 (Berkeley) 01/21/91";
13 #endif /* not lint */
14 
15 #include <sys/param.h>
16 #include <sys/stat.h>
17 #include <sys/errno.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <dirent.h>
21 #include <ar.h>
22 #include <stdio.h>
23 #include "archive.h"
24 
25 extern CHDR chdr;			/* converted header */
26 extern char *archive;			/* archive name */
27 extern char *tname;                     /* temporary file "name" */
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 replace(argv)
37 	char **argv;
38 {
39 	extern char *posname;		/* positioning file name */
40 	register char *file;
41 	register int afd, curfd, mods, sfd;
42 	struct stat sb;
43 	CF cf;
44 	off_t size, tsize;
45 	int err, exists, tfd1, tfd2;
46 	char *rname();
47 
48 	/*
49 	 * If doesn't exist, simply append to the archive.  There's
50 	 * a race here, but it's pretty short, and not worth fixing.
51 	 */
52 	exists = !stat(archive, &sb);
53 	afd = open_archive(O_CREAT|O_RDWR);
54 
55 	if (!exists) {
56 		tfd1 = -1;
57 		tfd2 = tmp();
58 		goto append;
59 	}
60 
61 	tfd1 = tmp();			/* Files before key file. */
62 	tfd2 = tmp();			/* Files after key file. */
63 
64 	/*
65 	 * Break archive into two parts -- entries before and after the key
66 	 * entry.  If positioning before the key, place the key at the
67 	 * beginning of the after key entries and if positioning after the
68 	 * key, place the key at the end of the before key entries.  Put it
69 	 * all back together at the end.
70 	 */
71 	mods = (options & (AR_A|AR_B));
72 	for (err = 0, curfd = tfd1; get_header(afd);) {
73 		if ((file = *argv) && files(argv)) {
74 			if ((sfd = open(file, O_RDONLY)) < 0) {
75 				err = 1;
76 				(void)fprintf(stderr, "ar: %s: %s.\n",
77 				    file, strerror(errno));
78 				goto useold;
79 			}
80 			(void)fstat(sfd, &sb);
81 			if (options & AR_U && sb.st_mtime <= chdr.date)
82 				goto useold;
83 
84 			if (options & AR_V)
85 			     (void)printf("r - %s\n", chdr.name);
86 
87 			SETCF(sfd, file, curfd, tname, WPAD);
88 			put_header(&cf, &sb);
89 			copyfile(&cf, sb.st_size);
90 			(void)close(sfd);
91 			SKIP(afd, chdr.size, archive);
92 			continue;
93 		}
94 
95 		if (mods && compare(posname)) {
96 			mods = 0;
97 			if (options & AR_B)
98 				curfd = tfd2;
99 			SETCF(afd, archive, curfd, tname, RPAD|WPAD);
100 			put_header(&cf, (struct stat *)NULL);
101 			copyfile(&cf, chdr.size);
102 			if (options & AR_A)
103 				curfd = tfd2;
104 		} else {
105 useold:			SETCF(afd, archive, curfd, tname, RPAD|WPAD);
106 			put_header(&cf, (struct stat *)NULL);
107 			copyfile(&cf, chdr.size);
108 		}
109 	}
110 
111 	if (mods) {
112 		(void)fprintf(stderr, "ar: %s: archive member not found.\n",
113 		    posname);
114                 close_archive(afd);
115                 return(1);
116         }
117 
118 	/* Append any left-over arguments to the end of the after file. */
119 append:	while (file = *argv++) {
120 		if (options & AR_V)
121 			(void)printf("a - %s\n", rname(file));
122 		if ((sfd = open(file, O_RDONLY)) < 0) {
123 			err = 1;
124 			(void)fprintf(stderr, "ar: %s: %s.\n",
125 			    file, strerror(errno));
126 			continue;
127 		}
128 		(void)fstat(sfd, &sb);
129 		SETCF(sfd, file,
130 		    options & (AR_A|AR_B) ? tfd1 : tfd2, tname, WPAD);
131 		put_header(&cf, &sb);
132 		copyfile(&cf, sb.st_size);
133 		(void)close(sfd);
134 	}
135 
136 	(void)lseek(afd, (off_t)SARMAG, SEEK_SET);
137 
138 	SETCF(tfd1, tname, afd, archive, 0);
139 	if (tfd1 != -1) {
140 		tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR);
141 		(void)lseek(tfd1, (off_t)0, SEEK_SET);
142 		copyfile(&cf, size);
143 	} else
144 		tsize = 0;
145 
146 	tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR);
147 	(void)lseek(tfd2, (off_t)0, SEEK_SET);
148 	cf.rfd = tfd2;
149 	copyfile(&cf, size);
150 
151 	(void)ftruncate(afd, tsize + SARMAG);
152 	close_archive(afd);
153 	return(err);
154 }
155