source: zzuf/trunk/test/fdcat.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.5 KB
Line 
1/*
2 *  fdcat - cat reimplementation
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: fdcat.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#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 <stdio.h>
25#include <string.h>
26
27int main(int argc, char *argv[])
28{
29    long int pos;
30    unsigned char *data;
31    int i, j, fd;
32
33    srand(0);
34
35    if(argc != 2)
36        return EXIT_FAILURE;
37
38    fd = open(argv[1], O_RDONLY);
39    if(fd < 0)
40        return EXIT_FAILURE;
41
42    pos = lseek(fd, 0, SEEK_END);
43    if(pos < 0)
44        return EXIT_FAILURE;
45
46    /* Read the whole file */
47    data = malloc(pos);
48    lseek(fd, 0, SEEK_SET);
49    read(fd, data, pos);
50
51    /* Read shit here and there */
52    for(i = 0; i < 128; i++)
53    {
54        lseek(fd, rand() % pos, SEEK_SET);
55        for(j = 0; j < 16; j++)
56            read(fd, data + lseek(fd, 0, SEEK_CUR), rand() % 4096);
57        lseek64(fd, rand() % pos, SEEK_SET);
58        for(j = 0; j < 16; j++)
59            read(fd, data + lseek(fd, 0, SEEK_CUR), rand() % 4096);
60    }
61
62    fwrite(data, pos, 1, stdout);
63
64    return EXIT_SUCCESS;
65}
66
Note: See TracBrowser for help on using the repository browser.