source: trunk/src/depend-gen.c@ 442

Last change on this file since 442 was 383, checked in by katerina, 13 years ago

Fix for ticket #281 (warnings from clang static analyzer).

File size: 8.6 KB
Line 
1#include <stdio.h>
2#include <string.h>
3
4/* copyright (c) 2002 Rainer Wichmann
5 * License: GNU Public License (GPL) version 2 or later
6 */
7
8/* gcc -O2 -Wall -o depend depend.c
9 */
10
11/*
12
13# redo if sources change
14#
15depend.dep: depend.c $(SOURCES)
16 $(CC) -o depend depend.c
17 for ff in $(SOURCES); do; \
18 depend -o depend.dep $ff; \
19 done
20 nsum=`sum depend.dep`; \
21 osum=`cat depend.sum`; \
22 if test "x$$osum" != "x$$nsum"; then \
23 echo $$nsum > depend.sum
24 fi
25
26# only updated if depencies change
27#
28depend.sum: depend.dep
29
30Makefile.in: depend.sum
31 for ff in $(SOURCES); do; \
32 depend -o Makefile.in $ff; \
33 done
34
35Makefile: Makefile.in
36
37*/
38
39unsigned int lzo_adler32(unsigned int adler,
40 const char *buf, unsigned int len);
41
42
43int main (int argc, char * argv[])
44{
45 FILE * fout = NULL;
46 FILE * ftmp = NULL;
47 FILE * fin = NULL;
48
49 int filep = 1;
50
51 char name[1024];
52 char base[1024];
53 char tmpname[1024];
54 char line[1024];
55 char buffer[2048];
56 char incdir[1024];
57 int inclen = 0;
58 int count = 2047;
59 int len = 0;
60
61 unsigned int adler;
62
63 char * p;
64 char * q;
65
66 incdir[0] = '\0';
67
68 if (argc < 2)
69 {
70 fprintf(stderr, "depend-gen: Missing argument\n");
71 return 1;
72 }
73
74 if (argv[1][0] == '-' && argv[1][1] == 'h')
75 {
76 printf("Usage: depend-gen [-i includedir] -(o|t) outfile infile\n");
77 printf(" -o replaces, -t truncates\n");
78 return 0;
79 }
80
81 if (argv[1][0] == '-' && argv[1][1] == 'i')
82 {
83 if (argc < 3)
84 {
85 fprintf(stderr, "depend-gen: -i: Missing argument (includedir)\n");
86 return 1;
87 }
88 strncpy(incdir, argv[2], 1023);
89 incdir[1023] = '\0';
90 inclen = strlen(incdir);
91 argc -= 2; ++argv; ++argv;
92 }
93
94 if (argv[1][0] == '-' &&
95 (argv[1][1] == 'o' || argv[1][1] == 't' || argv[1][1] == 'c'))
96 {
97 if (argc < 3)
98 {
99 fprintf(stderr, "depend-gen: -%c: Missing argument\n", argv[1][1]);
100 return 1;
101 }
102 if (argv[1][1] == 'o' || argv[1][1] == 'c')
103 fout = fopen(argv[2], "r+");
104 else
105 fout = fopen(argv[2], "w+");
106
107 if (!fout)
108 {
109 perror("depend-gen: fopen");
110 fprintf(stderr, "depend-gen [%d]: -%c %s: Could not open file\n",
111 __LINE__, argv[1][1], argv[2]);
112 return 1;
113 }
114 filep += 2;
115
116 if (argv[1][1] == 'c')
117 {
118 adler = lzo_adler32(0, NULL, 0);
119 while (NULL != fgets(line, 1023, fout))
120 {
121 adler = lzo_adler32(adler, line, strlen(line));
122 }
123 printf("%u\n", adler);
124 return 0;
125 }
126
127 if (argv[1][1] == 't')
128 ftmp = fout;
129 else
130 {
131 tmpname[0] = '\0';
132 if (strlen(argv[filep]) > 1029)
133 {
134 fprintf(stderr, "depend-gen: -%c %s: filename too long\n",
135 argv[1][1], argv[2]);
136 return 1;
137 }
138
139
140 strncat(tmpname, argv[filep], 1029);
141 strncat(tmpname, ".tmp", 1023);
142 ftmp = fopen(tmpname, "w");
143 if (!ftmp)
144 {
145 perror("depend-gen: fopen");
146 fprintf(stderr, "depend-gen [%d]: -%c %s: Could not open file\n",
147 __LINE__, argv[1][1], tmpname);
148 return 1;
149 }
150 }
151
152 }
153 else
154 {
155 fprintf(stderr, "depend-gen: no output file given (-(o|t) outfile)\n");
156 return 1;
157 }
158
159
160 if (argc < (filep+1))
161 {
162 fprintf(stderr, "depend-gen: missing argument (infile)\n");
163 return 1;
164 }
165 fin = fopen(argv[filep], "r");
166 if (!fin)
167 {
168 perror("depend-gen: fopen");
169 fprintf(stderr, "depend-gen [%d]: -%c %s: Could not open file\n",
170 __LINE__, argv[1][1], argv[filep]);
171 return 1;
172 }
173
174 /* fast forward to dependencies start
175 */
176 do
177 {
178 if (NULL == fgets(line, 1023, fout))
179 break;
180 if (0 == strncmp(line, "# DO NOT DELETE THIS LINE", 25))
181 break;
182 fprintf(ftmp, "%s", line);
183 }
184 while (1 == 1);
185
186 if (argv[1][1] == 'o')
187 {
188 fprintf(ftmp, "# DO NOT DELETE THIS LINE\n");
189 }
190
191 strncpy(name, argv[filep], 1023);
192 p = name;
193 while (*p != '\0') ++p;
194 if (name[0] != '\0') --p;
195 if (*p == 'c') *p = 'o';
196
197 p = strrchr(name, '/');
198 if (p)
199 {
200 ++p;
201 len = strlen(p);
202 }
203
204 /* skip other dependencies
205 */
206 do
207 {
208 if (NULL == fgets(line, 1023, fout))
209 break;
210 if (p && 0 == strncmp(line, p, len))
211 break;
212 fprintf(ftmp, "%s", line);
213 }
214 while (1 == 1);
215
216 buffer[0] = '\0';
217
218 while (NULL != fgets(line, 1023, fin))
219 {
220 p = line;
221 while (*p != '\0' && (*p == ' ' || *p == '\t'))
222 ++p;
223 if (0 == strncmp(p, "#include", 8))
224 p += 8;
225 else
226 continue;
227 while (*p != '\0' && (*p == ' ' || *p == '\t'))
228 ++p;
229 if (*p != '"')
230 continue;
231 else
232 {
233 ++p;
234 q = p;
235 ++q;
236 while (*q != '\0' && *q != '"')
237 ++q;
238 if (*q != '"')
239 continue;
240 *q = '\0';
241
242 /**************************************************
243 *
244 * EXCEPTIONS
245 *
246 **************************************************/
247 if (0 == strcmp(p, "sh_gpg_chksum.h") ||
248 0 == strcmp(p, "sh_gpg_fp.h"))
249 {
250 /* fprintf(stderr, "Excluding %s\n", p); */
251 continue;
252 }
253
254 len = strlen(p);
255 if (len > count)
256 {
257 /* graceful failure */
258 fprintf(fout, "# OVERFLOW: incomplete dependencies\n");
259 break;
260 }
261 if (incdir[0] != '\0')
262 {
263 if (0 == strcmp(p, "config.h") ||
264 0 == strcmp(p, "config_xor.h") ||
265 0 == strcmp(p, "internal.h") ||
266 0 == strcmp(p, "sh_ks.h") ||
267 0 == strcmp(p, "sh_ks_xor.h") ||
268 0 == strcmp(p, "sh_MK.h")); /* do nothing */
269 else
270 {
271 strncat(buffer, incdir, count);
272 count -= inclen;
273 }
274 }
275 strncat(buffer, p, count);
276 count -= len;
277 strncat(buffer, " ", count);
278 --count;
279 }
280 }
281
282 /* write the dependencies found
283 */
284 p = strrchr(argv[filep], '/');
285 if (p)
286 {
287 ++p;
288 strncpy(name, p, 1023);
289 }
290 else
291 strncpy(name, argv[filep], 1023);
292 name[1023] = '\0';
293
294 strcpy(base, "$(srcsrc)/");
295 strcat(base, name);
296
297 p = name;
298 while (*p != '\0') ++p;
299 if (name[0] != '\0') --p;
300 if (*p == 'c')
301 {
302 *p = 'o';
303 fprintf(ftmp, "%s: %s Makefile %s\n", name, base /* argv[filep] */,
304 buffer);
305 }
306 else
307 {
308 fprintf(ftmp, "%s: Makefile %s\n", argv[filep], buffer);
309 }
310
311 /* more dependencies
312 */
313 do
314 {
315 if (NULL == fgets(line, 1023, fout))
316 break;
317 fprintf(ftmp, "%s", line);
318 }
319 while (1 == 1);
320
321 if (ftmp != NULL)
322 {
323 fclose(ftmp);
324 }
325 if (fout != NULL)
326 {
327 fclose(fout);
328 }
329 if (fin != NULL)
330 {
331 fclose(fin);
332 }
333
334 if (argv[1][1] == 'o')
335 {
336 if (0 != rename (tmpname, argv[2]))
337 {
338 perror("depend-gen: rename");
339 fprintf(stderr, "depend-gen: could not rename %s --> %s\n",
340 tmpname, argv[2]);
341 return 1;
342 }
343 }
344
345 return 0;
346}
347
348/* from the LZO real-time data compression library
349
350 Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
351 Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
352 Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
353 Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
354
355 The LZO library is free software; you can redistribute it and/or
356 modify it under the terms of the GNU General Public License as
357 published by the Free Software Foundation; either version 2 of
358 the License, or (at your option) any later version.
359
360 The LZO library is distributed in the hope that it will be useful,
361 but WITHOUT ANY WARRANTY; without even the implied warranty of
362 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
363 GNU General Public License for more details.
364
365 You should have received a copy of the GNU General Public License
366 along with the LZO library; see the file COPYING.
367 If not, write to the Free Software Foundation, Inc.,
368 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
369
370 Markus F.X.J. Oberhumer
371 <markus.oberhumer@jk.uni-linz.ac.at>
372 http://wildsau.idv.uni-linz.ac.at/mfx/lzo.html
373*/
374/*
375 * NOTE:
376 * the full LZO package can be found at
377 * http://wildsau.idv.uni-linz.ac.at/mfx/lzo.html
378 */
379
380#define LZO_BASE 65521u
381#define LZO_NMAX 5552
382
383#define LZO_DO1(buf,i) {s1 += buf[i]; s2 += s1;}
384#define LZO_DO2(buf,i) LZO_DO1(buf,i); LZO_DO1(buf,i+1);
385#define LZO_DO4(buf,i) LZO_DO2(buf,i); LZO_DO2(buf,i+2);
386#define LZO_DO8(buf,i) LZO_DO4(buf,i); LZO_DO4(buf,i+4);
387#define LZO_DO16(buf,i) LZO_DO8(buf,i); LZO_DO8(buf,i+8);
388
389unsigned int lzo_adler32(unsigned int adler, const char *buf, unsigned int len)
390{
391 unsigned int s1 = adler & 0xffff;
392 unsigned int s2 = (adler >> 16) & 0xffff;
393 int k;
394
395 if (buf == NULL)
396 return 1;
397
398 while (len > 0)
399 {
400 k = len < LZO_NMAX ? (int) len : LZO_NMAX;
401 len -= k;
402 if (k >= 16) do
403 {
404 LZO_DO16(buf,0);
405 buf += 16;
406 k -= 16;
407 } while (k >= 16);
408 if (k != 0) do
409 {
410 s1 += *buf++;
411 s2 += s1;
412 } while (--k > 0);
413 s1 %= LZO_BASE;
414 s2 %= LZO_BASE;
415 }
416 return (s2 << 16) | s1;
417}
Note: See TracBrowser for help on using the repository browser.