Mercurial > hg-old > index.cgi
comparison lwar/replace.c @ 188:bb2665c7005c
Added --extract and --replace to lwar
author | lost |
---|---|
date | Sun, 22 Mar 2009 06:51:48 +0000 |
parents | |
children | bae1e3ecdce1 |
comparison
equal
deleted
inserted
replaced
187:857cb407229e | 188:bb2665c7005c |
---|---|
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 #ifdef HAVE_CONFIG_H | |
23 #include "config.h" | |
24 #endif | |
25 | |
26 #include <errno.h> | |
27 #include <stdio.h> | |
28 #include <stdlib.h> | |
29 #include <string.h> | |
30 | |
31 #include "lwar.h" | |
32 | |
33 void do_replace(void) | |
34 { | |
35 FILE *f; | |
36 FILE *nf; | |
37 unsigned char buf[8]; | |
38 long l; | |
39 int c; | |
40 FILE *f2; | |
41 int i; | |
42 char fnbuf[1024]; | |
43 char fnbuf2[1024]; | |
44 | |
45 sprintf(fnbuf, "%s.tmp", archive_file); | |
46 | |
47 f = fopen(archive_file, "r+"); | |
48 if (!f) | |
49 { | |
50 if (errno == ENOENT) | |
51 { | |
52 nf = fopen(fnbuf, "w"); | |
53 if (nf) | |
54 { | |
55 fputs("LWAR1V", nf); | |
56 goto doadd; | |
57 } | |
58 } | |
59 perror("Cannot open archive file"); | |
60 } | |
61 | |
62 fread(buf, 1, 6, f); | |
63 if (memcmp("LWAR1V", buf, 6)) | |
64 { | |
65 fprintf(stderr, "%s is not a valid archive file.\n", archive_file); | |
66 exit(1); | |
67 } | |
68 | |
69 nf = fopen(fnbuf, "w"); | |
70 if (!nf) | |
71 { | |
72 perror("Cannot create temp archive file"); | |
73 exit(1); | |
74 } | |
75 | |
76 fputs("LWAR1V", nf); | |
77 | |
78 for (;;) | |
79 { | |
80 c = fgetc(f); | |
81 if (c == EOF && ferror(f)) | |
82 { | |
83 perror("Reading archive file"); | |
84 exit(1); | |
85 } | |
86 if (c == EOF) | |
87 goto doadd; | |
88 | |
89 if (!c) | |
90 { | |
91 goto doadd; | |
92 } | |
93 | |
94 // find the end of the file name | |
95 i = 0; | |
96 while (c) | |
97 { | |
98 fnbuf2[i++] = c; | |
99 c = fgetc(f); | |
100 if (c == EOF || ferror(f)) | |
101 { | |
102 fprintf(stderr, "Bad archive file\n"); | |
103 exit(1); | |
104 } | |
105 } | |
106 fnbuf2[i] = 0; | |
107 | |
108 // get length of archive member | |
109 l = 0; | |
110 c = fgetc(f); | |
111 l = c << 24; | |
112 c = fgetc(f); | |
113 l |= c << 16; | |
114 c = fgetc(f); | |
115 l |= c << 8; | |
116 c = fgetc(f); | |
117 l |= c; | |
118 | |
119 // is it a file we are replacing? if so, do not copy it | |
120 for (i = 0; i < nfiles; i++) | |
121 { | |
122 if (!strcmp(files[i], fnbuf2)) | |
123 break; | |
124 } | |
125 if (i < nfiles) | |
126 { | |
127 fseek(f, l, SEEK_CUR); | |
128 } | |
129 else | |
130 { | |
131 // otherwise, copy it | |
132 fprintf(nf, "%s", fnbuf2); | |
133 fputc(0, nf); | |
134 fputc(l >> 24, nf); | |
135 fputc((l >> 16) & 0xff, nf); | |
136 fputc((l >> 8) & 0xff, nf); | |
137 fputc(l & 0xff, nf); | |
138 while (l) | |
139 { | |
140 c = fgetc(f); | |
141 fputc(c, nf); | |
142 l--; | |
143 } | |
144 } | |
145 } | |
146 | |
147 // done with the original file | |
148 fclose(f); | |
149 doadd: | |
150 for (i = 0; i < nfiles; i++) | |
151 { | |
152 f2 = fopen(files[i], "r"); | |
153 if (!f2) | |
154 { | |
155 fprintf(stderr, "Cannot open file %s:", files[i]); | |
156 perror(""); | |
157 exit(1); | |
158 } | |
159 fread(buf, 1, 6, f2); | |
160 if (mergeflag && !memcmp("LWAR1V", buf, 6)) | |
161 { | |
162 // add archive contents... | |
163 for (;;) | |
164 { | |
165 c = fgetc(f2); | |
166 if (c == EOF || ferror(f2)) | |
167 { | |
168 perror("Reading input archive file"); | |
169 exit(1); | |
170 } | |
171 if (c == EOF) | |
172 break; | |
173 | |
174 if (!c) | |
175 { | |
176 break; | |
177 } | |
178 | |
179 // find the end of the file name | |
180 while (c) | |
181 { | |
182 fputc(c, nf); | |
183 c = fgetc(f2); | |
184 if (c == EOF || ferror(f)) | |
185 { | |
186 fprintf(stderr, "Bad input archive file\n"); | |
187 exit(1); | |
188 } | |
189 } | |
190 fputc(0, nf); | |
191 | |
192 // get length of archive member | |
193 l = 0; | |
194 c = fgetc(f2); | |
195 fputc(c, nf); | |
196 l = c << 24; | |
197 c = fgetc(f2); | |
198 fputc(c, nf); | |
199 l |= c << 16; | |
200 c = fgetc(f2); | |
201 fputc(c, nf); | |
202 l |= c << 8; | |
203 c = fgetc(f2); | |
204 fputc(c, nf); | |
205 l |= c; | |
206 | |
207 while (l) | |
208 { | |
209 c = fgetc(f2); | |
210 fputc(c, nf); | |
211 l--; | |
212 } | |
213 } | |
214 | |
215 fclose(f2); | |
216 continue; | |
217 } | |
218 fseek(f2, 0, SEEK_END); | |
219 l = ftell(f2); | |
220 fseek(f2, 0, SEEK_SET); | |
221 fputs(files[i], nf); | |
222 fputc(0, nf); | |
223 fputc(l >> 24, nf); | |
224 fputc((l >> 16) & 0xff, nf); | |
225 fputc((l >> 8) & 0xff, nf); | |
226 fputc(l & 0xff, nf); | |
227 while (l) | |
228 { | |
229 c = fgetc(f2); | |
230 fputc(c, nf); | |
231 l--; | |
232 } | |
233 } | |
234 | |
235 // flag end of file | |
236 fputc(0, nf); | |
237 | |
238 fclose(nf); | |
239 | |
240 if (rename(fnbuf, archive_file) < 0) | |
241 { | |
242 perror("Cannot replace old archive file"); | |
243 unlink(fnbuf); | |
244 } | |
245 } |