| [1725] | 1 | /* |
|---|
| 2 | * zzcat - various cat reimplementations for testing purposes |
|---|
| [4240] | 3 | * Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net> |
|---|
| [1725] | 4 | * All Rights Reserved |
|---|
| 5 | * |
|---|
| 6 | * $Id$ |
|---|
| 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 | |
|---|
| [4121] | 15 | /* |
|---|
| 16 | * TODO: fsetpos64, fgetln |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| [1725] | 19 | #include "config.h" |
|---|
| 20 | |
|---|
| [2530] | 21 | /* Needed for lseek64() */ |
|---|
| 22 | #define _LARGEFILE64_SOURCE |
|---|
| 23 | /* Needed for O_RDONLY on HP-UX */ |
|---|
| 24 | #define _INCLUDE_POSIX_SOURCE |
|---|
| [4121] | 25 | /* Needed for fgets_unlocked() */ |
|---|
| 26 | #define _GNU_SOURCE |
|---|
| [1725] | 27 | |
|---|
| [2532] | 28 | #if defined HAVE_STDINT_H |
|---|
| 29 | # include <stdint.h> |
|---|
| 30 | #elif defined HAVE_INTTYPES_H |
|---|
| 31 | # include <inttypes.h> |
|---|
| 32 | #endif |
|---|
| [1725] | 33 | #include <sys/types.h> |
|---|
| 34 | #include <sys/stat.h> |
|---|
| 35 | #include <fcntl.h> |
|---|
| [1728] | 36 | #if defined HAVE_UNISTD_H |
|---|
| 37 | # include <unistd.h> |
|---|
| 38 | #endif |
|---|
| [1745] | 39 | #if defined HAVE_SYS_MMAN_H |
|---|
| 40 | # include <sys/mman.h> |
|---|
| 41 | #endif |
|---|
| [1725] | 42 | #include <stdlib.h> |
|---|
| 43 | #include <stdio.h> |
|---|
| 44 | #include <string.h> |
|---|
| 45 | |
|---|
| 46 | static inline unsigned int myrand(void) |
|---|
| 47 | { |
|---|
| 48 | static int seed = 1; |
|---|
| 49 | int x, y; |
|---|
| 50 | x = (seed + 0x12345678) << 11; |
|---|
| 51 | y = (seed + 0xfedcba98) >> 21; |
|---|
| 52 | seed = x * 1010101 + y * 343434; |
|---|
| 53 | return seed; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| [4243] | 56 | #define MY_FOPEN(cmd) \ |
|---|
| [4121] | 57 | do { \ |
|---|
| 58 | cmd; \ |
|---|
| 59 | if (!f) \ |
|---|
| 60 | { \ |
|---|
| 61 | fprintf(stderr, "E: zzcat: cannot open `%s'\n", file); \ |
|---|
| 62 | return EXIT_FAILURE; \ |
|---|
| 63 | } \ |
|---|
| 64 | retoff = 0; \ |
|---|
| 65 | p = strchr(p, ')') + 1; \ |
|---|
| 66 | } while(0) |
|---|
| 67 | |
|---|
| [4243] | 68 | #define MY_FCLOSE(cmd) \ |
|---|
| [4121] | 69 | do { \ |
|---|
| 70 | cmd; \ |
|---|
| 71 | f = NULL; \ |
|---|
| 72 | p = strchr(p, ')') + 1; \ |
|---|
| 73 | } while(0) |
|---|
| 74 | |
|---|
| 75 | #define MERGE(address, cnt, off) \ |
|---|
| 76 | do { \ |
|---|
| 77 | size_t _cnt = cnt, _off = off; \ |
|---|
| 78 | if (_cnt && retoff + _cnt > retlen) \ |
|---|
| 79 | { \ |
|---|
| 80 | retlen = retoff + _cnt; \ |
|---|
| 81 | retbuf = realloc(retbuf, retlen); \ |
|---|
| 82 | } \ |
|---|
| 83 | if (_cnt > 0) \ |
|---|
| 84 | memcpy(retbuf + retoff, address, _cnt); \ |
|---|
| 85 | retoff += _off; \ |
|---|
| 86 | } while(0) |
|---|
| 87 | |
|---|
| [4243] | 88 | #define MY_FREAD(cmd, buf, cnt) MY_FCALL(cmd, buf, cnt, cnt) |
|---|
| 89 | #define MY_FSEEK(cmd, off) MY_FCALL(cmd, /* unused */ "", 0, off) |
|---|
| [4121] | 90 | |
|---|
| [4243] | 91 | #define MY_FCALL(cmd, buf, cnt, off) \ |
|---|
| [4121] | 92 | do { \ |
|---|
| 93 | if (!f) \ |
|---|
| 94 | { \ |
|---|
| 95 | f = fopen(file, "r"); \ |
|---|
| 96 | if (!f) \ |
|---|
| 97 | { \ |
|---|
| 98 | fprintf(stderr, "E: zzcat: cannot open `%s'\n", file); \ |
|---|
| 99 | return EXIT_FAILURE; \ |
|---|
| 100 | } \ |
|---|
| 101 | } \ |
|---|
| 102 | cmd; \ |
|---|
| 103 | MERGE(buf, cnt, off); \ |
|---|
| 104 | p = strchr(p, ')') + 1; \ |
|---|
| 105 | } while(0) |
|---|
| 106 | |
|---|
| [4243] | 107 | #define MY_FEOF() \ |
|---|
| [4240] | 108 | do { \ |
|---|
| 109 | if (!f) \ |
|---|
| 110 | { \ |
|---|
| 111 | f = fopen(file, "r"); \ |
|---|
| 112 | if (!f) \ |
|---|
| 113 | { \ |
|---|
| 114 | fprintf(stderr, "E: zzcat: cannot open `%s'\n", file); \ |
|---|
| 115 | return EXIT_FAILURE; \ |
|---|
| 116 | } \ |
|---|
| 117 | } \ |
|---|
| 118 | if (feof(f)) \ |
|---|
| 119 | feofs++; \ |
|---|
| 120 | if (feofs >= l1) \ |
|---|
| 121 | finish = 1; \ |
|---|
| 122 | p = strchr(p, ')') + 1; \ |
|---|
| 123 | } while(0) |
|---|
| 124 | |
|---|
| [4121] | 125 | /* |
|---|
| 126 | * Command parser. We rewrite fmt by replacing the last character with |
|---|
| 127 | * '%c' and check that the sscanf() call returns the expected number of |
|---|
| 128 | * matches plus one (for the last character). We use this macro trick to |
|---|
| 129 | * avoid using vsscanf() which does not exist on all platforms. |
|---|
| 130 | */ |
|---|
| 131 | |
|---|
| 132 | struct parser |
|---|
| [4007] | 133 | { |
|---|
| [4121] | 134 | char tmpfmt[1024], ch, lastch; |
|---|
| 135 | }; |
|---|
| 136 | |
|---|
| 137 | static int make_fmt(struct parser *p, char const *fmt) |
|---|
| 138 | { |
|---|
| 139 | char const *tmp; |
|---|
| 140 | size_t len; |
|---|
| 141 | int ret = 0; |
|---|
| 142 | |
|---|
| 143 | len = strlen(fmt); |
|---|
| 144 | p->lastch = fmt[len - 1]; |
|---|
| 145 | |
|---|
| 146 | memcpy(p->tmpfmt, fmt, len - 1); |
|---|
| 147 | p->tmpfmt[len - 1] = '%'; |
|---|
| 148 | p->tmpfmt[len] = 'c'; |
|---|
| 149 | p->tmpfmt[len + 1] = '\0'; |
|---|
| 150 | |
|---|
| 151 | for (tmp = p->tmpfmt; *tmp; tmp++) |
|---|
| 152 | if (*tmp == '%') |
|---|
| 153 | tmp++, ret++; |
|---|
| 154 | |
|---|
| 155 | return ret; |
|---|
| [4007] | 156 | } |
|---|
| 157 | |
|---|
| [4121] | 158 | #define PARSECMD(fmt, arg...) \ |
|---|
| 159 | make_fmt(&parser, fmt) == sscanf(p, parser.tmpfmt, ##arg, &parser.ch) \ |
|---|
| 160 | && parser.ch == parser.lastch |
|---|
| 161 | |
|---|
| 162 | /* |
|---|
| 163 | * File reader. We parse a command line and perform all the operations it |
|---|
| 164 | * contains on the specified file. |
|---|
| 165 | */ |
|---|
| 166 | |
|---|
| 167 | static int cat_file(char const *p, char const *file) |
|---|
| [1725] | 168 | { |
|---|
| [4121] | 169 | struct { char const *p; int count; } loops[128]; |
|---|
| 170 | char *retbuf = NULL, *tmp; |
|---|
| 171 | FILE *f = NULL; |
|---|
| 172 | size_t retlen = 0, retoff = 0; |
|---|
| [4240] | 173 | int nloops = 0, fd = -1, feofs = 0, finish = 0; |
|---|
| [1725] | 174 | |
|---|
| [4121] | 175 | /* Allocate 32MB for our temporary buffer. Any larger value will crash. */ |
|---|
| 176 | tmp = malloc(32 * 1024 * 1024); |
|---|
| [1725] | 177 | |
|---|
| [4121] | 178 | while (*p) |
|---|
| 179 | { |
|---|
| 180 | struct parser parser; |
|---|
| 181 | long int l1, l2; |
|---|
| 182 | char *s, *lineptr = NULL; |
|---|
| 183 | size_t k; |
|---|
| 184 | ssize_t l; |
|---|
| 185 | int n; |
|---|
| 186 | char ch; |
|---|
| [1725] | 187 | |
|---|
| [4243] | 188 | (void)k; |
|---|
| 189 | |
|---|
| [4121] | 190 | /* Ignore punctuation */ |
|---|
| 191 | if (strchr(" \t,;\r\n", *p)) |
|---|
| 192 | p++; |
|---|
| [1725] | 193 | |
|---|
| [4121] | 194 | /* Loop handling */ |
|---|
| 195 | else if (PARSECMD("repeat ( %li ,", &l1)) |
|---|
| 196 | { |
|---|
| 197 | p = strchr(p, ',') + 1; |
|---|
| 198 | loops[nloops].p = p; |
|---|
| 199 | loops[nloops].count = l1; |
|---|
| 200 | nloops++; |
|---|
| 201 | } |
|---|
| 202 | else if (PARSECMD(")")) |
|---|
| 203 | { |
|---|
| 204 | if (nloops == 0) |
|---|
| 205 | { |
|---|
| 206 | fprintf(stderr, "E: zzcat: ')' outside a loop\n"); |
|---|
| 207 | return EXIT_FAILURE; |
|---|
| 208 | } |
|---|
| 209 | loops[nloops - 1].count--; |
|---|
| [4240] | 210 | if (loops[nloops - 1].count <= 0 || finish) |
|---|
| [4121] | 211 | { |
|---|
| 212 | nloops--; |
|---|
| 213 | p = strchr(p, ')') + 1; |
|---|
| 214 | } |
|---|
| 215 | else |
|---|
| 216 | { |
|---|
| 217 | p = loops[nloops - 1].p; |
|---|
| 218 | } |
|---|
| [4240] | 219 | |
|---|
| 220 | finish = 0; |
|---|
| [4121] | 221 | } |
|---|
| 222 | |
|---|
| 223 | /* FILE * opening functions */ |
|---|
| 224 | else if (PARSECMD("fopen ( )")) |
|---|
| [4243] | 225 | MY_FOPEN(f = fopen(file, "r")); |
|---|
| [4121] | 226 | #if defined HAVE_FOPEN64 |
|---|
| 227 | else if (PARSECMD("fopen64 ( )")) |
|---|
| [4243] | 228 | MY_FOPEN(f = fopen64(file, "r")); |
|---|
| [4007] | 229 | #endif |
|---|
| [4121] | 230 | #if defined HAVE___FOPEN64 |
|---|
| 231 | else if (PARSECMD("__fopen64 ( )")) |
|---|
| [4243] | 232 | MY_FOPEN(f = __fopen64(file, "r")); |
|---|
| [4121] | 233 | #endif |
|---|
| 234 | else if (PARSECMD("freopen ( )")) |
|---|
| [4243] | 235 | MY_FOPEN(f = freopen(file, "r", f)); |
|---|
| [4121] | 236 | #if defined HAVE_FREOPEN64 |
|---|
| 237 | else if (PARSECMD("freopen64 ( )")) |
|---|
| [4243] | 238 | MY_FOPEN(f = freopen64(file, "r", f)); |
|---|
| [4121] | 239 | #endif |
|---|
| 240 | #if defined HAVE___FREOPEN64 |
|---|
| 241 | else if (PARSECMD("__freopen64 ( )")) |
|---|
| [4243] | 242 | MY_FOPEN(f = __freopen64(file, "r", f)); |
|---|
| [4121] | 243 | #endif |
|---|
| 244 | |
|---|
| [4240] | 245 | /* FILE * EOF detection */ |
|---|
| 246 | else if (PARSECMD("feof ( %li )", &l1)) |
|---|
| [4243] | 247 | MY_FEOF(); |
|---|
| [4240] | 248 | |
|---|
| [4121] | 249 | /* FILE * closing functions */ |
|---|
| 250 | else if (PARSECMD("fclose ( )")) |
|---|
| [4243] | 251 | MY_FCLOSE(fclose(f)); |
|---|
| [4121] | 252 | |
|---|
| 253 | /* FILE * reading functions */ |
|---|
| 254 | else if (PARSECMD("fread ( %li , %li )", &l1, &l2)) |
|---|
| [4243] | 255 | MY_FREAD(l = fread(tmp, l1, l2, f), tmp, l > 0 ? l * l1 : 0); |
|---|
| [4121] | 256 | else if (PARSECMD("getc ( )")) |
|---|
| [4243] | 257 | MY_FREAD(ch = (n = getc(f)), &ch, (n != EOF)); |
|---|
| [4121] | 258 | else if (PARSECMD("fgetc ( )")) |
|---|
| [4243] | 259 | MY_FREAD(ch = (n = fgetc(f)), &ch, (n != EOF)); |
|---|
| [4121] | 260 | else if (PARSECMD("fgets ( %li )", &l1)) |
|---|
| [4243] | 261 | MY_FREAD(s = fgets(tmp, l1, f), tmp, s ? strlen(tmp) : 0); |
|---|
| [4121] | 262 | #if defined HAVE__IO_GETC |
|---|
| 263 | else if (PARSECMD("_IO_getc ( )")) |
|---|
| [4243] | 264 | MY_FREAD(ch = (n = _IO_getc(f)), &ch, (n != EOF)); |
|---|
| [4121] | 265 | #endif |
|---|
| 266 | #if defined HAVE_FREAD_UNLOCKED |
|---|
| 267 | else if (PARSECMD("fread_unlocked ( %li , %li )", &l1, &l2)) |
|---|
| [4243] | 268 | MY_FREAD(l = fread_unlocked(tmp, l1, l2, f), tmp, l > 0 ? l * l1 : 0); |
|---|
| [4121] | 269 | #endif |
|---|
| 270 | #if defined HAVE_FGETS_UNLOCKED |
|---|
| 271 | else if (PARSECMD("fgets_unlocked ( %li )", &l1)) |
|---|
| [4243] | 272 | MY_FREAD(s = fgets_unlocked(tmp, l1, f), tmp, s ? strlen(tmp) : 0); |
|---|
| [4121] | 273 | #endif |
|---|
| [4002] | 274 | #if defined HAVE_GETC_UNLOCKED |
|---|
| [4121] | 275 | else if (PARSECMD("getc_unlocked ( )")) |
|---|
| [4243] | 276 | MY_FREAD(ch = (n = getc_unlocked(f)), &ch, (n != EOF)); |
|---|
| [4004] | 277 | #endif |
|---|
| [4121] | 278 | #if defined HAVE_FGETC_UNLOCKED |
|---|
| 279 | else if (PARSECMD("fgetc_unlocked ( )")) |
|---|
| [4243] | 280 | MY_FREAD(ch = (n = fgetc_unlocked(f)), &ch, (n != EOF)); |
|---|
| [4004] | 281 | #endif |
|---|
| [4121] | 282 | |
|---|
| 283 | /* FILE * getdelim functions */ |
|---|
| 284 | #if defined HAVE_GETLINE |
|---|
| 285 | else if (PARSECMD("getline ( )")) |
|---|
| [4243] | 286 | MY_FREAD(l = getline(&lineptr, &k, f), lineptr, l >= 0 ? l : 0); |
|---|
| [4121] | 287 | #endif |
|---|
| 288 | #if defined HAVE_GETDELIM |
|---|
| 289 | else if (PARSECMD("getdelim ( '%c' )", &ch)) |
|---|
| [4243] | 290 | MY_FREAD(l = getdelim(&lineptr, &k, ch, f), lineptr, l >= 0 ? l : 0); |
|---|
| [4121] | 291 | else if (PARSECMD("getdelim ( %i )", &n)) |
|---|
| [4243] | 292 | MY_FREAD(l = getdelim(&lineptr, &k, n, f), lineptr, l >= 0 ? l : 0); |
|---|
| [4121] | 293 | #endif |
|---|
| 294 | #if defined HAVE___GETDELIM |
|---|
| 295 | else if (PARSECMD("__getdelim ( '%c' )", &ch)) |
|---|
| [4243] | 296 | MY_FREAD(l = __getdelim(&lineptr, &k, ch, f), lineptr, l >= 0 ? l : 0); |
|---|
| [4121] | 297 | else if (PARSECMD("__getdelim ( %i )", &n)) |
|---|
| [4243] | 298 | MY_FREAD(l = __getdelim(&lineptr, &k, n, f), lineptr, l >= 0 ? l : 0); |
|---|
| [4121] | 299 | #endif |
|---|
| 300 | |
|---|
| 301 | /* FILE * seeking functions */ |
|---|
| 302 | else if (PARSECMD("fseek ( %li , SEEK_CUR )", &l1)) |
|---|
| [4243] | 303 | MY_FSEEK(l = fseek(f, l1, SEEK_CUR), |
|---|
| 304 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
|---|
| [4121] | 305 | else if (PARSECMD("fseek ( %li , SEEK_SET )", &l1)) |
|---|
| [4243] | 306 | MY_FSEEK(l = fseek(f, l1, SEEK_SET), |
|---|
| 307 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
|---|
| [4121] | 308 | else if (PARSECMD("fseek ( %li , SEEK_END )", &l1)) |
|---|
| [4243] | 309 | MY_FSEEK(l = fseek(f, l1, SEEK_END), |
|---|
| 310 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
|---|
| [4121] | 311 | #if defined HAVE_FSEEKO |
|---|
| 312 | else if (PARSECMD("fseeko ( %li , SEEK_CUR )", &l1)) |
|---|
| [4243] | 313 | MY_FSEEK(l = fseeko(f, l1, SEEK_CUR), |
|---|
| 314 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
|---|
| [4121] | 315 | else if (PARSECMD("fseeko ( %li , SEEK_SET )", &l1)) |
|---|
| [4243] | 316 | MY_FSEEK(l = fseeko(f, l1, SEEK_SET), |
|---|
| 317 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
|---|
| [4121] | 318 | else if (PARSECMD("fseeko ( %li , SEEK_END )", &l1)) |
|---|
| [4243] | 319 | MY_FSEEK(l = fseeko(f, l1, SEEK_END), |
|---|
| 320 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
|---|
| [4121] | 321 | #endif |
|---|
| 322 | #if defined HAVE_FSEEKO64 |
|---|
| 323 | else if (PARSECMD("fseeko64 ( %li , SEEK_CUR )", &l1)) |
|---|
| [4243] | 324 | MY_FSEEK(l = fseeko64(f, l1, SEEK_CUR), |
|---|
| 325 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
|---|
| [4121] | 326 | else if (PARSECMD("fseeko64 ( %li , SEEK_SET )", &l1)) |
|---|
| [4243] | 327 | MY_FSEEK(l = fseeko64(f, l1, SEEK_SET), |
|---|
| 328 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
|---|
| [4121] | 329 | else if (PARSECMD("fseeko64 ( %li , SEEK_END )", &l1)) |
|---|
| [4243] | 330 | MY_FSEEK(l = fseeko64(f, l1, SEEK_END), |
|---|
| 331 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
|---|
| [4121] | 332 | #endif |
|---|
| 333 | #if defined HAVE___FSEEKO64 |
|---|
| 334 | else if (PARSECMD("__fseeko64 ( %li , SEEK_CUR )", &l1)) |
|---|
| [4243] | 335 | MY_FSEEK(l = __fseeko64(f, l1, SEEK_CUR), |
|---|
| 336 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
|---|
| [4121] | 337 | else if (PARSECMD("__fseeko64 ( %li , SEEK_SET )", &l1)) |
|---|
| [4243] | 338 | MY_FSEEK(l = __fseeko64(f, l1, SEEK_SET), |
|---|
| 339 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
|---|
| [4121] | 340 | else if (PARSECMD("__fseeko64 ( %li , SEEK_END )", &l1)) |
|---|
| [4243] | 341 | MY_FSEEK(l = __fseeko64(f, l1, SEEK_END), |
|---|
| 342 | ftell(f) >= 0 ? ftell(f) - retoff : 0); |
|---|
| [4121] | 343 | #endif |
|---|
| 344 | else if (PARSECMD("rewind ( )")) |
|---|
| [4243] | 345 | MY_FSEEK(rewind(f), -retlen); |
|---|
| [4121] | 346 | else if (PARSECMD("ungetc ( )")) |
|---|
| [4243] | 347 | MY_FSEEK(if(retoff) ungetc((unsigned char)retbuf[retoff - 1], f), |
|---|
| 348 | retoff ? -1 : 0); |
|---|
| [4121] | 349 | |
|---|
| 350 | /* Unrecognised sequence */ |
|---|
| 351 | else |
|---|
| 352 | { |
|---|
| 353 | char buf[16]; |
|---|
| 354 | snprintf(buf, 16, strlen(p) < 16 ? "%s" : "%.12s...", p); |
|---|
| 355 | fprintf(stderr, "E: zzcat: syntax error near `%s'\n", buf); |
|---|
| 356 | return EXIT_FAILURE; |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | /* Clean up our mess */ |
|---|
| 360 | if (lineptr) |
|---|
| 361 | free(lineptr); |
|---|
| [4240] | 362 | |
|---|
| 363 | if (finish && !nloops) |
|---|
| 364 | break; |
|---|
| [4004] | 365 | } |
|---|
| 366 | |
|---|
| [4121] | 367 | if (f) |
|---|
| 368 | fclose(f); |
|---|
| [4004] | 369 | |
|---|
| [4121] | 370 | if (fd >= 0) |
|---|
| 371 | close(fd); |
|---|
| [4004] | 372 | |
|---|
| [4121] | 373 | fwrite(retbuf, retlen, 1, stdout); |
|---|
| 374 | |
|---|
| 375 | free(retbuf); |
|---|
| 376 | free(tmp); |
|---|
| 377 | |
|---|
| [4004] | 378 | return EXIT_SUCCESS; |
|---|
| 379 | } |
|---|
| 380 | |
|---|
| [4121] | 381 | /* |
|---|
| 382 | * Main program. |
|---|
| 383 | */ |
|---|
| 384 | |
|---|
| 385 | int main(int argc, char *argv[]) |
|---|
| [4004] | 386 | { |
|---|
| 387 | int i; |
|---|
| 388 | |
|---|
| [4121] | 389 | if (argc < 2) |
|---|
| [4028] | 390 | { |
|---|
| [4121] | 391 | fprintf(stderr, "E: zzcat: too few arguments\n"); |
|---|
| [4030] | 392 | return EXIT_FAILURE; |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| [4121] | 395 | if (argc == 2) |
|---|
| 396 | return cat_file("fread(1,33554432)", argv[1]); |
|---|
| 397 | |
|---|
| 398 | for (i = 2; i < argc; i++) |
|---|
| [4004] | 399 | { |
|---|
| [4121] | 400 | int ret = cat_file(argv[1], argv[i]); |
|---|
| 401 | if (ret) |
|---|
| 402 | return ret; |
|---|
| [4004] | 403 | } |
|---|
| 404 | |
|---|
| [4007] | 405 | return EXIT_SUCCESS; |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| [4121] | 408 | #if 0 |
|---|
| 409 | /* Only read() calls */ |
|---|
| 410 | static int zzcat_read(char const *name, unsigned char *data, int64_t len, |
|---|
| 411 | int64_t chunk) |
|---|
| [4004] | 412 | { |
|---|
| [4121] | 413 | int i, fd = open(name, O_RDONLY); |
|---|
| 414 | if(fd < 0) |
|---|
| [4004] | 415 | return EXIT_FAILURE; |
|---|
| [4121] | 416 | for(i = 0; i < len; i += chunk) |
|---|
| 417 | read(fd, data + i, chunk); |
|---|
| 418 | close(fd); |
|---|
| [4004] | 419 | return EXIT_SUCCESS; |
|---|
| 420 | } |
|---|
| 421 | |
|---|
| 422 | /* Socket seeks and reads */ |
|---|
| 423 | static int zzcat_random_socket(char const *name, unsigned char *data, |
|---|
| 424 | int64_t len) |
|---|
| 425 | { |
|---|
| 426 | int i, j, fd = open(name, O_RDONLY); |
|---|
| 427 | if(fd < 0) |
|---|
| 428 | return EXIT_FAILURE; |
|---|
| 429 | for(i = 0; i < 128; i++) |
|---|
| 430 | { |
|---|
| 431 | lseek(fd, myrand() % len, SEEK_SET); |
|---|
| 432 | for(j = 0; j < 4; j++) |
|---|
| 433 | read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096); |
|---|
| [1725] | 434 | #ifdef HAVE_LSEEK64 |
|---|
| [4004] | 435 | lseek64(fd, myrand() % len, SEEK_SET); |
|---|
| 436 | for(j = 0; j < 4; j++) |
|---|
| 437 | read(fd, data + lseek(fd, 0, SEEK_CUR), myrand() % 4096); |
|---|
| [1725] | 438 | #endif |
|---|
| [4004] | 439 | } |
|---|
| 440 | close(fd); |
|---|
| 441 | return EXIT_SUCCESS; |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | /* Standard stream seeks and reads */ |
|---|
| 445 | static int zzcat_random_stream(char const *name, unsigned char *data, |
|---|
| 446 | int64_t len) |
|---|
| 447 | { |
|---|
| 448 | FILE *stream = fopen(name, "r"); |
|---|
| 449 | int i, j; |
|---|
| 450 | if(!stream) |
|---|
| 451 | return EXIT_FAILURE; |
|---|
| 452 | for(i = 0; i < 128; i++) |
|---|
| 453 | { |
|---|
| 454 | long int now; |
|---|
| 455 | fseek(stream, myrand() % len, SEEK_SET); |
|---|
| 456 | for(j = 0; j < 4; j++) |
|---|
| 457 | fread(data + ftell(stream), |
|---|
| 458 | myrand() % (len - ftell(stream)), 1, stream); |
|---|
| 459 | fseek(stream, myrand() % len, SEEK_SET); |
|---|
| 460 | now = ftell(stream); |
|---|
| 461 | for(j = 0; j < 16; j++) |
|---|
| 462 | data[now + j] = getc(stream); |
|---|
| 463 | now = ftell(stream); |
|---|
| 464 | for(j = 0; j < 16; j++) |
|---|
| 465 | data[now + j] = fgetc(stream); |
|---|
| 466 | } |
|---|
| 467 | fclose(stream); |
|---|
| 468 | return EXIT_SUCCESS; |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| [1745] | 471 | #ifdef HAVE_MMAP |
|---|
| [4004] | 472 | /* mmap() followed by random memory reads */ |
|---|
| 473 | static int zzcat_random_mmap(char const *name, unsigned char *data, |
|---|
| 474 | int64_t len) |
|---|
| 475 | { |
|---|
| 476 | int i, j, fd = open(name, O_RDONLY); |
|---|
| 477 | if(fd < 0) |
|---|
| 478 | return EXIT_FAILURE; |
|---|
| 479 | for(i = 0; i < 128; i++) |
|---|
| 480 | { |
|---|
| 481 | char *map; |
|---|
| 482 | int moff, mlen, pgsz = len + 1; |
|---|
| [1747] | 483 | #ifdef HAVE_GETPAGESIZE |
|---|
| [4004] | 484 | pgsz = getpagesize(); |
|---|
| [1747] | 485 | #endif |
|---|
| [4004] | 486 | moff = len < pgsz ? 0 : (myrand() % (len / pgsz)) * pgsz; |
|---|
| 487 | mlen = 1 + (myrand() % (len - moff)); |
|---|
| 488 | map = mmap(NULL, mlen, PROT_READ, MAP_PRIVATE, fd, moff); |
|---|
| 489 | if(map == MAP_FAILED) |
|---|
| 490 | return EXIT_FAILURE; |
|---|
| 491 | for(j = 0; j < 128; j++) |
|---|
| 492 | { |
|---|
| 493 | int x = myrand() % mlen; |
|---|
| 494 | data[moff + x] = map[x]; |
|---|
| [1745] | 495 | } |
|---|
| [4004] | 496 | munmap(map, mlen); |
|---|
| [1725] | 497 | } |
|---|
| [4004] | 498 | close(fd); |
|---|
| [1725] | 499 | return EXIT_SUCCESS; |
|---|
| 500 | } |
|---|
| [4004] | 501 | #endif |
|---|
| [4121] | 502 | #endif |
|---|
| [1725] | 503 | |
|---|