annotate lwar/add.c @ 425:67815606c5d9 3.0-beta2

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