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

Last change on this file since 2563 was 2563, checked in by Sam Hocevar, 12 years ago
  • Add slightly more complex modes to zzcat.
  • Property svn:keywords set to Id
File size: 5.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 2563 2008-07-18 09:28:06Z 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
58    if(argc != 3)
59        return EXIT_FAILURE;
60
61    name = argv[2];
62
63    /* Read the whole file */
64    fd = open(name, O_RDONLY);
65    if(fd < 0)
66        return EXIT_FAILURE;
67    len = lseek(fd, 0, SEEK_END);
68    if(len < 0)
69        return EXIT_FAILURE;
70    data = malloc(len + 16); /* 16 safety bytes */
71    lseek(fd, 0, SEEK_SET);
72    read(fd, data, len);
73    close(fd);
74
75    /* Read shit here and there, using different methods */
76    switch(atoi(argv[1]))
77    {
78    /* 0x: simple fd calls
79     * 1x: complex fd calls */
80    case 0: /* only read() calls */
81        fd = open(name, O_RDONLY);
82        if(fd < 0)
83            return EXIT_FAILURE;
84        for(i = 0; i < len; i++)
85            read(fd, data + i, 1);
86        close(fd);
87        break;
88    /* 2x: simple stdio calls
89     * 3x: complex stdio calls */
90    case 20: /* only fread() calls */
91        stream = fopen(name, "r");
92        if(!stream)
93            return EXIT_FAILURE;
94        for(i = 0; i < len; i++)
95            fread(data + i, 1, 1, stream);
96        fclose(stream);
97        break;
98    case 21: /* only getc() calls */
99        stream = fopen(name, "r");
100        if(!stream)
101            return EXIT_FAILURE;
102        for(i = 0; i < len; i++)
103            data[i] = getc(stream);
104        fclose(stream);
105        break;
106    case 22: /* only fgetc() calls */
107        stream = fopen(name, "r");
108        if(!stream)
109            return EXIT_FAILURE;
110        for(i = 0; i < len; i++)
111            data[i] = fgetc(stream);
112        fclose(stream);
113        break;
114    case 30: /* one fread(), then only getc() calls */
115        stream = fopen(name, "r");
116        if(!stream)
117            return EXIT_FAILURE;
118        fread(data, 1, 10, stream);
119        for(i = 10; i < len; i++)
120            data[i] = getc(stream);
121        fclose(stream);
122        break;
123    case 31: /* one fread(), then only fgetc() calls */
124        stream = fopen(name, "r");
125        if(!stream)
126            return EXIT_FAILURE;
127        fread(data, 1, 10, stream);
128        for(i = 10; i < len; i++)
129            data[i] = fgetc(stream);
130        fclose(stream);
131        break;
132    /* 4x: complex, random stuff */
133    case 40: /* socket seeks and reads */
134        fd = open(name, O_RDONLY);
135        if(fd < 0)
136            return EXIT_FAILURE;
137        for(i = 0; i < 128; i++)
138        {
139            lseek(fd, myrand() % len, SEEK_SET);
140            for(j = 0; j < 4; j++)
141                read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
142#ifdef HAVE_LSEEK64
143            lseek64(fd, myrand() % len, SEEK_SET);
144            for(j = 0; j < 4; j++)
145                read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
146#endif
147        }
148        close(fd);
149        break;
150    case 41: /* std streams seeks and reads */
151        stream = fopen(name, "r");
152        if(!stream)
153            return EXIT_FAILURE;
154        for(i = 0; i < 128; i++)
155        {
156            long int now;
157            fseek(stream, myrand() % len, SEEK_SET);
158            for(j = 0; j < 4; j++)
159                fread(data + ftell(stream),
160                      myrand() % (len - ftell(stream)), 1, stream);
161            fseek(stream, myrand() % len, SEEK_SET);
162            now = ftell(stream);
163            for(j = 0; j < 16; j++)
164                data[now + j] = getc(stream);
165            now = ftell(stream);
166            for(j = 0; j < 16; j++)
167                data[now + j] = fgetc(stream);
168        }
169        fclose(stream);
170        break;
171    case 42: /* mmap() */
172        fd = open(name, O_RDONLY);
173        if(fd < 0)
174            return EXIT_FAILURE;
175#ifdef HAVE_MMAP
176        for(i = 0; i < 128; i++)
177        {
178            char *map;
179            int moff, mlen, pgsz = len + 1;
180#ifdef HAVE_GETPAGESIZE
181            pgsz = getpagesize();
182#endif
183            moff = len < pgsz ? 0 : (myrand() % (len / pgsz)) * pgsz;
184            mlen = 1 + (myrand() % (len - moff));
185            map = mmap(NULL, mlen, PROT_READ, MAP_PRIVATE, fd, moff);
186            if(map == MAP_FAILED)
187                return EXIT_FAILURE;
188            for(j = 0; j < 128; j++)
189            {
190                int x = myrand() % mlen;
191                data[moff + x] = map[x];
192            }
193            munmap(map, mlen);
194        }
195#endif
196        close(fd);
197        break;
198    default:
199        return EXIT_FAILURE;
200    }
201
202    /* Write what we have read */
203    fwrite(data, len, 1, stdout);
204    free(data);
205
206    return EXIT_SUCCESS;
207}
208
Note: See TracBrowser for help on using the repository browser.