1 | /* |
---|
2 | * zzat - various cat reimplementations for testing purposes |
---|
3 | * Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * This program is free software. It comes without any warranty, to |
---|
7 | * the extent permitted by applicable law. You can redistribute it |
---|
8 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
9 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
10 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
11 | */ |
---|
12 | |
---|
13 | /* |
---|
14 | * TODO: fsetpos64, fgetln |
---|
15 | */ |
---|
16 | |
---|
17 | #include "config.h" |
---|
18 | |
---|
19 | /* Needed for lseek64() */ |
---|
20 | #define _LARGEFILE64_SOURCE |
---|
21 | /* Needed for O_RDONLY on HP-UX */ |
---|
22 | #define _INCLUDE_POSIX_SOURCE |
---|
23 | /* Needed for fgets_unlocked() */ |
---|
24 | #define _GNU_SOURCE |
---|
25 | /* Needed for getc_unlocked() on OpenSolaris */ |
---|
26 | #define __EXTENSIONS__ |
---|
27 | |
---|
28 | #if defined HAVE_STDINT_H |
---|
29 | # include <stdint.h> |
---|
30 | #elif defined HAVE_INTTYPES_H |
---|
31 | # include <inttypes.h> |
---|
32 | #endif |
---|
33 | #include <sys/types.h> |
---|
34 | #include <sys/stat.h> |
---|
35 | #include <fcntl.h> |
---|
36 | #if defined HAVE_UNISTD_H |
---|
37 | # include <unistd.h> |
---|
38 | #endif |
---|
39 | #if defined HAVE_SYS_MMAN_H |
---|
40 | # include <sys/mman.h> |
---|
41 | #endif |
---|
42 | #include <stdlib.h> |
---|
43 | #include <stdio.h> |
---|
44 | #include <string.h> |
---|
45 | |
---|
46 | #if !defined HAVE_GETOPT_LONG |
---|
47 | # include "mygetopt.h" |
---|
48 | #elif defined HAVE_GETOPT_H |
---|
49 | # include <getopt.h> |
---|
50 | #endif |
---|
51 | |
---|
52 | #if defined HAVE_GETOPT_LONG |
---|
53 | # define mygetopt getopt_long |
---|
54 | # define myoptind optind |
---|
55 | # define myoptarg optarg |
---|
56 | # define myoption option |
---|
57 | #endif |
---|
58 | |
---|
59 | static int run(char const *sequence, char const *file); |
---|
60 | static void output(char const *buf, size_t len); |
---|
61 | |
---|
62 | static void syntax(void); |
---|
63 | static void version(void); |
---|
64 | static void usage(void); |
---|
65 | |
---|
66 | /* Global parameters */ |
---|
67 | static int debug = 0; |
---|
68 | static int repeat = 1; |
---|
69 | static char escape_tabs = 0; |
---|
70 | static char escape_ends = 0; |
---|
71 | static char escape_other = 0; |
---|
72 | static char number_lines = 0; |
---|
73 | static char number_nonblank = 0; |
---|
74 | static char squeeze_lines = 0; |
---|
75 | |
---|
76 | /* Global output state */ |
---|
77 | static int ncrs = 0; |
---|
78 | static int line = 1; |
---|
79 | static char newline = 1; |
---|
80 | |
---|
81 | /* |
---|
82 | * Main program. |
---|
83 | */ |
---|
84 | |
---|
85 | int main(int argc, char *argv[]) |
---|
86 | { |
---|
87 | char const *sequence = "repeat(-1, fread(1,32768), feof(1))"; |
---|
88 | int i; |
---|
89 | |
---|
90 | for (;;) |
---|
91 | { |
---|
92 | #define OPTSTR "+AbdeEnr:stTvx:lhV" |
---|
93 | #define MOREINFO "Try `%s --help' for more information.\n" |
---|
94 | int option_index = 0; |
---|
95 | static struct myoption long_options[] = |
---|
96 | { |
---|
97 | { "show-all", 0, NULL, 'A' }, |
---|
98 | { "number-nonblank", 0, NULL, 'b' }, |
---|
99 | { "debug", 0, NULL, 'd' }, |
---|
100 | { "show-ends", 0, NULL, 'E' }, |
---|
101 | { "number", 0, NULL, 'n' }, |
---|
102 | { "repeat", 1, NULL, 'r' }, |
---|
103 | { "squeeze-blank", 0, NULL, 's' }, |
---|
104 | { "show-tabs", 0, NULL, 'T' }, |
---|
105 | { "show-nonprinting", 0, NULL, 'v' }, |
---|
106 | { "execute", 1, NULL, 'x' }, |
---|
107 | { "list", 0, NULL, 'l' }, |
---|
108 | { "help", 0, NULL, 'h' }, |
---|
109 | { "version", 0, NULL, 'V' }, |
---|
110 | { NULL, 0, NULL, 0 } |
---|
111 | }; |
---|
112 | int c = mygetopt(argc, argv, OPTSTR, long_options, &option_index); |
---|
113 | |
---|
114 | if (c == -1) |
---|
115 | break; |
---|
116 | |
---|
117 | switch (c) |
---|
118 | { |
---|
119 | case 'A': /* --show-all */ |
---|
120 | escape_tabs = escape_ends = escape_other = 1; |
---|
121 | break; |
---|
122 | case 'b': /* --number-nonblank */ |
---|
123 | number_nonblank = 1; |
---|
124 | break; |
---|
125 | case 'd': /* --debug */ |
---|
126 | debug = 1; |
---|
127 | break; |
---|
128 | case 'e': |
---|
129 | escape_ends = escape_other = 1; |
---|
130 | break; |
---|
131 | case 'E': /* --show-ends */ |
---|
132 | escape_ends = 1; |
---|
133 | break; |
---|
134 | case 'n': /* --number */ |
---|
135 | number_lines = 1; |
---|
136 | break; |
---|
137 | case 'r': /* --repeat */ |
---|
138 | repeat = atoi(optarg); |
---|
139 | break; |
---|
140 | case 's': /* --squeeze-blank */ |
---|
141 | squeeze_lines = 1; |
---|
142 | break; |
---|
143 | case 't': |
---|
144 | escape_tabs = escape_other = 1; |
---|
145 | break; |
---|
146 | case 'T': /* --show-tabs */ |
---|
147 | escape_tabs = 1; |
---|
148 | break; |
---|
149 | case 'v': /* --show-nonprinting */ |
---|
150 | escape_tabs = 1; |
---|
151 | break; |
---|
152 | case 'x': /* --execute */ |
---|
153 | if (myoptarg[0] == '=') |
---|
154 | myoptarg++; |
---|
155 | sequence = myoptarg; |
---|
156 | break; |
---|
157 | case 'l': /* --list */ |
---|
158 | syntax(); |
---|
159 | return 0; |
---|
160 | case 'h': /* --help */ |
---|
161 | usage(); |
---|
162 | return 0; |
---|
163 | case 'V': /* --version */ |
---|
164 | version(); |
---|
165 | return 0; |
---|
166 | default: |
---|
167 | fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c); |
---|
168 | printf(MOREINFO, argv[0]); |
---|
169 | return EXIT_FAILURE; |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | if (myoptind >= argc) |
---|
174 | { |
---|
175 | fprintf(stderr, "E: zzat: too few arguments\n"); |
---|
176 | return EXIT_FAILURE; |
---|
177 | } |
---|
178 | |
---|
179 | while (repeat-- > 0) |
---|
180 | for (i = myoptind; i < argc; i++) |
---|
181 | { |
---|
182 | int ret = run(sequence, argv[i]); |
---|
183 | if (ret) |
---|
184 | return ret; |
---|
185 | } |
---|
186 | |
---|
187 | return EXIT_SUCCESS; |
---|
188 | } |
---|
189 | |
---|
190 | /* |
---|
191 | * File output method. |
---|
192 | */ |
---|
193 | |
---|
194 | static void output(char const *buf, size_t len) |
---|
195 | { |
---|
196 | size_t i; |
---|
197 | |
---|
198 | /* If no special features are requested, output directly */ |
---|
199 | if (!(escape_tabs || escape_ends || escape_other |
---|
200 | || number_lines || number_nonblank || squeeze_lines)) |
---|
201 | { |
---|
202 | fwrite(buf, len, 1, stdout); |
---|
203 | return; |
---|
204 | } |
---|
205 | |
---|
206 | /* If any special feature is active, go through every possibility */ |
---|
207 | for (i = 0; i < len; i++) |
---|
208 | { |
---|
209 | int ch = (unsigned int)(unsigned char)buf[i]; |
---|
210 | |
---|
211 | if (squeeze_lines) |
---|
212 | { |
---|
213 | if (ch == '\n') |
---|
214 | { |
---|
215 | if (++ncrs > 2) |
---|
216 | continue; |
---|
217 | } |
---|
218 | else |
---|
219 | ncrs = 0; |
---|
220 | } |
---|
221 | |
---|
222 | if (number_lines || number_nonblank) |
---|
223 | { |
---|
224 | if (newline) |
---|
225 | { |
---|
226 | newline = 0; |
---|
227 | if (!number_nonblank || ch != '\n') |
---|
228 | fprintf(stdout, "% 6i\t", line++); |
---|
229 | } |
---|
230 | |
---|
231 | if (ch == '\n') |
---|
232 | newline = 1; |
---|
233 | } |
---|
234 | |
---|
235 | if (escape_other && ch >= 0x80) |
---|
236 | { |
---|
237 | if (ch - 0x80 < 0x20 || ch - 0x80 == 0x7f) |
---|
238 | fprintf(stdout, "M-^%c", (ch - 0x80) ^ 0x40); |
---|
239 | else |
---|
240 | fprintf(stdout, "M-%c", ch - 0x80); |
---|
241 | } |
---|
242 | else if (escape_tabs && ch == '\t') |
---|
243 | fprintf(stdout, "^I"); |
---|
244 | else if (escape_ends && ch == '\n') |
---|
245 | puts("$"); |
---|
246 | else if (escape_other && (ch < 0x20 || ch == 0x7f)) |
---|
247 | fprintf(stdout, "^%c", ch ^ 0x40); |
---|
248 | else |
---|
249 | putchar(ch); |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | /* |
---|
254 | * Command intepreter |
---|
255 | */ |
---|
256 | |
---|
257 | #define MY_FOPEN(cmd) \ |
---|
258 | do { \ |
---|
259 | cmd; \ |
---|
260 | if (!f) \ |
---|
261 | { \ |
---|
262 | fprintf(stderr, "E: zzat: cannot open `%s'\n", file); \ |
---|
263 | return EXIT_FAILURE; \ |
---|
264 | } \ |
---|
265 | retoff = 0; \ |
---|
266 | sequence = strchr(sequence, ')') + 1; \ |
---|
267 | } while(0) |
---|
268 | |
---|
269 | #define MY_FCLOSE(cmd) \ |
---|
270 | do { \ |
---|
271 | cmd; \ |
---|
272 | f = NULL; \ |
---|
273 | sequence = strchr(sequence, ')') + 1; \ |
---|
274 | } while(0) |
---|
275 | |
---|
276 | #define ROUNDUP(size) (((size) + 0x1000) & ~0xfff) |
---|
277 | |
---|
278 | #define MERGE(address, cnt, off) \ |
---|
279 | do { \ |
---|
280 | size_t _cnt = cnt, _off = off; \ |
---|
281 | if (_cnt && retoff + _cnt > retlen) \ |
---|
282 | { \ |
---|
283 | retlen = retoff + _cnt; \ |
---|
284 | if (!retbuf || ROUNDUP(retlen) != ROUNDUP(retlen - _cnt)) \ |
---|
285 | { \ |
---|
286 | if (debug) \ |
---|
287 | fprintf(stderr, "D: zzat: allocating %i bytes for %i\n", \ |
---|
288 | (int)ROUNDUP(retlen), (int)retlen); \ |
---|
289 | retbuf = realloc(retbuf, ROUNDUP(retlen)); \ |
---|
290 | } \ |
---|
291 | } \ |
---|
292 | if (_cnt > 0) \ |
---|
293 | { \ |
---|
294 | if (debug) \ |
---|
295 | fprintf(stderr, "D: zzat: writing %i byte%s at offset %i\n", \ |
---|
296 | (int)_cnt, _cnt == 1 ? "" : "s", (int)retoff); \ |
---|
297 | memcpy(retbuf + retoff, address, _cnt); \ |
---|
298 | } \ |
---|
299 | retoff += _off; \ |
---|
300 | } while(0) |
---|
301 | |
---|
302 | #define MY_FREAD(cmd, buf, cnt) MY_FCALL(cmd, buf, cnt, cnt) |
---|
303 | #define MY_FSEEK(cmd, off) MY_FCALL(cmd, /* unused */ "", 0, off) |
---|
304 | |
---|
305 | #define MY_FCALL(cmd, buf, cnt, off) \ |
---|
306 | do { \ |
---|
307 | if (!f) \ |
---|
308 | { \ |
---|
309 | f = fopen(file, "r"); \ |
---|
310 | if (!f) \ |
---|
311 | { \ |
---|
312 | fprintf(stderr, "E: zzat: cannot open `%s'\n", file); \ |
---|
313 | return EXIT_FAILURE; \ |
---|
314 | } \ |
---|
315 | } \ |
---|
316 | /* fprintf(stderr, "debug: %s\n", #cmd); */ \ |
---|
317 | cmd; \ |
---|
318 | MERGE(buf, cnt, off); \ |
---|
319 | sequence = strchr(sequence, ')') + 1; \ |
---|
320 | } while(0) |
---|
321 | |
---|
322 | #define MY_FEOF() \ |
---|
323 | do { \ |
---|
324 | if (!f) \ |
---|
325 | { \ |
---|
326 | f = fopen(file, "r"); \ |
---|
327 | if (!f) \ |
---|
328 | { \ |
---|
329 | fprintf(stderr, "E: zzat: cannot open `%s'\n", file); \ |
---|
330 | return EXIT_FAILURE; \ |
---|
331 | } \ |
---|
332 | } \ |
---|
333 | if (feof(f)) \ |
---|
334 | feofs++; \ |
---|
335 | if (feofs >= l1) \ |
---|
336 | finish = 1; \ |
---|
337 | sequence = strchr(sequence, ')') + 1; \ |
---|
338 | } while(0) |
---|
339 | |
---|
340 | /* |
---|
341 | * Command parser. We rewrite fmt by replacing the last character with |
---|
342 | * '%c' and check that the sscanf() call returns the expected number of |
---|
343 | * matches plus one (for the last character). We use this macro trick to |
---|
344 | * avoid using vsscanf() which does not exist on all platforms. |
---|
345 | */ |
---|
346 | |
---|
347 | struct parser |
---|
348 | { |
---|
349 | char tmpfmt[1024], ch, lastch; |
---|
350 | }; |
---|
351 | |
---|
352 | static int make_fmt(struct parser *p, char const *fmt) |
---|
353 | { |
---|
354 | char const *tmp; |
---|
355 | size_t len; |
---|
356 | int ret = 0; |
---|
357 | |
---|
358 | len = strlen(fmt); |
---|
359 | p->lastch = fmt[len - 1]; |
---|
360 | |
---|
361 | memcpy(p->tmpfmt, fmt, len - 1); |
---|
362 | p->tmpfmt[len - 1] = '%'; |
---|
363 | p->tmpfmt[len] = 'c'; |
---|
364 | p->tmpfmt[len + 1] = '\0'; |
---|
365 | |
---|
366 | for (tmp = p->tmpfmt; *tmp; tmp++) |
---|
367 | if (*tmp == '%') |
---|
368 | tmp++, ret++; |
---|
369 | |
---|
370 | return ret; |
---|
371 | } |
---|
372 | |
---|
373 | #define PARSECMD(fmt, arg...) \ |
---|
374 | (make_fmt(&parser, fmt) == sscanf(sequence, parser.tmpfmt, \ |
---|
375 | ##arg, &parser.ch) \ |
---|
376 | && parser.ch == parser.lastch) |
---|
377 | |
---|
378 | /* |
---|
379 | * File reader. We parse a command line and perform all the operations it |
---|
380 | * contains on the specified file. |
---|
381 | */ |
---|
382 | |
---|
383 | static int run(char const *sequence, char const *file) |
---|
384 | { |
---|
385 | struct { char const *p; int count; } loops[128]; |
---|
386 | char *retbuf = NULL, *tmp; |
---|
387 | FILE *f = NULL; |
---|
388 | size_t retlen = 0, retoff = 0; |
---|
389 | int nloops = 0, fd = -1, feofs = 0, finish = 0; |
---|
390 | |
---|
391 | /* Initialise per-file state */ |
---|
392 | /* TODO */ |
---|
393 | |
---|
394 | /* Allocate 32MB for our temporary buffer. Any larger value will crash. */ |
---|
395 | tmp = malloc(32 * 1024 * 1024); |
---|
396 | |
---|
397 | while (*sequence) |
---|
398 | { |
---|
399 | struct parser parser; |
---|
400 | long int l1, l2; |
---|
401 | char *s, *lineptr = NULL; |
---|
402 | size_t k; |
---|
403 | ssize_t l; |
---|
404 | int n; |
---|
405 | char ch; |
---|
406 | |
---|
407 | (void)k; |
---|
408 | |
---|
409 | /* Ignore punctuation */ |
---|
410 | if (strchr(" \t,;\r\n", *sequence)) |
---|
411 | sequence++; |
---|
412 | |
---|
413 | /* Loop handling */ |
---|
414 | else if (PARSECMD("repeat ( %li ,", &l1)) |
---|
415 | { |
---|
416 | sequence = strchr(sequence, ',') + 1; |
---|
417 | loops[nloops].p = sequence; |
---|
418 | loops[nloops].count = l1; |
---|
419 | nloops++; |
---|
420 | } |
---|
421 | else if (PARSECMD(")")) |
---|
422 | { |
---|
423 | if (nloops == 0) |
---|
424 | { |
---|
425 | fprintf(stderr, "E: zzat: ')' outside a loop\n"); |
---|
426 | return EXIT_FAILURE; |
---|
427 | } |
---|
428 | if (loops[nloops - 1].count == 1 || finish) |
---|
429 | { |
---|
430 | nloops--; |
---|
431 | sequence = strchr(sequence, ')') + 1; |
---|
432 | } |
---|
433 | else |
---|
434 | { |
---|
435 | loops[nloops - 1].count--; |
---|
436 | sequence = loops[nloops - 1].p; |
---|
437 | } |
---|
438 | |
---|
439 | finish = 0; |
---|
440 | } |
---|
441 | |
---|
442 | /* FILE * opening functions */ |
---|
443 | else if (PARSECMD("fopen ( )")) |
---|
444 | MY_FOPEN(f = fopen(file, "r")); |
---|
445 | #if defined HAVE_FOPEN64 |
---|
446 | else if (PARSECMD("fopen64 ( )")) |
---|
447 | MY_FOPEN(f = fopen64(file, "r")); |
---|
448 | #endif |
---|
449 | #if defined HAVE___FOPEN64 |
---|
450 | else if (PARSECMD("__fopen64 ( )")) |
---|
451 | MY_FOPEN(f = __fopen64(file, "r")); |
---|
452 | #endif |
---|
453 | else if (PARSECMD("freopen ( )")) |
---|
454 | MY_FOPEN(f = freopen(file, "r", f)); |
---|
455 | #if defined HAVE_FREOPEN64 |
---|
456 | else if (PARSECMD("freopen64 ( )")) |
---|
457 | MY_FOPEN(f = freopen64(file, "r", f)); |
---|
458 | #endif |
---|
459 | #if defined HAVE___FREOPEN64 |
---|
460 | else if (PARSECMD("__freopen64 ( )")) |
---|
461 | MY_FOPEN(f = __freopen64(file, "r", f)); |
---|
462 | #endif |
---|
463 | |
---|
464 | /* FILE * EOF detection */ |
---|
465 | else if (PARSECMD("feof ( %li )", &l1)) |
---|
466 | MY_FEOF(); |
---|
467 | |
---|
468 | /* FILE * closing functions */ |
---|
469 | else if (PARSECMD("fclose ( )")) |
---|
470 | MY_FCLOSE(fclose(f)); |
---|
471 | |
---|
472 | /* FILE * reading functions */ |
---|
473 | else if (PARSECMD("fread ( %li , %li )", &l1, &l2)) |
---|
474 | MY_FREAD(l = fread(tmp, l1, l2, f), tmp, l > 0 ? l * l1 : 0); |
---|
475 | else if (PARSECMD("getc ( )")) |
---|
476 | MY_FREAD(ch = (n = getc(f)), &ch, (n != EOF)); |
---|
477 | else if (PARSECMD("fgetc ( )")) |
---|
478 | MY_FREAD(ch = (n = fgetc(f)), &ch, (n != EOF)); |
---|
479 | else if (PARSECMD("fgets ( %li )", &l1)) |
---|
480 | MY_FREAD(s = fgets(tmp, l1, f), tmp, s ? strlen(tmp) : 0); |
---|
481 | #if defined HAVE__IO_GETC |
---|
482 | else if (PARSECMD("_IO_getc ( )")) |
---|
483 | MY_FREAD(ch = (n = _IO_getc(f)), &ch, (n != EOF)); |
---|
484 | #endif |
---|
485 | #if defined HAVE_FREAD_UNLOCKED |
---|
486 | else if (PARSECMD("fread_unlocked ( %li , %li )", &l1, &l2)) |
---|
487 | MY_FREAD(l = fread_unlocked(tmp, l1, l2, f), tmp, l > 0 ? l * l1 : 0); |
---|
488 | #endif |
---|
489 | #if defined HAVE_FGETS_UNLOCKED |
---|
490 | else if (PARSECMD("fgets_unlocked ( %li )", &l1)) |
---|
491 | MY_FREAD(s = fgets_unlocked(tmp, l1, f), tmp, s ? strlen(tmp) : 0); |
---|
492 | #endif |
---|
493 | #if defined HAVE_GETC_UNLOCKED |
---|
494 | else if (PARSECMD("getc_unlocked ( )")) |
---|
495 | MY_FREAD(ch = (n = getc_unlocked(f)), &ch, (n != EOF)); |
---|
496 | #endif |
---|
497 | #if defined HAVE_FGETC_UNLOCKED |
---|
498 | else if (PARSECMD("fgetc_unlocked ( )")) |
---|
499 | MY_FREAD(ch = (n = fgetc_unlocked(f)), &ch, (n != EOF)); |
---|
500 | #endif |
---|
501 | |
---|
502 | /* FILE * getdelim functions */ |
---|
503 | #if defined HAVE_GETLINE |
---|
504 | else if (PARSECMD("getline ( )")) |
---|
505 | MY_FREAD(l = getline(&lineptr, &k, f), lineptr, l >= 0 ? l : 0); |
---|
506 | #endif |
---|
507 | #if defined HAVE_GETDELIM |
---|
508 | else if (PARSECMD("getdelim ( '%c' )", &ch)) |
---|
509 | MY_FREAD(l = getdelim(&lineptr, &k, ch, f), lineptr, l >= 0 ? l : 0); |
---|
510 | else if (PARSECMD("getdelim ( %i )", &n)) |
---|
511 | MY_FREAD(l = getdelim(&lineptr, &k, n, f), lineptr, l >= 0 ? l : 0); |
---|
512 | #endif |
---|
513 | #if defined HAVE___GETDELIM |
---|
514 | else if (PARSECMD("__getdelim ( '%c' )", &ch)) |
---|
515 | MY_FREAD(l = __getdelim(&lineptr, &k, ch, f), lineptr, l >= 0 ? l : 0); |
---|
516 | else if (PARSECMD("__getdelim ( %i )", &n)) |
---|
517 | MY_FREAD(l = __getdelim(&lineptr, &k, n, f), lineptr, l >= 0 ? l : 0); |
---|
518 | #endif |
---|
519 | |
---|
520 | /* FILE * seeking functions */ |
---|
521 | else if (PARSECMD("fseek ( %li , SEEK_CUR )", &l1)) |
---|
522 | MY_FSEEK(l = fseek(f, l1, SEEK_CUR), |
---|
523 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
---|
524 | else if (PARSECMD("fseek ( %li , SEEK_SET )", &l1)) |
---|
525 | MY_FSEEK(l = fseek(f, l1, SEEK_SET), |
---|
526 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
---|
527 | else if (PARSECMD("fseek ( %li , SEEK_END )", &l1)) |
---|
528 | MY_FSEEK(l = fseek(f, l1, SEEK_END), |
---|
529 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
---|
530 | #if defined HAVE_FSEEKO |
---|
531 | else if (PARSECMD("fseeko ( %li , SEEK_CUR )", &l1)) |
---|
532 | MY_FSEEK(l = fseeko(f, l1, SEEK_CUR), |
---|
533 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
---|
534 | else if (PARSECMD("fseeko ( %li , SEEK_SET )", &l1)) |
---|
535 | MY_FSEEK(l = fseeko(f, l1, SEEK_SET), |
---|
536 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
---|
537 | else if (PARSECMD("fseeko ( %li , SEEK_END )", &l1)) |
---|
538 | MY_FSEEK(l = fseeko(f, l1, SEEK_END), |
---|
539 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
---|
540 | #endif |
---|
541 | #if defined HAVE_FSEEKO64 |
---|
542 | else if (PARSECMD("fseeko64 ( %li , SEEK_CUR )", &l1)) |
---|
543 | MY_FSEEK(l = fseeko64(f, l1, SEEK_CUR), |
---|
544 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
---|
545 | else if (PARSECMD("fseeko64 ( %li , SEEK_SET )", &l1)) |
---|
546 | MY_FSEEK(l = fseeko64(f, l1, SEEK_SET), |
---|
547 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
---|
548 | else if (PARSECMD("fseeko64 ( %li , SEEK_END )", &l1)) |
---|
549 | MY_FSEEK(l = fseeko64(f, l1, SEEK_END), |
---|
550 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
---|
551 | #endif |
---|
552 | #if defined HAVE___FSEEKO64 |
---|
553 | else if (PARSECMD("__fseeko64 ( %li , SEEK_CUR )", &l1)) |
---|
554 | MY_FSEEK(l = __fseeko64(f, l1, SEEK_CUR), |
---|
555 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
---|
556 | else if (PARSECMD("__fseeko64 ( %li , SEEK_SET )", &l1)) |
---|
557 | MY_FSEEK(l = __fseeko64(f, l1, SEEK_SET), |
---|
558 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
---|
559 | else if (PARSECMD("__fseeko64 ( %li , SEEK_END )", &l1)) |
---|
560 | MY_FSEEK(l = __fseeko64(f, l1, SEEK_END), |
---|
561 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
---|
562 | #endif |
---|
563 | else if (PARSECMD("rewind ( )")) |
---|
564 | MY_FSEEK(rewind(f), -retlen); |
---|
565 | else if (PARSECMD("ungetc ( )")) |
---|
566 | MY_FSEEK(if(retoff) ungetc((unsigned char)retbuf[retoff - 1], f), |
---|
567 | retoff ? -1 : 0); |
---|
568 | |
---|
569 | /* Unrecognised sequence */ |
---|
570 | else |
---|
571 | { |
---|
572 | char buf[16]; |
---|
573 | snprintf(buf, 16, strlen(sequence) < 16 ? "%s" : "%.12s...", |
---|
574 | sequence); |
---|
575 | fprintf(stderr, "E: zzat: syntax error near `%s'\n", buf); |
---|
576 | return EXIT_FAILURE; |
---|
577 | } |
---|
578 | |
---|
579 | /* Clean up our mess */ |
---|
580 | if (lineptr) |
---|
581 | free(lineptr); |
---|
582 | |
---|
583 | if (finish && !nloops) |
---|
584 | break; |
---|
585 | } |
---|
586 | |
---|
587 | if (f) |
---|
588 | fclose(f); |
---|
589 | |
---|
590 | if (fd >= 0) |
---|
591 | close(fd); |
---|
592 | |
---|
593 | output(retbuf, retlen); |
---|
594 | free(retbuf); |
---|
595 | free(tmp); |
---|
596 | |
---|
597 | return EXIT_SUCCESS; |
---|
598 | } |
---|
599 | |
---|
600 | #if 0 |
---|
601 | /* Only read() calls */ |
---|
602 | static int zzat_read(char const *name, unsigned char *data, int64_t len, |
---|
603 | int64_t chunk) |
---|
604 | { |
---|
605 | int i, fd = open(name, O_RDONLY); |
---|
606 | if(fd < 0) |
---|
607 | return EXIT_FAILURE; |
---|
608 | for(i = 0; i < len; i += chunk) |
---|
609 | read(fd, data + i, chunk); |
---|
610 | close(fd); |
---|
611 | return EXIT_SUCCESS; |
---|
612 | } |
---|
613 | |
---|
614 | /* Socket seeks and reads */ |
---|
615 | static int zzat_random_socket(char const *name, unsigned char *data, |
---|
616 | int64_t len) |
---|
617 | { |
---|
618 | int i, j, fd = open(name, O_RDONLY); |
---|
619 | if(fd < 0) |
---|
620 | return EXIT_FAILURE; |
---|
621 | for(i = 0; i < 128; i++) |
---|
622 | { |
---|
623 | lseek(fd, myrand() % len, SEEK_SET); |
---|
624 | for(j = 0; j < 4; j++) |
---|
625 | read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096); |
---|
626 | #ifdef HAVE_LSEEK64 |
---|
627 | lseek64(fd, myrand() % len, SEEK_SET); |
---|
628 | for(j = 0; j < 4; j++) |
---|
629 | read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096); |
---|
630 | #endif |
---|
631 | } |
---|
632 | close(fd); |
---|
633 | return EXIT_SUCCESS; |
---|
634 | } |
---|
635 | |
---|
636 | /* Standard stream seeks and reads */ |
---|
637 | static int zzat_random_stream(char const *name, unsigned char *data, |
---|
638 | int64_t len) |
---|
639 | { |
---|
640 | FILE *stream = fopen(name, "r"); |
---|
641 | int i, j; |
---|
642 | if(!stream) |
---|
643 | return EXIT_FAILURE; |
---|
644 | for(i = 0; i < 128; i++) |
---|
645 | { |
---|
646 | long int now; |
---|
647 | fseek(stream, myrand() % len, SEEK_SET); |
---|
648 | for(j = 0; j < 4; j++) |
---|
649 | fread(data + ftell(stream), |
---|
650 | myrand() % (len - ftell(stream)), 1, stream); |
---|
651 | fseek(stream, myrand() % len, SEEK_SET); |
---|
652 | now = ftell(stream); |
---|
653 | for(j = 0; j < 16; j++) |
---|
654 | data[now + j] = getc(stream); |
---|
655 | now = ftell(stream); |
---|
656 | for(j = 0; j < 16; j++) |
---|
657 | data[now + j] = fgetc(stream); |
---|
658 | } |
---|
659 | fclose(stream); |
---|
660 | return EXIT_SUCCESS; |
---|
661 | } |
---|
662 | |
---|
663 | #ifdef HAVE_MMAP |
---|
664 | /* mmap() followed by random memory reads */ |
---|
665 | static int zzat_random_mmap(char const *name, unsigned char *data, |
---|
666 | int64_t len) |
---|
667 | { |
---|
668 | int i, j, fd = open(name, O_RDONLY); |
---|
669 | if(fd < 0) |
---|
670 | return EXIT_FAILURE; |
---|
671 | for(i = 0; i < 128; i++) |
---|
672 | { |
---|
673 | char *map; |
---|
674 | int moff, mlen, pgsz = len + 1; |
---|
675 | #ifdef HAVE_GETPAGESIZE |
---|
676 | pgsz = getpagesize(); |
---|
677 | #endif |
---|
678 | moff = len < pgsz ? 0 : (myrand() % (len / pgsz)) * pgsz; |
---|
679 | mlen = 1 + (myrand() % (len - moff)); |
---|
680 | map = mmap(NULL, mlen, PROT_READ, MAP_PRIVATE, fd, moff); |
---|
681 | if(map == MAP_FAILED) |
---|
682 | return EXIT_FAILURE; |
---|
683 | for(j = 0; j < 128; j++) |
---|
684 | { |
---|
685 | int x = myrand() % mlen; |
---|
686 | data[moff + x] = map[x]; |
---|
687 | } |
---|
688 | munmap(map, mlen); |
---|
689 | } |
---|
690 | close(fd); |
---|
691 | return EXIT_SUCCESS; |
---|
692 | } |
---|
693 | #endif |
---|
694 | #endif |
---|
695 | |
---|
696 | static char const *keyword_list[] = |
---|
697 | { |
---|
698 | "repeat", "(<int>,<sequence>)", "loop <int> times through <sequence>", |
---|
699 | "feof", "(<int>)", "break out of loop or sequence after <int> EOFs", |
---|
700 | NULL |
---|
701 | }; |
---|
702 | |
---|
703 | static char const *function_list[] = |
---|
704 | { |
---|
705 | "fopen", "()", "open file", |
---|
706 | #if defined HAVE_FOPEN64 |
---|
707 | "fopen64", "()", "same as fopen()", |
---|
708 | #endif |
---|
709 | #if defined HAVE___FOPEN64 |
---|
710 | "__fopen64", "()", "same as fopen()", |
---|
711 | #endif |
---|
712 | "freopen", "()", "reopen file", |
---|
713 | #if defined HAVE_FREOPEN64 |
---|
714 | "freopen64", "()", "same as reopen()", |
---|
715 | #endif |
---|
716 | #if defined HAVE___FREOPEN64 |
---|
717 | "__freopen64", "()", "same as reopen()", |
---|
718 | #endif |
---|
719 | "fclose", "()", "close file", |
---|
720 | "fread", "(<inta>,<intb>)", "read <intb> chunks of <inta> bytes", |
---|
721 | "getc", "()", "get one character (can be a macro)", |
---|
722 | "fgetc", "()", "get one character", |
---|
723 | "fgets", "(<int>)", "read one line no longer than <int> bytes", |
---|
724 | #if defined HAVE__IO_GETC |
---|
725 | "_IO_getc", "()", "get one character", |
---|
726 | #endif |
---|
727 | #if defined HAVE_FREAD_UNLOCKED |
---|
728 | "fread_unlocked", "(<inta>,<intb>)", "same as fread(), unlocked I/O version", |
---|
729 | #endif |
---|
730 | #if defined HAVE_FGETS_UNLOCKED |
---|
731 | "fgets_unlocked", "(<int>)", "same as fgets(), unlocked I/O version", |
---|
732 | #endif |
---|
733 | #if defined HAVE_GETC_UNLOCKED |
---|
734 | "getc_unlocked", "()", "same as getc(), unlocked I/O version", |
---|
735 | #endif |
---|
736 | #if defined HAVE_FGETC_UNLOCKED |
---|
737 | "fgetc_unlocked", "()", "same as fgetc(), unlocked I/O version", |
---|
738 | #endif |
---|
739 | #if defined HAVE_GETLINE |
---|
740 | "getline", "()", "read one complete line of text", |
---|
741 | #endif |
---|
742 | #if defined HAVE_GETDELIM |
---|
743 | "getdelim", "('<char>')", "read all data until delimiter character <char>", |
---|
744 | "getdelim", "(<int>)", "read all data until delimiter character <int>", |
---|
745 | #endif |
---|
746 | #if defined HAVE___GETDELIM |
---|
747 | "__getdelim", "('<char>')", "same as getdelim()", |
---|
748 | "__getdelim", "(<int>)", "same as getdelim()", |
---|
749 | #endif |
---|
750 | "fseek", "(<int>,<whence>)", "seek using SEEK_CUR, SEEK_SET or SEEK_END", |
---|
751 | #if defined HAVE_FSEEKO |
---|
752 | "fseeko", "(<int>,<whence>)", "same as fseek()", |
---|
753 | #endif |
---|
754 | #if defined HAVE_FSEEKO64 |
---|
755 | "fseeko64", "(<int>,<whence>)", "same as fseek()", |
---|
756 | #endif |
---|
757 | #if defined HAVE___FSEEKO64 |
---|
758 | "__fseeko64", "(<int>,<whence>)", "same as fseek()", |
---|
759 | #endif |
---|
760 | "rewind", "()", "rewind to the beginning of the stream", |
---|
761 | "ungetc", "()", "put one byte back in the stream", |
---|
762 | NULL |
---|
763 | }; |
---|
764 | |
---|
765 | static void print_list(char const **list) |
---|
766 | { |
---|
767 | static char const spaces[] = " "; |
---|
768 | |
---|
769 | while (*list) |
---|
770 | { |
---|
771 | size_t len = printf(" %s%s", list[0], list[1]); |
---|
772 | if (len < strlen(spaces)) |
---|
773 | printf("%s", spaces + len); |
---|
774 | printf("%s\n", list[2]); |
---|
775 | list += 3; |
---|
776 | } |
---|
777 | } |
---|
778 | |
---|
779 | static void syntax(void) |
---|
780 | { |
---|
781 | printf("Available control keywords:\n"); |
---|
782 | print_list(keyword_list); |
---|
783 | printf("\n"); |
---|
784 | printf("Available functions:\n"); |
---|
785 | print_list(function_list); |
---|
786 | } |
---|
787 | |
---|
788 | static void version(void) |
---|
789 | { |
---|
790 | printf("zzat %s\n", PACKAGE_VERSION); |
---|
791 | printf("Copyright (C) 2002-2010 Sam Hocevar <sam@hocevar.net>\n"); |
---|
792 | printf("This program is free software. It comes without any warranty, to the extent\n"); |
---|
793 | printf("permitted by applicable law. You can redistribute it and/or modify it under\n"); |
---|
794 | printf("the terms of the Do What The Fuck You Want To Public License, Version 2, as\n"); |
---|
795 | printf("published by Sam Hocevar. See <http://sam.zoy.org/wtfpl/> for more details.\n"); |
---|
796 | printf("\n"); |
---|
797 | printf("Written by Sam Hocevar. Report bugs to <sam@hocevar.net>.\n"); |
---|
798 | } |
---|
799 | |
---|
800 | static void usage(void) |
---|
801 | { |
---|
802 | printf("Usage: zzat [AbdeEntTv] [-x sequence] [FILE...]\n"); |
---|
803 | printf(" zzat -l | --list\n"); |
---|
804 | printf(" zzat -h | --help\n"); |
---|
805 | printf(" zzat -V | --version\n"); |
---|
806 | printf("Read FILE using a sequence of various I/O methods.\n"); |
---|
807 | printf("\n"); |
---|
808 | printf("Mandatory arguments to long options are mandatory for short options too.\n"); |
---|
809 | printf(" -A, --show-all equivalent to -vET\n"); |
---|
810 | printf(" -b, --number-nonblank number nonempty output lines\n"); |
---|
811 | printf(" -d, --debug print debugging information\n"); |
---|
812 | printf(" -e equivalent to -vE\n"); |
---|
813 | printf(" -E, --show-ends display $ at end of each line\n"); |
---|
814 | printf(" -n, --number number all output lines\n"); |
---|
815 | printf(" -r, --repeat=<loops> concatenate command line files <loops> times\n"); |
---|
816 | printf(" -t equivalent to -vT\n"); |
---|
817 | printf(" -T, --show-tabs display TAB characters as ^I\n"); |
---|
818 | printf(" -v, --show-nonprinting use ^ and M- notation, except for LFD and TAB\n"); |
---|
819 | printf(" -x, --execute=<sequence> execute commands in <sequence>\n"); |
---|
820 | printf(" -l, --list list available program functions\n"); |
---|
821 | printf(" -h, --help display this help and exit\n"); |
---|
822 | printf(" -V, --version output version information and exit\n"); |
---|
823 | printf("\n"); |
---|
824 | printf("Written by Sam Hocevar. Report bugs to <sam@hocevar.net>.\n"); |
---|
825 | } |
---|
826 | |
---|