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

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

Add a -r flag to zzcat to repeatedly cat file lists.

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