Mercurial > hg > index.cgi
comparison lwar/add.c @ 0:2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
author | lost@l-w.ca |
---|---|
date | Wed, 19 Jan 2011 22:27:17 -0700 |
parents | |
children | 7317fbe024af |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:2c24602be78f |
---|---|
1 /* | |
2 add.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 Implements the program startup code | |
22 | |
23 */ | |
24 | |
25 #include <errno.h> | |
26 #include <stdio.h> | |
27 #include <stdlib.h> | |
28 | |
29 #include "lwar.h" | |
30 | |
31 void do_add(void) | |
32 { | |
33 FILE *f; | |
34 unsigned char buf[8]; | |
35 long l; | |
36 int c; | |
37 FILE *f2; | |
38 int i; | |
39 | |
40 f = fopen(archive_file, "r+"); | |
41 if (!f) | |
42 { | |
43 if (errno == ENOENT) | |
44 { | |
45 f = fopen(archive_file, "w"); | |
46 if (f) | |
47 { | |
48 fputs("LWAR1V", f); | |
49 goto doadd; | |
50 } | |
51 } | |
52 perror("Cannot open archive file"); | |
53 } | |
54 | |
55 fread(buf, 1, 6, f); | |
56 if (memcmp("LWAR1V", buf, 6)) | |
57 { | |
58 fprintf(stderr, "%s is not a valid archive file.\n", archive_file); | |
59 exit(1); | |
60 } | |
61 | |
62 for (;;) | |
63 { | |
64 c = fgetc(f); | |
65 if (c == EOF && ferror(f)) | |
66 { | |
67 perror("Reading archive file"); | |
68 exit(1); | |
69 } | |
70 if (c == EOF) | |
71 goto doadd; | |
72 | |
73 if (!c) | |
74 { | |
75 fseek(f, -1, SEEK_CUR); | |
76 goto doadd; | |
77 } | |
78 | |
79 // find the end of the file name | |
80 while (c) | |
81 { | |
82 c = fgetc(f); | |
83 if (c == EOF || ferror(f)) | |
84 { | |
85 fprintf(stderr, "Bad archive file\n"); | |
86 exit(1); | |
87 } | |
88 } | |
89 | |
90 // get length of archive member | |
91 l = 0; | |
92 c = fgetc(f); | |
93 l = c << 24; | |
94 c = fgetc(f); | |
95 l |= c << 16; | |
96 c = fgetc(f); | |
97 l |= c << 8; | |
98 c = fgetc(f); | |
99 l |= c; | |
100 | |
101 fseek(f, l, SEEK_CUR); | |
102 } | |
103 // back up to the NUL byte at the end of the file | |
104 fseek(f, -1, SEEK_CUR); | |
105 doadd: | |
106 for (i = 0; i < nfiles; i++) | |
107 { | |
108 f2 = fopen(files[i], "r"); | |
109 if (!f2) | |
110 { | |
111 fprintf(stderr, "Cannot open file %s:", files[i]); | |
112 perror(""); | |
113 exit(1); | |
114 } | |
115 fread(buf, 1, 6, f2); | |
116 if (mergeflag && !memcmp("LWAR1V", buf, 6)) | |
117 { | |
118 // add archive contents... | |
119 for (;;) | |
120 { | |
121 c = fgetc(f2); | |
122 if (c == EOF || ferror(f2)) | |
123 { | |
124 perror("Reading input archive file"); | |
125 exit(1); | |
126 } | |
127 if (c == EOF) | |
128 break; | |
129 | |
130 if (!c) | |
131 { | |
132 break; | |
133 } | |
134 | |
135 // find the end of the file name | |
136 while (c) | |
137 { | |
138 fputc(c, f); | |
139 c = fgetc(f2); | |
140 if (c == EOF || ferror(f)) | |
141 { | |
142 fprintf(stderr, "Bad input archive file\n"); | |
143 exit(1); | |
144 } | |
145 } | |
146 fputc(0, f); | |
147 | |
148 // get length of archive member | |
149 l = 0; | |
150 c = fgetc(f2); | |
151 fputc(c, f); | |
152 l = c << 24; | |
153 c = fgetc(f2); | |
154 fputc(c, f); | |
155 l |= c << 16; | |
156 c = fgetc(f2); | |
157 fputc(c, f); | |
158 l |= c << 8; | |
159 c = fgetc(f2); | |
160 fputc(c, f); | |
161 l |= c; | |
162 | |
163 while (l) | |
164 { | |
165 c = fgetc(f2); | |
166 fputc(c, f); | |
167 l--; | |
168 } | |
169 } | |
170 | |
171 fclose(f2); | |
172 continue; | |
173 } | |
174 fseek(f2, 0, SEEK_END); | |
175 l = ftell(f2); | |
176 fseek(f2, 0, SEEK_SET); | |
177 fputs(files[i], f); | |
178 fputc(0, f); | |
179 fputc(l >> 24, f); | |
180 fputc((l >> 16) & 0xff, f); | |
181 fputc((l >> 8) & 0xff, f); | |
182 fputc(l & 0xff, f); | |
183 while (l) | |
184 { | |
185 c = fgetc(f2); | |
186 fputc(c, f); | |
187 l--; | |
188 } | |
189 } | |
190 | |
191 // flag end of file | |
192 fputc(0, f); | |
193 } |