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 1746 2007-02-19 09:28:52Z 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 | #if defined HAVE_SYS_MMAN_H |
---|
26 | # include <sys/mman.h> |
---|
27 | #endif |
---|
28 | #include <stdlib.h> |
---|
29 | #include <stdint.h> |
---|
30 | #include <stdio.h> |
---|
31 | #include <string.h> |
---|
32 | |
---|
33 | static inline unsigned int myrand(void) |
---|
34 | { |
---|
35 | static int seed = 1; |
---|
36 | int x, y; |
---|
37 | x = (seed + 0x12345678) << 11; |
---|
38 | y = (seed + 0xfedcba98) >> 21; |
---|
39 | seed = x * 1010101 + y * 343434; |
---|
40 | return seed; |
---|
41 | } |
---|
42 | |
---|
43 | int main(int argc, char *argv[]) |
---|
44 | { |
---|
45 | int64_t len; |
---|
46 | unsigned char *data; |
---|
47 | char const *name; |
---|
48 | FILE *stream; |
---|
49 | int i, j, fd; |
---|
50 | |
---|
51 | if(argc != 3) |
---|
52 | return EXIT_FAILURE; |
---|
53 | |
---|
54 | name = argv[2]; |
---|
55 | |
---|
56 | /* Read the whole file */ |
---|
57 | fd = open(name, O_RDONLY); |
---|
58 | if(fd < 0) |
---|
59 | return EXIT_FAILURE; |
---|
60 | len = lseek(fd, 0, SEEK_END); |
---|
61 | if(len < 0) |
---|
62 | return EXIT_FAILURE; |
---|
63 | data = malloc(len + 16); /* 16 safety bytes */ |
---|
64 | lseek(fd, 0, SEEK_SET); |
---|
65 | read(fd, data, len); |
---|
66 | close(fd); |
---|
67 | |
---|
68 | /* Read shit here and there, using different methods */ |
---|
69 | switch(atoi(argv[1])) |
---|
70 | { |
---|
71 | case 1: /* socket seeks and reads */ |
---|
72 | fd = open(name, O_RDONLY); |
---|
73 | if(fd < 0) |
---|
74 | return EXIT_FAILURE; |
---|
75 | for(i = 0; i < 128; i++) |
---|
76 | { |
---|
77 | lseek(fd, myrand() % len, SEEK_SET); |
---|
78 | for(j = 0; j < 4; j++) |
---|
79 | read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096); |
---|
80 | #ifdef HAVE_LSEEK64 |
---|
81 | lseek64(fd, myrand() % len, SEEK_SET); |
---|
82 | for(j = 0; j < 4; j++) |
---|
83 | read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096); |
---|
84 | #endif |
---|
85 | } |
---|
86 | close(fd); |
---|
87 | break; |
---|
88 | case 2: /* std streams seeks and reads */ |
---|
89 | stream = fopen(name, "r"); |
---|
90 | if(!stream) |
---|
91 | return EXIT_FAILURE; |
---|
92 | for(i = 0; i < 128; i++) |
---|
93 | { |
---|
94 | long int now; |
---|
95 | fseek(stream, myrand() % len, SEEK_SET); |
---|
96 | for(j = 0; j < 4; j++) |
---|
97 | fread(data + ftell(stream), myrand() % 4096, 1, stream); |
---|
98 | fseek(stream, myrand() % len, SEEK_SET); |
---|
99 | now = ftell(stream); |
---|
100 | for(j = 0; j < 16; j++) |
---|
101 | data[now + j] = getc(stream); |
---|
102 | now = ftell(stream); |
---|
103 | for(j = 0; j < 16; j++) |
---|
104 | data[now + j] = fgetc(stream); |
---|
105 | } |
---|
106 | fclose(stream); |
---|
107 | break; |
---|
108 | case 3: /* mmap() */ |
---|
109 | fd = open(name, O_RDONLY); |
---|
110 | if(fd < 0) |
---|
111 | return EXIT_FAILURE; |
---|
112 | #ifdef HAVE_MMAP |
---|
113 | for(i = 0; i < 128; i++) |
---|
114 | { |
---|
115 | int moff = myrand() % len; |
---|
116 | int mlen = myrand() % (len - moff); |
---|
117 | char *map = mmap(NULL, mlen, PROT_READ, MAP_PRIVATE, fd, moff); |
---|
118 | for(j = 0; j < 128; j++) |
---|
119 | { |
---|
120 | int x = myrand() % mlen; |
---|
121 | data[moff + x] = data[x]; |
---|
122 | } |
---|
123 | munmap(map, mlen); |
---|
124 | } |
---|
125 | #endif |
---|
126 | close(fd); |
---|
127 | } |
---|
128 | |
---|
129 | /* Write what we have read */ |
---|
130 | fwrite(data, len, 1, stdout); |
---|
131 | free(data); |
---|
132 | |
---|
133 | return EXIT_SUCCESS; |
---|
134 | } |
---|
135 | |
---|