1 | /* |
---|
2 | * fdcat - cat reimplementation |
---|
3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: fdcat.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 | #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 | |
---|
27 | static inline int myrand(void) |
---|
28 | { |
---|
29 | static int seed = 1; |
---|
30 | int x, y; |
---|
31 | x = (seed + 0x12345678) << 11; |
---|
32 | y = (seed + 0xfedcba98) >> 21; |
---|
33 | seed = x * 1010101 + y * 343434; |
---|
34 | return seed; |
---|
35 | } |
---|
36 | |
---|
37 | int main(int argc, char *argv[]) |
---|
38 | { |
---|
39 | long int pos; |
---|
40 | unsigned char *data; |
---|
41 | int i, j, fd; |
---|
42 | |
---|
43 | if(argc != 2) |
---|
44 | return EXIT_FAILURE; |
---|
45 | |
---|
46 | fd = open(argv[1], O_RDONLY); |
---|
47 | if(fd < 0) |
---|
48 | return EXIT_FAILURE; |
---|
49 | |
---|
50 | pos = lseek(fd, 0, SEEK_END); |
---|
51 | if(pos < 0) |
---|
52 | return EXIT_FAILURE; |
---|
53 | |
---|
54 | /* Read the whole file */ |
---|
55 | data = malloc(pos); |
---|
56 | lseek(fd, 0, SEEK_SET); |
---|
57 | read(fd, data, pos); |
---|
58 | |
---|
59 | /* Read shit here and there */ |
---|
60 | for(i = 0; i < 128; i++) |
---|
61 | { |
---|
62 | lseek(fd, myrand() % pos, SEEK_SET); |
---|
63 | for(j = 0; j < 16; j++) |
---|
64 | read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096); |
---|
65 | #ifdef HAVE_LSEEK64 |
---|
66 | lseek64(fd, myrand() % pos, SEEK_SET); |
---|
67 | for(j = 0; j < 16; j++) |
---|
68 | read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096); |
---|
69 | #endif |
---|
70 | } |
---|
71 | |
---|
72 | fwrite(data, pos, 1, stdout); |
---|
73 | |
---|
74 | return EXIT_SUCCESS; |
---|
75 | } |
---|
76 | |
---|