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

Last change on this file since 2532 was 2532, checked in by Sam Hocevar, 12 years ago
  • zzcat.c: compilation fix for missing headers.
  • Property svn:keywords set to Id
File size: 3.9 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 2532 2008-07-16 09:51:58Z 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    case 1: /* socket seeks and reads */
79        fd = open(name, O_RDONLY);
80        if(fd < 0)
81            return EXIT_FAILURE;
82        for(i = 0; i < 128; i++)
83        {
84            lseek(fd, myrand() % len, SEEK_SET);
85            for(j = 0; j < 4; j++)
86                read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
87#ifdef HAVE_LSEEK64
88            lseek64(fd, myrand() % len, SEEK_SET);
89            for(j = 0; j < 4; j++)
90                read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
91#endif
92        }
93        close(fd);
94        break;
95    case 2: /* std streams seeks and reads */
96        stream = fopen(name, "r");
97        if(!stream)
98            return EXIT_FAILURE;
99        for(i = 0; i < 128; i++)
100        {
101            long int now;
102            fseek(stream, myrand() % len, SEEK_SET);
103            for(j = 0; j < 4; j++)
104                fread(data + ftell(stream), myrand() % 4096, 1, stream);
105            fseek(stream, myrand() % len, SEEK_SET);
106            now = ftell(stream);
107            for(j = 0; j < 16; j++)
108                data[now + j] = getc(stream);
109            now = ftell(stream);
110            for(j = 0; j < 16; j++)
111                data[now + j] = fgetc(stream);
112        }
113        fclose(stream);
114        break;
115    case 3: /* mmap() */
116        fd = open(name, O_RDONLY);
117        if(fd < 0)
118            return EXIT_FAILURE;
119#ifdef HAVE_MMAP
120        for(i = 0; i < 128; i++)
121        {
122            char *map;
123            int moff, mlen, pgsz = len + 1;
124#ifdef HAVE_GETPAGESIZE
125            pgsz = getpagesize();
126#endif
127            moff = len < pgsz ? 0 : (myrand() % (len / pgsz)) * pgsz;
128            mlen = 1 + (myrand() % (len - moff));
129            map = mmap(NULL, mlen, PROT_READ, MAP_PRIVATE, fd, moff);
130            if(map == MAP_FAILED)
131                return EXIT_FAILURE;
132            for(j = 0; j < 128; j++)
133            {
134                int x = myrand() % mlen;
135                data[moff + x] = map[x];
136            }
137            munmap(map, mlen);
138        }
139#endif
140        close(fd);
141        break;
142    default:
143        return EXIT_FAILURE;
144    }
145
146    /* Write what we have read */
147    fwrite(data, len, 1, stdout);
148    free(data);
149
150    return EXIT_SUCCESS;
151}
152
Note: See TracBrowser for help on using the repository browser.