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 2561 2008-07-18 09:27:56Z 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 | /* Needed for lseek64() */ |
---|
18 | #define _LARGEFILE64_SOURCE |
---|
19 | /* Needed for O_RDONLY on HP-UX */ |
---|
20 | #define _INCLUDE_POSIX_SOURCE |
---|
21 | |
---|
22 | #if defined HAVE_STDINT_H |
---|
23 | # include <stdint.h> |
---|
24 | #elif defined HAVE_INTTYPES_H |
---|
25 | # include <inttypes.h> |
---|
26 | #endif |
---|
27 | #include <sys/types.h> |
---|
28 | #include <sys/stat.h> |
---|
29 | #include <fcntl.h> |
---|
30 | #if defined HAVE_UNISTD_H |
---|
31 | # include <unistd.h> |
---|
32 | #endif |
---|
33 | #if defined HAVE_SYS_MMAN_H |
---|
34 | # include <sys/mman.h> |
---|
35 | #endif |
---|
36 | #include <stdlib.h> |
---|
37 | #include <stdio.h> |
---|
38 | #include <string.h> |
---|
39 | |
---|
40 | static inline unsigned int myrand(void) |
---|
41 | { |
---|
42 | static int seed = 1; |
---|
43 | int x, y; |
---|
44 | x = (seed + 0x12345678) << 11; |
---|
45 | y = (seed + 0xfedcba98) >> 21; |
---|
46 | seed = x * 1010101 + y * 343434; |
---|
47 | return seed; |
---|
48 | } |
---|
49 | |
---|
50 | int main(int argc, char *argv[]) |
---|
51 | { |
---|
52 | int64_t len; |
---|
53 | unsigned char *data; |
---|
54 | char const *name; |
---|
55 | FILE *stream; |
---|
56 | int i, j, fd; |
---|
57 | |
---|
58 | if(argc != 3) |
---|
59 | return EXIT_FAILURE; |
---|
60 | |
---|
61 | name = argv[2]; |
---|
62 | |
---|
63 | /* Read the whole file */ |
---|
64 | fd = open(name, O_RDONLY); |
---|
65 | if(fd < 0) |
---|
66 | return EXIT_FAILURE; |
---|
67 | len = lseek(fd, 0, SEEK_END); |
---|
68 | if(len < 0) |
---|
69 | return EXIT_FAILURE; |
---|
70 | data = malloc(len + 16); /* 16 safety bytes */ |
---|
71 | lseek(fd, 0, SEEK_SET); |
---|
72 | read(fd, data, len); |
---|
73 | close(fd); |
---|
74 | |
---|
75 | /* Read shit here and there, using different methods */ |
---|
76 | switch(atoi(argv[1])) |
---|
77 | { |
---|
78 | case 0: /* only read() calls */ |
---|
79 | fd = open(name, O_RDONLY); |
---|
80 | if(fd < 0) |
---|
81 | return EXIT_FAILURE; |
---|
82 | for(i = 0; i < len; i++) |
---|
83 | read(fd, data + i, 1); |
---|
84 | close(fd); |
---|
85 | break; |
---|
86 | case 10: /* only fread() calls */ |
---|
87 | stream = fopen(name, "r"); |
---|
88 | if(!stream) |
---|
89 | return EXIT_FAILURE; |
---|
90 | for(i = 0; i < len; i++) |
---|
91 | fread(data + i, 1, 1, stream); |
---|
92 | fclose(stream); |
---|
93 | break; |
---|
94 | case 11: /* only getc() calls */ |
---|
95 | stream = fopen(name, "r"); |
---|
96 | if(!stream) |
---|
97 | return EXIT_FAILURE; |
---|
98 | for(i = 0; i < len; i++) |
---|
99 | data[i] = getc(stream); |
---|
100 | fclose(stream); |
---|
101 | break; |
---|
102 | case 12: /* only fgetc() calls */ |
---|
103 | stream = fopen(name, "r"); |
---|
104 | if(!stream) |
---|
105 | return EXIT_FAILURE; |
---|
106 | for(i = 0; i < len; i++) |
---|
107 | data[i] = fgetc(stream); |
---|
108 | fclose(stream); |
---|
109 | break; |
---|
110 | case 20: /* socket seeks and reads */ |
---|
111 | fd = open(name, O_RDONLY); |
---|
112 | if(fd < 0) |
---|
113 | return EXIT_FAILURE; |
---|
114 | for(i = 0; i < 128; i++) |
---|
115 | { |
---|
116 | lseek(fd, myrand() % len, SEEK_SET); |
---|
117 | for(j = 0; j < 4; j++) |
---|
118 | read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096); |
---|
119 | #ifdef HAVE_LSEEK64 |
---|
120 | lseek64(fd, myrand() % len, SEEK_SET); |
---|
121 | for(j = 0; j < 4; j++) |
---|
122 | read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096); |
---|
123 | #endif |
---|
124 | } |
---|
125 | close(fd); |
---|
126 | break; |
---|
127 | case 21: /* std streams seeks and reads */ |
---|
128 | stream = fopen(name, "r"); |
---|
129 | if(!stream) |
---|
130 | return EXIT_FAILURE; |
---|
131 | for(i = 0; i < 128; i++) |
---|
132 | { |
---|
133 | long int now; |
---|
134 | fseek(stream, myrand() % len, SEEK_SET); |
---|
135 | for(j = 0; j < 4; j++) |
---|
136 | fread(data + ftell(stream), |
---|
137 | myrand() % (len - ftell(stream)), 1, stream); |
---|
138 | fseek(stream, myrand() % len, SEEK_SET); |
---|
139 | now = ftell(stream); |
---|
140 | for(j = 0; j < 16; j++) |
---|
141 | data[now + j] = getc(stream); |
---|
142 | now = ftell(stream); |
---|
143 | for(j = 0; j < 16; j++) |
---|
144 | data[now + j] = fgetc(stream); |
---|
145 | } |
---|
146 | fclose(stream); |
---|
147 | break; |
---|
148 | case 22: /* mmap() */ |
---|
149 | fd = open(name, O_RDONLY); |
---|
150 | if(fd < 0) |
---|
151 | return EXIT_FAILURE; |
---|
152 | #ifdef HAVE_MMAP |
---|
153 | for(i = 0; i < 128; i++) |
---|
154 | { |
---|
155 | char *map; |
---|
156 | int moff, mlen, pgsz = len + 1; |
---|
157 | #ifdef HAVE_GETPAGESIZE |
---|
158 | pgsz = getpagesize(); |
---|
159 | #endif |
---|
160 | moff = len < pgsz ? 0 : (myrand() % (len / pgsz)) * pgsz; |
---|
161 | mlen = 1 + (myrand() % (len - moff)); |
---|
162 | map = mmap(NULL, mlen, PROT_READ, MAP_PRIVATE, fd, moff); |
---|
163 | if(map == MAP_FAILED) |
---|
164 | return EXIT_FAILURE; |
---|
165 | for(j = 0; j < 128; j++) |
---|
166 | { |
---|
167 | int x = myrand() % mlen; |
---|
168 | data[moff + x] = map[x]; |
---|
169 | } |
---|
170 | munmap(map, mlen); |
---|
171 | } |
---|
172 | #endif |
---|
173 | close(fd); |
---|
174 | break; |
---|
175 | default: |
---|
176 | return EXIT_FAILURE; |
---|
177 | } |
---|
178 | |
---|
179 | /* Write what we have read */ |
---|
180 | fwrite(data, len, 1, stdout); |
---|
181 | free(data); |
---|
182 | |
---|
183 | return EXIT_SUCCESS; |
---|
184 | } |
---|
185 | |
---|