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

Last change on this file since 1725 was 1725, checked in by Sam Hocevar, 16 years ago
  • Merged fdcat and streamcat into zzcat.
  • Property svn:keywords set to Id
File size: 2.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 1725 2007-01-28 00:53:19Z 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#define _LARGEFILE64_SOURCE /* for lseek64() */
18
19#include <sys/types.h>
20#include <sys/stat.h>
21#include <fcntl.h>
22#include <unistd.h>
23#include <stdlib.h>
24#include <stdint.h>
25#include <stdio.h>
26#include <string.h>
27
28static inline unsigned int myrand(void)
29{
30    static int seed = 1;
31    int x, y;
32    x = (seed + 0x12345678) << 11;
33    y = (seed + 0xfedcba98) >> 21;
34    seed = x * 1010101 + y * 343434;
35    return seed;
36}
37
38int main(int argc, char *argv[])
39{
40    int64_t len;
41    unsigned char *data;
42    char const *name;
43    FILE *stream;
44    int i, j, fd;
45
46    if(argc != 3)
47        return EXIT_FAILURE;
48
49    name = argv[2];
50
51    /* Read the whole file */
52    fd = open(name, O_RDONLY);
53    if(fd < 0)
54        return EXIT_FAILURE;
55    len = lseek(fd, 0, SEEK_END);
56    if(len < 0)
57        return EXIT_FAILURE;
58    data = malloc(len + 16); /* 16 safety bytes */
59    lseek(fd, 0, SEEK_SET);
60    read(fd, data, len);
61    close(fd);
62
63    /* Read shit here and there, using different methods */
64    switch(atoi(argv[1]))
65    {
66    case 1: /* socket seeks and reads */
67        fd = open(name, O_RDONLY);
68        if(fd < 0)
69            return EXIT_FAILURE;
70        for(i = 0; i < 128; i++)
71        {
72            lseek(fd, myrand() % len, SEEK_SET);
73            for(j = 0; j < 4; j++)
74                read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
75#ifdef HAVE_LSEEK64
76            lseek64(fd, myrand() % len, SEEK_SET);
77            for(j = 0; j < 4; j++)
78                read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096);
79#endif
80        }
81        close(fd);
82        break;
83    case 2: /* std streams seeks and reads */
84        stream = fopen(name, "r");
85        if(!stream)
86            return EXIT_FAILURE;
87        for(i = 0; i < 128; i++)
88        {
89            long int now;
90            fseek(stream, myrand() % len, SEEK_SET);
91            for(j = 0; j < 4; j++)
92                fread(data + ftell(stream), myrand() % 4096, 1, stream);
93            fseek(stream, myrand() % len, SEEK_SET);
94            now = ftell(stream);
95            for(j = 0; j < 16; j++)
96                data[now + j] = getc(stream);
97            now = ftell(stream);
98            for(j = 0; j < 16; j++)
99                data[now + j] = fgetc(stream);
100        }
101        fclose(stream);
102        break;
103    }
104
105    /* Write what we have read */
106    fwrite(data, len, 1, stdout);
107    free(data);
108
109    return EXIT_SUCCESS;
110}
111
Note: See TracBrowser for help on using the repository browser.