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

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