annotate lwar/add.c @ 203:2c1afbdb2de0

Added |, &, and ^ binary bitwise operators
author lost
date Sun, 22 Mar 2009 22:11:12 +0000
parents cc41ccee8f64
children bae1e3ecdce1
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
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
25 #ifdef HAVE_CONFIG_H
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
26 #include "config.h"
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
27 #endif
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
28
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
29 #include <errno.h>
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
30 #include <stdio.h>
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
31 #include <stdlib.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 #include "lwar.h"
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 void do_add(void)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
36 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
37 FILE *f;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
38 unsigned char buf[8];
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
39 long l;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
40 int c;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
41 FILE *f2;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
42 int i;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
43
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
44 f = fopen(archive_file, "r+");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
45 if (!f)
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 if (errno == ENOENT)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
48 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
49 f = fopen(archive_file, "w");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
50 if (f)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
51 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
52 fputs("LWAR1V", f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
53 goto doadd;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
54 }
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 perror("Cannot open archive file");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
57 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
58
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
59 fread(buf, 1, 6, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
60 if (memcmp("LWAR1V", buf, 6))
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
61 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
62 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
63 exit(1);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
64 }
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 for (;;)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
67 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
68 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
69 if (c == EOF && ferror(f))
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
70 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
71 perror("Reading archive file");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
72 exit(1);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
73 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
74 if (c == EOF)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
75 goto doadd;
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 if (!c)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
78 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
79 fseek(f, -1, SEEK_CUR);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
80 goto doadd;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
81 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
82
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
83 // find the end of the file name
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
84 while (c)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
85 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
86 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
87 if (c == EOF || ferror(f))
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
88 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
89 fprintf(stderr, "Bad archive file\n");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
90 exit(1);
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 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
93
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
94 // get length of archive member
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
95 l = 0;
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 << 24;
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 << 16;
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 << 8;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
102 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
103 l |= c;
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 fseek(f, l, SEEK_CUR);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
106 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
107 // 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
108 fseek(f, -1, SEEK_CUR);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
109 doadd:
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
110 for (i = 0; i < nfiles; i++)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
111 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
112 f2 = fopen(files[i], "r");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
113 if (!f2)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
114 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
115 fprintf(stderr, "Cannot open file %s:", files[i]);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
116 perror("");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
117 exit(1);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
118 }
174
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
119 fread(buf, 1, 6, f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
120 if (mergeflag && !memcmp("LWAR1V", buf, 6))
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
121 {
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
122 // add archive contents...
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
123 for (;;)
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
124 {
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
125 c = fgetc(f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
126 if (c == EOF || ferror(f2))
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
127 {
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
128 perror("Reading input archive file");
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
129 exit(1);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
130 }
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
131 if (c == EOF)
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
132 break;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
133
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
134 if (!c)
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
135 {
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
136 break;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
137 }
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
138
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
139 // find the end of the file name
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
140 while (c)
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
141 {
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
142 fputc(c, f);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
143 c = fgetc(f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
144 if (c == EOF || ferror(f))
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
145 {
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
146 fprintf(stderr, "Bad input archive file\n");
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
147 exit(1);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
148 }
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
149 }
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
150 fputc(0, f);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
151
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
152 // get length of archive member
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
153 l = 0;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
154 c = fgetc(f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
155 fputc(c, f);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
156 l = c << 24;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
157 c = fgetc(f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
158 fputc(c, f);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
159 l |= c << 16;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
160 c = fgetc(f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
161 fputc(c, f);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
162 l |= c << 8;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
163 c = fgetc(f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
164 fputc(c, f);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
165 l |= c;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
166
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
167 while (l)
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
168 {
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
169 c = fgetc(f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
170 fputc(c, f);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
171 l--;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
172 }
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
173 }
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
174
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
175 fclose(f2);
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
176 continue;
cc41ccee8f64 added --merge to lwar
lost
parents: 172
diff changeset
177 }
172
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
178 fseek(f2, 0, SEEK_END);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
179 l = ftell(f2);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
180 fseek(f2, 0, SEEK_SET);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
181 fputs(files[i], f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
182 fputc(0, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
183 fputc(l >> 24, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
184 fputc((l >> 16) & 0xff, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
185 fputc((l >> 8) & 0xff, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
186 fputc(l & 0xff, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
187 while (l)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
188 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
189 c = fgetc(f2);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
190 fputc(c, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
191 l--;
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 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
194
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
195 // flag end of file
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
196 fputc(0, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
197 }