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

Last change on this file since 2530 was 2530, checked in by Sam Hocevar, 15 years ago
  • Add a few _INCLUDE_POSIX_SOURCE defines here and there so that we build out of the box with HP-UX's c99 compiler.
  • Property svn:keywords set to Id
File size: 3.8 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 2530 2008-07-15 21:34:14Z 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#include <sys/types.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25#if defined HAVE_UNISTD_H
26#   include <unistd.h>
27#endif
28#if defined HAVE_SYS_MMAN_H
29#   include <sys/mman.h>
30#endif
31#include <stdlib.h>
32#include <stdint.h>
33#include <stdio.h>
34#include <string.h>
35
36static inline unsigned int myrand(void)
37{
38    static int seed = 1;
39    int x, y;
40    x = (seed + 0x12345678) << 11;
41    y = (seed + 0xfedcba98) >> 21;
42    seed = x * 1010101 + y * 343434;
43    return seed;
44}
45
46int main(int argc, char *argv[])
47{
48    int64_t len;
49    unsigned char *data;
50    char const *name;
51    FILE *stream;
52    int i, j, fd;
53
54    if(argc != 3)
55        return EXIT_FAILURE;
56
57    name = argv[2];
58
59    /* Read the whole file */
60    fd = open(name, O_RDONLY);
61    if(fd < 0)
62        return EXIT_FAILURE;
63    len = lseek(fd, 0, SEEK_END);
64    if(len < 0)
65        return EXIT_FAILURE;
66    data = malloc(len + 16); /* 16 safety bytes */
67    lseek(fd, 0, SEEK_SET);
68    read(fd, data, len);
69    close(fd);
70
71    /* Read shit here and there, using different methods */
72    switch(atoi(argv[1]))
73    {
74    case 1: /* socket seeks and reads */
75        fd = open(name, O_RDONLY);
76        if(fd < 0)
77            return EXIT_FAILURE;
78        for(i = 0; i < 128; i++)
79        {
80            lseek(fd, myrand() % len, SEEK_SET);
81            for(j = 0; j < 4; j++)
82                read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
83#ifdef HAVE_LSEEK64
84            lseek64(fd, myrand() % len, SEEK_SET);
85            for(j = 0; j < 4; j++)
86                read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
87#endif
88        }
89        close(fd);
90        break;
91    case 2: /* std streams seeks and reads */
92        stream = fopen(name, "r");
93        if(!stream)
94            return EXIT_FAILURE;
95        for(i = 0; i < 128; i++)
96        {
97            long int now;
98            fseek(stream, myrand() % len, SEEK_SET);
99            for(j = 0; j < 4; j++)
100                fread(data + ftell(stream), myrand() % 4096, 1, stream);
101            fseek(stream, myrand() % len, SEEK_SET);
102            now = ftell(stream);
103            for(j = 0; j < 16; j++)
104                data[now + j] = getc(stream);
105            now = ftell(stream);
106            for(j = 0; j < 16; j++)
107                data[now + j] = fgetc(stream);
108        }
109        fclose(stream);
110        break;
111    case 3: /* mmap() */
112        fd = open(name, O_RDONLY);
113        if(fd < 0)
114            return EXIT_FAILURE;
115#ifdef HAVE_MMAP
116        for(i = 0; i < 128; i++)
117        {
118            char *map;
119            int moff, mlen, pgsz = len + 1;
120#ifdef HAVE_GETPAGESIZE
121            pgsz = getpagesize();
122#endif
123            moff = len < pgsz ? 0 : (myrand() % (len / pgsz)) * pgsz;
124            mlen = 1 + (myrand() % (len - moff));
125            map = mmap(NULL, mlen, PROT_READ, MAP_PRIVATE, fd, moff);
126            if(map == MAP_FAILED)
127                return EXIT_FAILURE;
128            for(j = 0; j < 128; j++)
129            {
130                int x = myrand() % mlen;
131                data[moff + x] = map[x];
132            }
133            munmap(map, mlen);
134        }
135#endif
136        close(fd);
137        break;
138    default:
139        return EXIT_FAILURE;
140    }
141
142    /* Write what we have read */
143    fwrite(data, len, 1, stdout);
144    free(data);
145
146    return EXIT_SUCCESS;
147}
148
Note: See TracBrowser for help on using the repository browser.