Mercurial > hg-old > index.cgi
comparison old-trunk/lwar/replace.c @ 339:eb230fa7d28e
Prepare for migration to hg
author | lost |
---|---|
date | Fri, 19 Mar 2010 02:54:14 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
338:e7885b3ee266 | 339:eb230fa7d28e |
---|---|
1 /* | |
2 replace.c | |
3 Copyright © 2009 William Astle | |
4 | |
5 This file is part of LWAR. | |
6 | |
7 LWAR is free software: you can redistribute it and/or modify it under the | |
8 terms of the GNU General Public License as published by the Free Software | |
9 Foundation, either version 3 of the License, or (at your option) any later | |
10 version. | |
11 | |
12 This program is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
15 more details. | |
16 | |
17 You should have received a copy of the GNU General Public License along with | |
18 this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 */ | |
21 | |
22 #include <config.h> | |
23 | |
24 #include <errno.h> | |
25 #include <stdio.h> | |
26 #include <stdlib.h> | |
27 #include <string.h> | |
28 | |
29 #include "lwar.h" | |
30 | |
31 void do_replace(void) | |
32 { | |
33 FILE *f; | |
34 FILE *nf; | |
35 unsigned char buf[8]; | |
36 long l; | |
37 int c; | |
38 FILE *f2; | |
39 int i; | |
40 char fnbuf[1024]; | |
41 char fnbuf2[1024]; | |
42 | |
43 sprintf(fnbuf, "%s.tmp", archive_file); | |
44 | |
45 f = fopen(archive_file, "r+"); | |
46 if (!f) | |
47 { | |
48 if (errno == ENOENT) | |
49 { | |
50 nf = fopen(fnbuf, "w"); | |
51 if (nf) | |
52 { | |
53 fputs("LWAR1V", nf); | |
54 goto doadd; | |
55 } | |
56 } | |
57 perror("Cannot open archive file"); | |
58 } | |
59 | |
60 fread(buf, 1, 6, f); | |
61 if (memcmp("LWAR1V", buf, 6)) | |
62 { | |
63 fprintf(stderr, "%s is not a valid archive file.\n", archive_file); | |
64 exit(1); | |
65 } | |
66 | |
67 nf = fopen(fnbuf, "w"); | |
68 if (!nf) | |
69 { | |
70 perror("Cannot create temp archive file"); | |
71 exit(1); | |
72 } | |
73 | |
74 fputs("LWAR1V", nf); | |
75 | |
76 for (;;) | |
77 { | |
78 c = fgetc(f); | |
79 if (c == EOF && ferror(f)) | |
80 { | |
81 perror("Reading archive file"); | |
82 exit(1); | |
83 } | |
84 if (c == EOF) | |
85 goto doadd; | |
86 | |
87 if (!c) | |
88 { | |
89 goto doadd; | |
90 } | |
91 | |
92 // find the end of the file name | |
93 i = 0; | |
94 while (c) | |
95 { | |
96 fnbuf2[i++] = c; | |
97 c = fgetc(f); | |
98 if (c == EOF || ferror(f)) | |
99 { | |
100 fprintf(stderr, "Bad archive file\n"); | |
101 exit(1); | |
102 } | |
103 } | |
104 fnbuf2[i] = 0; | |
105 | |
106 // get length of archive member | |
107 l = 0; | |
108 c = fgetc(f); | |
109 l = c << 24; | |
110 c = fgetc(f); | |
111 l |= c << 16; | |
112 c = fgetc(f); | |
113 l |= c << 8; | |
114 c = fgetc(f); | |
115 l |= c; | |
116 | |
117 // is it a file we are replacing? if so, do not copy it | |
118 for (i = 0; i < nfiles; i++) | |
119 { | |
120 if (!strcmp(files[i], fnbuf2)) | |
121 break; | |
122 } | |
123 if (i < nfiles) | |
124 { | |
125 fseek(f, l, SEEK_CUR); | |
126 } | |
127 else | |
128 { | |
129 // otherwise, copy it | |
130 fprintf(nf, "%s", fnbuf2); | |
131 fputc(0, nf); | |
132 fputc(l >> 24, nf); | |
133 fputc((l >> 16) & 0xff, nf); | |
134 fputc((l >> 8) & 0xff, nf); | |
135 fputc(l & 0xff, nf); | |
136 while (l) | |
137 { | |
138 c = fgetc(f); | |
139 fputc(c, nf); | |
140 l--; | |
141 } | |
142 } | |
143 } | |
144 | |
145 // done with the original file | |
146 fclose(f); | |
147 doadd: | |
148 for (i = 0; i < nfiles; i++) | |
149 { | |
150 f2 = fopen(files[i], "r"); | |
151 if (!f2) | |
152 { | |
153 fprintf(stderr, "Cannot open file %s:", files[i]); | |
154 perror(""); | |
155 exit(1); | |
156 } | |
157 fread(buf, 1, 6, f2); | |
158 if (mergeflag && !memcmp("LWAR1V", buf, 6)) | |
159 { | |
160 // add archive contents... | |
161 for (;;) | |
162 { | |
163 c = fgetc(f2); | |
164 if (c == EOF || ferror(f2)) | |
165 { | |
166 perror("Reading input archive file"); | |
167 exit(1); | |
168 } | |
169 if (c == EOF) | |
170 break; | |
171 | |
172 if (!c) | |
173 { | |
174 break; | |
175 } | |
176 | |
177 // find the end of the file name | |
178 while (c) | |
179 { | |
180 fputc(c, nf); | |
181 c = fgetc(f2); | |
182 if (c == EOF || ferror(f)) | |
183 { | |
184 fprintf(stderr, "Bad input archive file\n"); | |
185 exit(1); | |
186 } | |
187 } | |
188 fputc(0, nf); | |
189 | |
190 // get length of archive member | |
191 l = 0; | |
192 c = fgetc(f2); | |
193 fputc(c, nf); | |
194 l = c << 24; | |
195 c = fgetc(f2); | |
196 fputc(c, nf); | |
197 l |= c << 16; | |
198 c = fgetc(f2); | |
199 fputc(c, nf); | |
200 l |= c << 8; | |
201 c = fgetc(f2); | |
202 fputc(c, nf); | |
203 l |= c; | |
204 | |
205 while (l) | |
206 { | |
207 c = fgetc(f2); | |
208 fputc(c, nf); | |
209 l--; | |
210 } | |
211 } | |
212 | |
213 fclose(f2); | |
214 continue; | |
215 } | |
216 fseek(f2, 0, SEEK_END); | |
217 l = ftell(f2); | |
218 fseek(f2, 0, SEEK_SET); | |
219 fputs(files[i], nf); | |
220 fputc(0, nf); | |
221 fputc(l >> 24, nf); | |
222 fputc((l >> 16) & 0xff, nf); | |
223 fputc((l >> 8) & 0xff, nf); | |
224 fputc(l & 0xff, nf); | |
225 while (l) | |
226 { | |
227 c = fgetc(f2); | |
228 fputc(c, nf); | |
229 l--; | |
230 } | |
231 } | |
232 | |
233 // flag end of file | |
234 fputc(0, nf); | |
235 | |
236 fclose(nf); | |
237 | |
238 if (rename(fnbuf, archive_file) < 0) | |
239 { | |
240 perror("Cannot replace old archive file"); | |
241 unlink(fnbuf); | |
242 } | |
243 } |