source: zzuf/trunk/test/streamcat.c @ 1548

Last change on this file since 1548 was 1548, checked in by Sam Hocevar, 16 years ago
  • Added a stupid PRNG to streamcat and fdcat.
  • Do not use sed in the testsuite, because of OS X.
  • Property svn:keywords set to Id
File size: 1.8 KB
Line 
1/*
2 *  streamcat - cat reimplementation
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: streamcat.c 1548 2007-01-03 21:51:11Z 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#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20
21static inline int myrand(void)
22{
23    static int seed = 1;
24    int x, y;
25    x = (seed + 0x12345678) << 11;
26    y = (seed + 0xfedcba98) >> 21;
27    seed = x * 1010101 + y * 343434;
28    return seed;
29}
30
31int main(int argc, char *argv[])
32{
33    long int pos;
34    unsigned char *data;
35    int i, j;
36    FILE *stream;
37
38    if(argc != 2)
39        return EXIT_FAILURE;
40
41    stream = fopen(argv[1], "r");
42    if(!stream)
43        return EXIT_FAILURE;
44
45    fseek(stream, 0, SEEK_END);
46    pos = ftell(stream);
47    if(pos < 0)
48        return EXIT_FAILURE;
49
50    /* Read the whole file */
51    data = malloc(pos + 16); /* 16 safety bytes */
52    fseek(stream, 0, SEEK_SET);
53    fread(data, pos, 1, stream);
54
55    /* Read shit here and there */
56    for(i = 0; i < 128; i++)
57    {
58        long int now;
59        fseek(stream, myrand() % pos, SEEK_SET);
60        for(j = 0; j < 16; j++)
61            fread(data + ftell(stream), myrand() % 4096, 1, stream);
62        fseek(stream, myrand() % pos, SEEK_SET);
63        now = ftell(stream);
64        for(j = 0; j < 16; j++)
65            data[now + j] = getc(stream);
66        now = ftell(stream);
67        for(j = 0; j < 16; j++)
68            data[now + j] = fgetc(stream);
69    }
70
71    fwrite(data, pos, 1, stdout);
72
73    return EXIT_SUCCESS;
74}
75
Note: See TracBrowser for help on using the repository browser.