| 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$ |
|---|
| 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 | char *map; |
|---|
| 116 | int moff, mlen, pgsz = len + 1; |
|---|
| 117 | #ifdef HAVE_GETPAGESIZE |
|---|
| 118 | pgsz = getpagesize(); |
|---|
| 119 | #endif |
|---|
| 120 | moff = len < pgsz ? 0 : (myrand() % (len / pgsz)) * pgsz; |
|---|
| 121 | mlen = 1 + (myrand() % (len - moff)); |
|---|
| 122 | map = mmap(NULL, mlen, PROT_READ, MAP_PRIVATE, fd, moff); |
|---|
| 123 | if(map == MAP_FAILED) |
|---|
| 124 | return EXIT_FAILURE; |
|---|
| 125 | for(j = 0; j < 128; j++) |
|---|
| 126 | { |
|---|
| 127 | int x = myrand() % mlen; |
|---|
| 128 | data[moff + x] = map[x]; |
|---|
| 129 | } |
|---|
| 130 | munmap(map, mlen); |
|---|
| 131 | } |
|---|
| 132 | #endif |
|---|
| 133 | close(fd); |
|---|
| 134 | break; |
|---|
| 135 | default: |
|---|
| 136 | return EXIT_FAILURE; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | /* Write what we have read */ |
|---|
| 140 | fwrite(data, len, 1, stdout); |
|---|
| 141 | free(data); |
|---|
| 142 | |
|---|
| 143 | return EXIT_SUCCESS; |
|---|
| 144 | } |
|---|
| 145 | |
|---|