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

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

Add fortify versions of libc calls to zzat.

  • Property svn:keywords set to Id
File size: 25.9 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 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
59static int run(char const *sequence, char const *file);
60static void output(char const *buf, size_t len);
61
62static void syntax(void);
63static void version(void);
64static void usage(void);
65
66/* Global parameters */
67static int debug = 0;
68static int repeat = 1;
69static char escape_tabs = 0;
70static char escape_ends = 0;
71static char escape_other = 0;
72static char number_lines = 0;
73static char number_nonblank = 0;
74static char squeeze_lines = 0;
75
76/* Global output state */
77static int ncrs = 0;
78static int line = 1;
79static char newline = 1;
80
81/*
82 * Main program.
83 */
84
85int 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
194static 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
347struct parser
348{
349    char tmpfmt[1024], ch, lastch;
350};
351
352static 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
383static 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___FGETS_CHK
482        else if (PARSECMD("__fgets_chk ( %li )", &l1))
483            MY_FREAD(s = __fgets_chk(tmp, l1, l1, f), tmp, s ? strlen(tmp) : 0);
484#endif
485#if defined HAVE__IO_GETC
486        else if (PARSECMD("_IO_getc ( )"))
487            MY_FREAD(ch = (n = _IO_getc(f)), &ch, (n != EOF));
488#endif
489#if defined HAVE___FREAD_CHK
490        else if (PARSECMD("__fread_chk ( %li , %li )", &l1, &l2))
491            MY_FREAD(l = __fread_chk(tmp, l1 * l2, l1, l2, f), tmp, l > 0 ? l * l1 : 0);
492#endif
493#if defined HAVE_FREAD_UNLOCKED
494        else if (PARSECMD("fread_unlocked ( %li , %li )", &l1, &l2))
495            MY_FREAD(l = fread_unlocked(tmp, l1, l2, f), tmp, l > 0 ? l * l1 : 0);
496#endif
497#if defined HAVE___FREAD_UNLOCKED_CHK
498        else if (PARSECMD("__fread_unlocked_chk ( %li , %li )", &l1, &l2))
499            MY_FREAD(l = __fread_unlocked_chk(tmp, l1 * l2, l1, l2, f), tmp, l > 0 ? l * l1 : 0);
500#endif
501#if defined HAVE_FGETS_UNLOCKED
502        else if (PARSECMD("fgets_unlocked ( %li )", &l1))
503            MY_FREAD(s = fgets_unlocked(tmp, l1, f), tmp, s ? strlen(tmp) : 0);
504#endif
505#if defined HAVE___FGETS_UNLOCKED_CHK
506        else if (PARSECMD("__fgets_unlocked_chk ( %li )", &l1))
507            MY_FREAD(s = __fgets_unlocked_chk(tmp, l1, l1, f), tmp, s ? strlen(tmp) : 0);
508#endif
509#if defined HAVE_GETC_UNLOCKED
510        else if (PARSECMD("getc_unlocked ( )"))
511            MY_FREAD(ch = (n = getc_unlocked(f)), &ch, (n != EOF));
512#endif
513#if defined HAVE_FGETC_UNLOCKED
514        else if (PARSECMD("fgetc_unlocked ( )"))
515            MY_FREAD(ch = (n = fgetc_unlocked(f)), &ch, (n != EOF));
516#endif
517
518        /* FILE * getdelim functions */
519#if defined HAVE_GETLINE
520        else if (PARSECMD("getline ( )"))
521            MY_FREAD(l = getline(&lineptr, &k, f), lineptr, l >= 0 ? l : 0);
522#endif
523#if defined HAVE_GETDELIM
524        else if (PARSECMD("getdelim ( '%c' )", &ch))
525            MY_FREAD(l = getdelim(&lineptr, &k, ch, f), lineptr, l >= 0 ? l : 0);
526        else if (PARSECMD("getdelim ( %i )", &n))
527            MY_FREAD(l = getdelim(&lineptr, &k, n, f), lineptr, l >= 0 ? l : 0);
528#endif
529#if defined HAVE___GETDELIM
530        else if (PARSECMD("__getdelim ( '%c' )", &ch))
531            MY_FREAD(l = __getdelim(&lineptr, &k, ch, f), lineptr, l >= 0 ? l : 0);
532        else if (PARSECMD("__getdelim ( %i )", &n))
533            MY_FREAD(l = __getdelim(&lineptr, &k, n, f), lineptr, l >= 0 ? l : 0);
534#endif
535
536        /* FILE * seeking functions */
537        else if (PARSECMD("fseek ( %li , SEEK_CUR )", &l1))
538            MY_FSEEK(l = fseek(f, l1, SEEK_CUR),
539                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
540        else if (PARSECMD("fseek ( %li , SEEK_SET )", &l1))
541            MY_FSEEK(l = fseek(f, l1, SEEK_SET),
542                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
543        else if (PARSECMD("fseek ( %li , SEEK_END )", &l1))
544            MY_FSEEK(l = fseek(f, l1, SEEK_END),
545                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
546#if defined HAVE_FSEEKO
547        else if (PARSECMD("fseeko ( %li , SEEK_CUR )", &l1))
548            MY_FSEEK(l = fseeko(f, l1, SEEK_CUR),
549                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
550        else if (PARSECMD("fseeko ( %li , SEEK_SET )", &l1))
551            MY_FSEEK(l = fseeko(f, l1, SEEK_SET),
552                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
553        else if (PARSECMD("fseeko ( %li , SEEK_END )", &l1))
554            MY_FSEEK(l = fseeko(f, l1, SEEK_END),
555                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
556#endif
557#if defined HAVE_FSEEKO64
558        else if (PARSECMD("fseeko64 ( %li , SEEK_CUR )", &l1))
559            MY_FSEEK(l = fseeko64(f, l1, SEEK_CUR),
560                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
561        else if (PARSECMD("fseeko64 ( %li , SEEK_SET )", &l1))
562            MY_FSEEK(l = fseeko64(f, l1, SEEK_SET),
563                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
564        else if (PARSECMD("fseeko64 ( %li , SEEK_END )", &l1))
565            MY_FSEEK(l = fseeko64(f, l1, SEEK_END),
566                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
567#endif
568#if defined HAVE___FSEEKO64
569        else if (PARSECMD("__fseeko64 ( %li , SEEK_CUR )", &l1))
570            MY_FSEEK(l = __fseeko64(f, l1, SEEK_CUR),
571                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
572        else if (PARSECMD("__fseeko64 ( %li , SEEK_SET )", &l1))
573            MY_FSEEK(l = __fseeko64(f, l1, SEEK_SET),
574                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
575        else if (PARSECMD("__fseeko64 ( %li , SEEK_END )", &l1))
576            MY_FSEEK(l = __fseeko64(f, l1, SEEK_END),
577                     ftell(f) >= 0 ? ftell(f) - retoff : 0);
578#endif
579        else if (PARSECMD("rewind ( )"))
580            MY_FSEEK(rewind(f), -retlen);
581        else if (PARSECMD("ungetc ( )"))
582            MY_FSEEK(if(retoff) ungetc((unsigned char)retbuf[retoff - 1], f),
583                     retoff ? -1 : 0);
584
585        /* Unrecognised sequence */
586        else
587        {
588            char buf[16];
589            snprintf(buf, 16, strlen(sequence) < 16 ? "%s" : "%.12s...",
590                     sequence);
591            fprintf(stderr, "E: zzat: syntax error near `%s'\n", buf);
592            return EXIT_FAILURE;
593        }
594
595        /* Clean up our mess */
596        if (lineptr)
597            free(lineptr);
598
599        if (finish && !nloops)
600            break;
601    }
602
603    if (f)
604        fclose(f);
605
606    if (fd >= 0)
607        close(fd);
608
609    output(retbuf, retlen);
610    free(retbuf);
611    free(tmp);
612
613    return EXIT_SUCCESS;
614}
615
616#if 0
617/* Only read() calls */
618static int zzat_read(char const *name, unsigned char *data, int64_t len,
619                      int64_t chunk)
620{
621    int i, fd = open(name, O_RDONLY);
622    if(fd < 0)
623        return EXIT_FAILURE;
624    for(i = 0; i < len; i += chunk)
625        read(fd, data + i, chunk);
626    close(fd);
627    return EXIT_SUCCESS;
628}
629
630/* Socket seeks and reads */
631static int zzat_random_socket(char const *name, unsigned char *data,
632                               int64_t len)
633{
634    int i, j, fd = open(name, O_RDONLY);
635    if(fd < 0)
636        return EXIT_FAILURE;
637    for(i = 0; i < 128; i++)
638    {
639        lseek(fd, myrand() % len, SEEK_SET);
640        for(j = 0; j < 4; j++)
641            read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
642#ifdef HAVE_LSEEK64
643        lseek64(fd, myrand() % len, SEEK_SET);
644        for(j = 0; j < 4; j++)
645            read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
646#endif
647    }
648    close(fd);
649    return EXIT_SUCCESS;
650}
651
652/* Standard stream seeks and reads */
653static int zzat_random_stream(char const *name, unsigned char *data,
654                               int64_t len)
655{
656    FILE *stream = fopen(name, "r");
657    int i, j;
658    if(!stream)
659        return EXIT_FAILURE;
660    for(i = 0; i < 128; i++)
661    {
662        long int now;
663        fseek(stream, myrand() % len, SEEK_SET);
664        for(j = 0; j < 4; j++)
665            fread(data + ftell(stream),
666                  myrand() % (len - ftell(stream)), 1, stream);
667        fseek(stream, myrand() % len, SEEK_SET);
668        now = ftell(stream);
669        for(j = 0; j < 16; j++)
670            data[now + j] = getc(stream);
671        now = ftell(stream);
672        for(j = 0; j < 16; j++)
673            data[now + j] = fgetc(stream);
674    }
675    fclose(stream);
676    return EXIT_SUCCESS;
677}
678
679#ifdef HAVE_MMAP
680/* mmap() followed by random memory reads */
681static int zzat_random_mmap(char const *name, unsigned char *data,
682                            int64_t len)
683{
684    int i, j, fd = open(name, O_RDONLY);
685    if(fd < 0)
686        return EXIT_FAILURE;
687    for(i = 0; i < 128; i++)
688    {
689        char *map;
690        int moff, mlen, pgsz = len + 1;
691#ifdef HAVE_GETPAGESIZE
692        pgsz = getpagesize();
693#endif
694        moff = len < pgsz ? 0 : (myrand() % (len / pgsz)) * pgsz;
695        mlen = 1 + (myrand() % (len - moff));
696        map = mmap(NULL, mlen, PROT_READ, MAP_PRIVATE, fd, moff);
697        if(map == MAP_FAILED)
698            return EXIT_FAILURE;
699        for(j = 0; j < 128; j++)
700        {
701            int x = myrand() % mlen;
702            data[moff + x] = map[x];
703        }
704        munmap(map, mlen);
705    }
706    close(fd);
707    return EXIT_SUCCESS;
708}
709#endif
710#endif
711
712static char const *keyword_list[] =
713{
714    "repeat", "(<int>,<sequence>)", "loop <int> times through <sequence>",
715    "feof", "(<int>)", "break out of loop or sequence after <int> EOFs",
716    NULL
717};
718
719static char const *function_list[] =
720{
721    "fopen", "()", "open file",
722#if defined HAVE_FOPEN64
723    "fopen64", "()", "same as fopen()",
724#endif
725#if defined HAVE___FOPEN64
726    "__fopen64", "()", "same as fopen()",
727#endif
728    "freopen", "()", "reopen file",
729#if defined HAVE_FREOPEN64
730    "freopen64", "()", "same as reopen()",
731#endif
732#if defined HAVE___FREOPEN64
733    "__freopen64", "()", "same as reopen()",
734#endif
735    "fclose", "()", "close file",
736    "fread", "(<inta>,<intb>)", "read <intb> chunks of <inta> bytes",
737    "getc", "()", "get one character (can be a macro)",
738    "fgetc", "()", "get one character",
739    "fgets", "(<int>)", "read one line no longer than <int> bytes",
740#if defined HAVE___FGETS_CHK
741    "__fgets_chk", "(<int>)", "same as fgets(), fortified version",
742#endif
743#if defined HAVE__IO_GETC
744    "_IO_getc", "()", "get one character",
745#endif
746#if defined HAVE___FREAD_CHK
747    "__fread_chk", "(<inta>,<intb>)", "same as fread(), fortified version",
748#endif
749#if defined HAVE_FREAD_UNLOCKED
750    "fread_unlocked", "(<inta>,<intb>)", "same as fread(), unlocked I/O version",
751#endif
752#if defined HAVE___FREAD_UNLOCKED_CHK
753    "__fread_unlocked_chk", "(<inta>,<intb>)", "same as fread_unlocked(), fortified version",
754#endif
755#if defined HAVE_FGETS_UNLOCKED
756    "fgets_unlocked", "(<int>)", "same as fgets(), unlocked I/O version",
757#endif
758#if defined HAVE___FGETS__UNLOCKED_CHK
759    "__fgets_unlocked_chk", "(<int>)", "same as fgets_unlocked(), fortified version",
760#endif
761#if defined HAVE_GETC_UNLOCKED
762    "getc_unlocked", "()", "same as getc(), unlocked I/O version",
763#endif
764#if defined HAVE_FGETC_UNLOCKED
765    "fgetc_unlocked", "()", "same as fgetc(), unlocked I/O version",
766#endif
767#if defined HAVE_GETLINE
768    "getline", "()", "read one complete line of text",
769#endif
770#if defined HAVE_GETDELIM
771    "getdelim", "('<char>')", "read all data until delimiter character <char>",
772    "getdelim", "(<int>)", "read all data until delimiter character <int>",
773#endif
774#if defined HAVE___GETDELIM
775    "__getdelim", "('<char>')", "same as getdelim()",
776    "__getdelim", "(<int>)", "same as getdelim()",
777#endif
778    "fseek", "(<int>,<whence>)", "seek using SEEK_CUR, SEEK_SET or SEEK_END",
779#if defined HAVE_FSEEKO
780    "fseeko", "(<int>,<whence>)", "same as fseek()",
781#endif
782#if defined HAVE_FSEEKO64
783    "fseeko64", "(<int>,<whence>)", "same as fseek()",
784#endif
785#if defined HAVE___FSEEKO64
786    "__fseeko64", "(<int>,<whence>)", "same as fseek()",
787#endif
788    "rewind", "()", "rewind to the beginning of the stream",
789    "ungetc", "()", "put one byte back in the stream",
790    NULL
791};
792
793static void print_list(char const **list)
794{
795    static char const spaces[] = "                                ";
796
797    while (*list)
798    {
799        size_t len = printf("  %s%s", list[0], list[1]);
800        if (len < strlen(spaces))
801            printf("%s", spaces + len);
802        printf("%s\n", list[2]);
803        list += 3;
804    }
805}
806
807static void syntax(void)
808{
809    printf("Available control keywords:\n");
810    print_list(keyword_list);
811    printf("\n");
812    printf("Available functions:\n");
813    print_list(function_list);
814}
815
816static void version(void)
817{
818    printf("zzat %s\n", PACKAGE_VERSION);
819    printf("Copyright (C) 2002-2010 Sam Hocevar <sam@hocevar.net>\n");
820    printf("This program is free software. It comes without any warranty, to the extent\n");
821    printf("permitted by applicable law. You can redistribute it and/or modify it under\n");
822    printf("the terms of the Do What The Fuck You Want To Public License, Version 2, as\n");
823    printf("published by Sam Hocevar. See <http://sam.zoy.org/wtfpl/> for more details.\n");
824    printf("\n");
825    printf("Written by Sam Hocevar. Report bugs to <sam@hocevar.net>.\n");
826}
827
828static void usage(void)
829{
830    printf("Usage: zzat [AbdeEntTv] [-x sequence] [FILE...]\n");
831    printf("       zzat -l | --list\n");
832    printf("       zzat -h | --help\n");
833    printf("       zzat -V | --version\n");
834    printf("Read FILE using a sequence of various I/O methods.\n");
835    printf("\n");
836    printf("Mandatory arguments to long options are mandatory for short options too.\n");
837    printf("  -A, --show-all            equivalent to -vET\n");
838    printf("  -b, --number-nonblank     number nonempty output lines\n");
839    printf("  -d, --debug               print debugging information\n");
840    printf("  -e                        equivalent to -vE\n");
841    printf("  -E, --show-ends           display $ at end of each line\n");
842    printf("  -n, --number              number all output lines\n");
843    printf("  -r, --repeat=<loops>      concatenate command line files <loops> times\n");
844    printf("  -t                        equivalent to -vT\n");
845    printf("  -T, --show-tabs           display TAB characters as ^I\n");
846    printf("  -v, --show-nonprinting    use ^ and M- notation, except for LFD and TAB\n");
847    printf("  -x, --execute=<sequence>  execute commands in <sequence>\n");
848    printf("  -l, --list                list available program functions\n");
849    printf("  -h, --help                display this help and exit\n");
850    printf("  -V, --version             output version information and exit\n");
851    printf("\n");
852    printf("Written by Sam Hocevar. Report bugs to <sam@hocevar.net>.\n");
853}
854
Note: See TracBrowser for help on using the repository browser.