source: zzuf/trunk/src/zzat.c @ 4654

Last change on this file since 4654 was 4654, checked in by Sam Hocevar, 11 years ago

Fix a bug caused by undefined function call precedence.

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