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

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

Get rid of the getopt reimplementation and depend on libcaca instead.

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