annotate lwar/add.c @ 265:68fbca173508 2.6

Added generated files for release
author lost
date Tue, 22 Dec 2009 05:31:23 +0000
parents bae1e3ecdce1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
172
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
1 /*
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
2 add.c
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
3 Copyright © 2009 William Astle
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
4
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
5 This file is part of LWAR.
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
6
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
7 LWAR is free software: you can redistribute it and/or modify it under the
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
10 version.
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
11
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
15 more details.
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
16
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
19
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
20
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
21 Implements the program startup code
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
22
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
23 */
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
24
212
bae1e3ecdce1 More preparation for gnulib integration
lost
parents: 174
diff changeset
25 #include <config.h>
172
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
26
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
27 #include <errno.h>
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
28 #include <stdio.h>
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
29 #include <stdlib.h>
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
30
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
31 #include "lwar.h"
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
32
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
33 void do_add(void)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
34 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
35 FILE *f;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
36 unsigned char buf[8];
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
37 long l;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
38 int c;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
39 FILE *f2;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
40 int i;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
41
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
42 f = fopen(archive_file, "r+");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
43 if (!f)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
44 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
45 if (errno == ENOENT)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
46 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
47 f = fopen(archive_file, "w");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
48 if (f)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
49 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
50 fputs("LWAR1V", f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
51 goto doadd;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
52 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
53 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
54 perror("Cannot open archive file");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
55 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
56
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
57 fread(buf, 1, 6, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
58 if (memcmp("LWAR1V", buf, 6))
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
59 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
60 fprintf(stderr, "%s is not a valid archive file.\n", archive_file);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
61 exit(1);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
62 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
63
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
64 for (;;)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
65 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
66 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
67 if (c == EOF && ferror(f))
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
68 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
69 perror("Reading archive file");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
70 exit(1);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
71 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
72 if (c == EOF)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
73 goto doadd;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
74
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
75 if (!c)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
76 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
77 fseek(f, -1, SEEK_CUR);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
78 goto doadd;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
79 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
80
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
81 // find the end of the file name
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
82 while (c)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
83 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
84 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
85 if (c == EOF || ferror(f))
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
86 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
87 fprintf(stderr, "Bad archive file\n");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
88 exit(1);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
89 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
90 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
91
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
92 // get length of archive member
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
93 l = 0;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
94 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
95 l = c << 24;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
96 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
97 l |= c << 16;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
98 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
99 l |= c << 8;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
100 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
101 l |= c;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
102
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
103 fseek(f, l, SEEK_CUR);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
104 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
105 // back up to the NUL byte at the end of the file
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
106 fseek(f, -1, SEEK_CUR);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
107 doadd:
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
108 for (i = 0; i < nfiles; i++)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
109 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
110 f2 = fopen(files[i], "r");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
111 if (!f2)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
112 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
113 fprintf(stderr, "Cannot open file %s:", files[i]);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
114 perror("");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
115 exit(1);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
116 }
174
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
117 fread(buf, 1, 6, f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
118 if (mergeflag && !memcmp("LWAR1V", buf, 6))
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
119 {
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
120 // add archive contents...
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
121 for (;;)
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
122 {
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
123 c = fgetc(f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
124 if (c == EOF || ferror(f2))
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
125 {
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
126 perror("Reading input archive file");
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
127 exit(1);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
128 }
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
129 if (c == EOF)
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
130 break;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
131
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
132 if (!c)
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
133 {
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
134 break;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
135 }
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
136
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
137 // find the end of the file name
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
138 while (c)
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
139 {
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
140 fputc(c, f);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
141 c = fgetc(f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
142 if (c == EOF || ferror(f))
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
143 {
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
144 fprintf(stderr, "Bad input archive file\n");
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
145 exit(1);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
146 }
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
147 }
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
148 fputc(0, f);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
149
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
150 // get length of archive member
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
151 l = 0;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
152 c = fgetc(f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
153 fputc(c, f);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
154 l = c << 24;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
155 c = fgetc(f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
156 fputc(c, f);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
157 l |= c << 16;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
158 c = fgetc(f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
159 fputc(c, f);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
160 l |= c << 8;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
161 c = fgetc(f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
162 fputc(c, f);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
163 l |= c;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
164
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
165 while (l)
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
166 {
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
167 c = fgetc(f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
168 fputc(c, f);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
169 l--;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
170 }
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
171 }
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
172
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
173 fclose(f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
174 continue;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
175 }
172
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
176 fseek(f2, 0, SEEK_END);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
177 l = ftell(f2);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
178 fseek(f2, 0, SEEK_SET);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
179 fputs(files[i], f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
180 fputc(0, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
181 fputc(l >> 24, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
182 fputc((l >> 16) & 0xff, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
183 fputc((l >> 8) & 0xff, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
184 fputc(l & 0xff, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
185 while (l)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
186 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
187 c = fgetc(f2);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
188 fputc(c, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
189 l--;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
190 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
191 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
192
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
193 // flag end of file
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
194 fputc(0, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
195 }