source: zzuf/trunk/src/zzcat.c @ 4286

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

Fix compilation warnings and errors on OpenSolaris? caused by a few of
our source feature macros.

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