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

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

Compilation fix for systems without get_unlocked().

  • Property svn:keywords set to Id
File size: 6.4 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 3742 2009-10-27 23:10:22Z 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 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(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        stream = fopen(name, "r");
101        if(!stream)
102            return EXIT_FAILURE;
103        for(i = 0; i < len; i++)
104            data[i] = getc(stream);
105        fclose(stream);
106        break;
107    case 22: /* only fgetc() calls */
108        stream = fopen(name, "r");
109        if(!stream)
110            return EXIT_FAILURE;
111        for(i = 0; i < len; i++)
112            data[i] = fgetc(stream);
113        fclose(stream);
114        break;
115#if defined HAVE_GETLINE
116    case 23: /* getline() and getc() calls */
117#if defined HAVE_GETC_UNLOCKED
118    case 24: /* getline() and getc_unlocked() calls */
119#endif
120        stream = fopen(name, "r");
121        if(!stream)
122            return EXIT_FAILURE;
123        i = 0;
124#if defined HAVE_GETC_UNLOCKED
125        while ((c = (atoi(argv[1]) == 23) ? getc(stream)
126                                          : getc_unlocked(stream)) != EOF)
127#else
128        while ((c = getc(stream)) != EOF)
129#endif
130        {
131            char *line;
132            ssize_t ret;
133            size_t n;
134
135            ungetc(c, stream);
136            line = NULL;
137            ret = getline(&line, &n, stream);
138            for (j = 0; j < ret; i++, j++)
139                data[i] = line[j];
140        }
141        fclose(stream);
142        break;
143#endif
144    case 30: /* one fread(), then only getc() calls */
145        stream = fopen(name, "r");
146        if(!stream)
147            return EXIT_FAILURE;
148        fread(data, 1, 10, stream);
149        for(i = 10; i < len; i++)
150            data[i] = getc(stream);
151        fclose(stream);
152        break;
153    case 31: /* one fread(), then only fgetc() calls */
154        stream = fopen(name, "r");
155        if(!stream)
156            return EXIT_FAILURE;
157        fread(data, 1, 10, stream);
158        for(i = 10; i < len; i++)
159            data[i] = fgetc(stream);
160        fclose(stream);
161        break;
162    /* 4x: complex, random stuff */
163    case 40: /* socket seeks and reads */
164        fd = open(name, O_RDONLY);
165        if(fd < 0)
166            return EXIT_FAILURE;
167        for(i = 0; i < 128; i++)
168        {
169            lseek(fd, myrand() % len, SEEK_SET);
170            for(j = 0; j < 4; j++)
171                read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
172#ifdef HAVE_LSEEK64
173            lseek64(fd, myrand() % len, SEEK_SET);
174            for(j = 0; j < 4; j++)
175                read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
176#endif
177        }
178        close(fd);
179        break;
180    case 41: /* std streams seeks and reads */
181        stream = fopen(name, "r");
182        if(!stream)
183            return EXIT_FAILURE;
184        for(i = 0; i < 128; i++)
185        {
186            long int now;
187            fseek(stream, myrand() % len, SEEK_SET);
188            for(j = 0; j < 4; j++)
189                fread(data + ftell(stream),
190                      myrand() % (len - ftell(stream)), 1, stream);
191            fseek(stream, myrand() % len, SEEK_SET);
192            now = ftell(stream);
193            for(j = 0; j < 16; j++)
194                data[now + j] = getc(stream);
195            now = ftell(stream);
196            for(j = 0; j < 16; j++)
197                data[now + j] = fgetc(stream);
198        }
199        fclose(stream);
200        break;
201    case 42: /* mmap() */
202        fd = open(name, O_RDONLY);
203        if(fd < 0)
204            return EXIT_FAILURE;
205#ifdef HAVE_MMAP
206        for(i = 0; i < 128; i++)
207        {
208            char *map;
209            int moff, mlen, pgsz = len + 1;
210#ifdef HAVE_GETPAGESIZE
211            pgsz = getpagesize();
212#endif
213            moff = len < pgsz ? 0 : (myrand() % (len / pgsz)) * pgsz;
214            mlen = 1 + (myrand() % (len - moff));
215            map = mmap(NULL, mlen, PROT_READ, MAP_PRIVATE, fd, moff);
216            if(map == MAP_FAILED)
217                return EXIT_FAILURE;
218            for(j = 0; j < 128; j++)
219            {
220                int x = myrand() % mlen;
221                data[moff + x] = map[x];
222            }
223            munmap(map, mlen);
224        }
225#endif
226        close(fd);
227        break;
228    default:
229        return EXIT_FAILURE;
230    }
231
232    /* Write what we have read */
233    fwrite(data, len, 1, stdout);
234    free(data);
235
236    return EXIT_SUCCESS;
237}
238
Note: See TracBrowser for help on using the repository browser.