source: zzuf/trunk/test/zzcat.c @ 4002

Last change on this file since 4002 was 4002, checked in by Sam Hocevar, 10 years ago

Add a getc_unlocked method to zzcat.

  • Property svn:keywords set to Id
File size: 6.6 KB
Line 
1/*
2 *  zzcat - various cat reimplementations for testing purposes
3 *  Copyright (c) 2006, 2007 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: zzcat.c 4002 2009-11-22 18:54:17Z sam $
7 *
8 *  This program is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15#include "config.h"
16
17/* Needed for lseek64() */
18#define _LARGEFILE64_SOURCE
19/* Needed for O_RDONLY on HP-UX */
20#define _INCLUDE_POSIX_SOURCE
21
22#if defined HAVE_STDINT_H
23#   include <stdint.h>
24#elif defined HAVE_INTTYPES_H
25#   include <inttypes.h>
26#endif
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <fcntl.h>
30#if defined HAVE_UNISTD_H
31#   include <unistd.h>
32#endif
33#if defined HAVE_SYS_MMAN_H
34#   include <sys/mman.h>
35#endif
36#include <stdlib.h>
37#include <stdio.h>
38#include <string.h>
39
40static inline unsigned int myrand(void)
41{
42    static int seed = 1;
43    int x, y;
44    x = (seed + 0x12345678) << 11;
45    y = (seed + 0xfedcba98) >> 21;
46    seed = x * 1010101 + y * 343434;
47    return seed;
48}
49
50int main(int argc, char *argv[])
51{
52    int64_t len;
53    unsigned char *data;
54    char const *name;
55    FILE *stream;
56    int cmd, i, j, fd;
57    char c;
58
59    if(argc != 3)
60        return EXIT_FAILURE;
61
62    name = argv[2];
63
64    /* Read the whole file */
65    fd = open(name, O_RDONLY);
66    if(fd < 0)
67        return EXIT_FAILURE;
68    len = lseek(fd, 0, SEEK_END);
69    if(len < 0)
70        return EXIT_FAILURE;
71    data = malloc(len + 16); /* 16 safety bytes */
72    lseek(fd, 0, SEEK_SET);
73    read(fd, data, len);
74    close(fd);
75
76    /* Read shit here and there, using different methods */
77    switch((cmd = atoi(argv[1])))
78    {
79    /* 0x: simple fd calls
80     * 1x: complex fd calls */
81    case 0: /* only read() calls */
82        fd = open(name, O_RDONLY);
83        if(fd < 0)
84            return EXIT_FAILURE;
85        for(i = 0; i < len; i++)
86            read(fd, data + i, 1);
87        close(fd);
88        break;
89    /* 2x: simple stdio calls
90     * 3x: complex stdio calls */
91    case 20: /* only fread() calls */
92        stream = fopen(name, "r");
93        if(!stream)
94            return EXIT_FAILURE;
95        for(i = 0; i < len; i++)
96            fread(data + i, 1, 1, stream);
97        fclose(stream);
98        break;
99    case 21: /* only getc() calls */
100#if defined HAVE_GETC_UNLOCKED
101    case 22: /* only getc_unlocked() calls */
102#endif
103        stream = fopen(name, "r");
104        if(!stream)
105            return EXIT_FAILURE;
106        for(i = 0; i < len; i++)
107#if defined HAVE_GETC_UNLOCKED
108            data[i] = cmd == 21 ? getc(stream)
109                                : getc_unlocked(stream);
110#else
111            data[i] = getc(stream);
112#endif
113        fclose(stream);
114        break;
115    case 23: /* only fgetc() calls */
116        stream = fopen(name, "r");
117        if(!stream)
118            return EXIT_FAILURE;
119        for(i = 0; i < len; i++)
120            data[i] = fgetc(stream);
121        fclose(stream);
122        break;
123#if defined HAVE_GETLINE
124    case 24: /* getline() and getc() calls */
125#if defined HAVE_GETC_UNLOCKED
126    case 25: /* getline() and getc_unlocked() calls */
127#endif
128        stream = fopen(name, "r");
129        if(!stream)
130            return EXIT_FAILURE;
131        i = 0;
132#if defined HAVE_GETC_UNLOCKED
133        while ((c = (cmd == 24 ? getc(stream)
134                               : getc_unlocked(stream))) != EOF)
135#else
136        while ((c = getc(stream)) != EOF)
137#endif
138        {
139            char *line;
140            ssize_t ret;
141            size_t n;
142
143            ungetc(c, stream);
144            line = NULL;
145            ret = getline(&line, &n, stream);
146            for (j = 0; j < ret; i++, j++)
147                data[i] = line[j];
148        }
149        fclose(stream);
150        break;
151#endif
152    case 30: /* one fread(), then only getc() calls */
153        stream = fopen(name, "r");
154        if(!stream)
155            return EXIT_FAILURE;
156        fread(data, 1, 10, stream);
157        for(i = 10; i < len; i++)
158            data[i] = getc(stream);
159        fclose(stream);
160        break;
161    case 31: /* one fread(), then only fgetc() calls */
162        stream = fopen(name, "r");
163        if(!stream)
164            return EXIT_FAILURE;
165        fread(data, 1, 10, stream);
166        for(i = 10; i < len; i++)
167            data[i] = fgetc(stream);
168        fclose(stream);
169        break;
170    /* 4x: complex, random stuff */
171    case 40: /* socket seeks and reads */
172        fd = open(name, O_RDONLY);
173        if(fd < 0)
174            return EXIT_FAILURE;
175        for(i = 0; i < 128; i++)
176        {
177            lseek(fd, myrand() % len, SEEK_SET);
178            for(j = 0; j < 4; j++)
179                read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
180#ifdef HAVE_LSEEK64
181            lseek64(fd, myrand() % len, SEEK_SET);
182            for(j = 0; j < 4; j++)
183                read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
184#endif
185        }
186        close(fd);
187        break;
188    case 41: /* std streams seeks and reads */
189        stream = fopen(name, "r");
190        if(!stream)
191            return EXIT_FAILURE;
192        for(i = 0; i < 128; i++)
193        {
194            long int now;
195            fseek(stream, myrand() % len, SEEK_SET);
196            for(j = 0; j < 4; j++)
197                fread(data + ftell(stream),
198                      myrand() % (len - ftell(stream)), 1, stream);
199            fseek(stream, myrand() % len, SEEK_SET);
200            now = ftell(stream);
201            for(j = 0; j < 16; j++)
202                data[now + j] = getc(stream);
203            now = ftell(stream);
204            for(j = 0; j < 16; j++)
205                data[now + j] = fgetc(stream);
206        }
207        fclose(stream);
208        break;
209    case 42: /* mmap() */
210        fd = open(name, O_RDONLY);
211        if(fd < 0)
212            return EXIT_FAILURE;
213#ifdef HAVE_MMAP
214        for(i = 0; i < 128; i++)
215        {
216            char *map;
217            int moff, mlen, pgsz = len + 1;
218#ifdef HAVE_GETPAGESIZE
219            pgsz = getpagesize();
220#endif
221            moff = len < pgsz ? 0 : (myrand() % (len / pgsz)) * pgsz;
222            mlen = 1 + (myrand() % (len - moff));
223            map = mmap(NULL, mlen, PROT_READ, MAP_PRIVATE, fd, moff);
224            if(map == MAP_FAILED)
225                return EXIT_FAILURE;
226            for(j = 0; j < 128; j++)
227            {
228                int x = myrand() % mlen;
229                data[moff + x] = map[x];
230            }
231            munmap(map, mlen);
232        }
233#endif
234        close(fd);
235        break;
236    default:
237        return EXIT_FAILURE;
238    }
239
240    /* Write what we have read */
241    fwrite(data, len, 1, stdout);
242    free(data);
243
244    return EXIT_SUCCESS;
245}
246
Note: See TracBrowser for help on using the repository browser.