source: branches/samhain-2_2-branch/src/depend-gen.c@ 555

Last change on this file since 555 was 1, checked in by katerina, 19 years ago

Initial import

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 ++p;
200 len = strlen(p);
201
202 /* skip other dependencies
203 */
204 do
205 {
206 if (NULL == fgets(line, 1023, fout))
207 break;
208 if (0 == strncmp(line, p, len))
209 break;
210 fprintf(ftmp, "%s", line);
211 }
212 while (1 == 1);
213
214 buffer[0] = '\0';
215
216 while (NULL != fgets(line, 1023, fin))
217 {
218 p = line;
219 while (*p != '\0' && (*p == ' ' || *p == '\t'))
220 ++p;
221 if (0 == strncmp(p, "#include", 8))
222 p += 8;
223 else
224 continue;
225 while (*p != '\0' && (*p == ' ' || *p == '\t'))
226 ++p;
227 if (*p != '"')
228 continue;
229 else
230 {
231 ++p;
232 q = p;
233 ++q;
234 while (*q != '\0' && *q != '"')
235 ++q;
236 if (*q != '"')
237 continue;
238 *q = '\0';
239
240 /**************************************************
241 *
242 * EXCEPTIONS
243 *
244 **************************************************/
245 if (0 == strcmp(p, "sh_gpg_chksum.h") ||
246 0 == strcmp(p, "sh_gpg_fp.h"))
247 {
248 /* fprintf(stderr, "Excluding %s\n", p); */
249 continue;
250 }
251
252 len = strlen(p);
253 if (len > count)
254 {
255 /* graceful failure */
256 fprintf(fout, "# OVERFLOW: incomplete dependencies\n");
257 break;
258 }
259 if (incdir[0] != '\0')
260 {
261 if (0 == strcmp(p, "config.h") ||
262 0 == strcmp(p, "config_xor.h") ||
263 0 == strcmp(p, "internal.h") ||
264 0 == strcmp(p, "sh_ks.h") ||
265 0 == strcmp(p, "sh_ks_xor.h") ||
266 0 == strcmp(p, "sh_MK.h")); /* do nothing */
267 else
268 {
269 strncat(buffer, incdir, count);
270 count -= inclen;
271 }
272 }
273 strncat(buffer, p, count);
274 count -= len;
275 strncat(buffer, " ", count);
276 --count;
277 }
278 }
279
280 /* write the dependencies found
281 */
282 p = strrchr(argv[filep], '/');
283 if (p)
284 {
285 ++p;
286 strncpy(name, p, 1023);
287 }
288 else
289 strncpy(name, argv[filep], 1023);
290 name[1023] = '\0';
291
292 strcpy(base, "$(srcsrc)/");
293 strcat(base, name);
294
295 p = name;
296 while (*p != '\0') ++p;
297 if (name[0] != '\0') --p;
298 if (*p == 'c')
299 {
300 *p = 'o';
301 fprintf(ftmp, "%s: %s Makefile %s\n", name, base /* argv[filep] */,
302 buffer);
303 }
304 else
305 {
306 fprintf(ftmp, "%s: Makefile %s\n", argv[filep], buffer);
307 }
308
309 /* more dependencies
310 */
311 do
312 {
313 if (NULL == fgets(line, 1023, fout))
314 break;
315 fprintf(ftmp, "%s", line);
316 }
317 while (1 == 1);
318
319 if (ftmp != NULL)
320 {
321 fclose(ftmp);
322 }
323 if (fout != NULL)
324 {
325 fclose(fout);
326 }
327 if (fin != NULL)
328 {
329 fclose(fin);
330 }
331
332 if (argv[1][1] == 'o')
333 {
334 if (0 != rename (tmpname, argv[2]))
335 {
336 perror("depend-gen: rename");
337 fprintf(stderr, "depend-gen: could not rename %s --> %s\n",
338 tmpname, argv[2]);
339 return 1;
340 }
341 }
342
343 return 0;
344}
345
346/* from the LZO real-time data compression library
347
348 Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
349 Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
350 Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
351 Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
352
353 The LZO library is free software; you can redistribute it and/or
354 modify it under the terms of the GNU General Public License as
355 published by the Free Software Foundation; either version 2 of
356 the License, or (at your option) any later version.
357
358 The LZO library is distributed in the hope that it will be useful,
359 but WITHOUT ANY WARRANTY; without even the implied warranty of
360 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
361 GNU General Public License for more details.
362
363 You should have received a copy of the GNU General Public License
364 along with the LZO library; see the file COPYING.
365 If not, write to the Free Software Foundation, Inc.,
366 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
367
368 Markus F.X.J. Oberhumer
369 <markus.oberhumer@jk.uni-linz.ac.at>
370 http://wildsau.idv.uni-linz.ac.at/mfx/lzo.html
371*/
372/*
373 * NOTE:
374 * the full LZO package can be found at
375 * http://wildsau.idv.uni-linz.ac.at/mfx/lzo.html
376 */
377
378#define LZO_BASE 65521u
379#define LZO_NMAX 5552
380
381#define LZO_DO1(buf,i) {s1 += buf[i]; s2 += s1;}
382#define LZO_DO2(buf,i) LZO_DO1(buf,i); LZO_DO1(buf,i+1);
383#define LZO_DO4(buf,i) LZO_DO2(buf,i); LZO_DO2(buf,i+2);
384#define LZO_DO8(buf,i) LZO_DO4(buf,i); LZO_DO4(buf,i+4);
385#define LZO_DO16(buf,i) LZO_DO8(buf,i); LZO_DO8(buf,i+8);
386
387unsigned int lzo_adler32(unsigned int adler, const char *buf, unsigned int len)
388{
389 unsigned int s1 = adler & 0xffff;
390 unsigned int s2 = (adler >> 16) & 0xffff;
391 int k;
392
393 if (buf == NULL)
394 return 1;
395
396 while (len > 0)
397 {
398 k = len < LZO_NMAX ? (int) len : LZO_NMAX;
399 len -= k;
400 if (k >= 16) do
401 {
402 LZO_DO16(buf,0);
403 buf += 16;
404 k -= 16;
405 } while (k >= 16);
406 if (k != 0) do
407 {
408 s1 += *buf++;
409 s2 += s1;
410 } while (--k > 0);
411 s1 %= LZO_BASE;
412 s2 %= LZO_BASE;
413 }
414 return (s2 << 16) | s1;
415}
Note: See TracBrowser for help on using the repository browser.