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