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