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

Last change on this file since 1521 was 1521, checked in by Sam Hocevar, 14 years ago
  • Added cat reimplementations to the testsuite.
  • Property svn:keywords set to Id
File size: 1.6 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 1521 2006-12-28 17:47:26Z 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
21int main(int argc, char *argv[])
22{
23    long int pos;
24    unsigned char *data;
25    int i, j;
26    FILE *stream;
27
28    srand(0);
29
30    if(argc != 2)
31        return EXIT_FAILURE;
32
33    stream = fopen(argv[1], "r");
34    if(!stream)
35        return EXIT_FAILURE;
36
37    fseek(stream, 0, SEEK_END);
38    pos = ftell(stream);
39    if(pos < 0)
40        return EXIT_FAILURE;
41
42    /* Read the whole file */
43    data = malloc(pos + 16); /* 16 safety bytes */
44    fseek(stream, 0, SEEK_SET);
45    fread(data, pos, 1, stream);
46
47    /* Read shit here and there */
48    for(i = 0; i < 128; i++)
49    {
50        long int now;
51        fseek(stream, rand() % pos, SEEK_SET);
52        for(j = 0; j < 16; j++)
53            fread(data + ftell(stream), rand() % 4096, 1, stream);
54        fseek(stream, rand() % pos, SEEK_SET);
55        now = ftell(stream);
56        for(j = 0; j < 16; j++)
57            data[now + j] = getc(stream);
58        now = ftell(stream);
59        for(j = 0; j < 16; j++)
60            data[now + j] = fgetc(stream);
61    }
62
63    fwrite(data, pos, 1, stdout);
64
65    return EXIT_SUCCESS;
66}
67
Note: See TracBrowser for help on using the repository browser.