nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it would be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | * |
| 12 | * Further, this software is distributed without any warranty that it is |
| 13 | * free of the rightful claim of any third person regarding infringement |
| 14 | * or the like. Any license provided herein, whether implied or |
| 15 | * otherwise, applies only to this software file. Patent licenses, if |
| 16 | * any, provided herein do not apply to combinations of this program with |
| 17 | * other software, or any other product whatsoever. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 22 | * |
| 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
| 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
| 30 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 31 | * |
| 32 | */ |
nstraz | a700ef5 | 2002-09-16 15:02:57 +0000 | [diff] [blame] | 33 | /* $Id: rand_lines.c,v 1.5 2002/09/16 15:02:57 nstraz Exp $ */ |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 34 | /************************************************************** |
| 35 | * |
| 36 | * OS Testing - Silicon Graphics, Inc. |
| 37 | * |
| 38 | * TOOL IDENTIFIER : rand_lines |
| 39 | * |
| 40 | * DESCRIPTION : prints lines from a file in random order |
| 41 | * |
| 42 | * SYNOPSIS: |
| 43 | * rand_line [-hg][-S seed][-l numlines] [files...] |
| 44 | * |
| 45 | * AUTHOR : Richard Logan |
| 46 | * |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 47 | * CO-PILOT(s) : |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 48 | * |
| 49 | * DATE STARTED : 05/94 |
| 50 | * |
| 51 | * INPUT SPECIFICATIONS |
| 52 | * This tool will print lines of a file in random order. |
nstraz | 9d10051 | 2002-06-20 19:03:00 +0000 | [diff] [blame] | 53 | * The max line length is 4096. |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 54 | * The options supported are: |
| 55 | * -h This option prints an help message then exits. |
| 56 | * |
| 57 | * -g This option specifies to count the number of lines |
| 58 | * in the file before randomizing. This option overrides |
| 59 | * -l option. Using this option, will give you the best |
| 60 | * randomization, but it requires processing |
| 61 | * the file an additional time. |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 62 | * |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 63 | * -l numlines : This option specifies to randomize file in |
nstraz | 9d10051 | 2002-06-20 19:03:00 +0000 | [diff] [blame] | 64 | * numlines chucks. The default size is 4096. |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 65 | * |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 66 | * -S seed : sets randomization seed to seed. |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 67 | * The default is time(0). If seed is zero, time(0) is used. |
| 68 | * |
| 69 | * file A readable, seekable filename. The cmd allows the user |
| 70 | * to specify multiple files, but each file is dealt with |
| 71 | * separately. |
| 72 | * |
| 73 | * DESIGN DESCRIPTION |
| 74 | * This tool uses a simple algorithm where the file is read. |
| 75 | * The offset to the each line is randomly placed into an |
| 76 | * array. The array is then processed sequentially. The infile's |
| 77 | * line who's offset in the array element is thus reread then printed. |
| 78 | * This output will thus be infile's lines in random order. |
| 79 | * |
| 80 | * SPECIAL REQUIREMENTS |
| 81 | * None. |
| 82 | * |
| 83 | * UPDATE HISTORY |
| 84 | * This should contain the description, author, and date of any |
| 85 | * "interesting" modifications (i.e. info should helpful in |
| 86 | * maintaining/enhancing this tool). |
| 87 | * username description |
| 88 | * ---------------------------------------------------------------- |
| 89 | * rrl Creatation of program |
nstraz | 9d10051 | 2002-06-20 19:03:00 +0000 | [diff] [blame] | 90 | * rrl 06/02 Fixed bug and some cleanup. Changed default chunk |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 91 | * and line size to 4096 characters. |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 92 | * |
| 93 | * BUGS/LIMITATIONS |
| 94 | * This program can not deal with non-seekable file like |
| 95 | * stdin or a pipe. If more than one file is specified, |
| 96 | * each file is randomized one at a time. The max line |
nstraz | 9d10051 | 2002-06-20 19:03:00 +0000 | [diff] [blame] | 97 | * length is 4096 characters. |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 98 | * |
| 99 | **************************************************************/ |
| 100 | |
Garrett Cooper | 8346631 | 2011-02-15 06:39:09 -0800 | [diff] [blame] | 101 | #include <err.h> |
Garrett Cooper | 04f4207 | 2011-01-10 12:55:44 -0800 | [diff] [blame] | 102 | #include <errno.h> |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 103 | #include <stdio.h> |
| 104 | #include <stdlib.h> |
nstraz | d5d51ca | 2001-02-28 17:41:59 +0000 | [diff] [blame] | 105 | #include <string.h> |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 106 | #include <time.h> |
Garrett Cooper | 04f4207 | 2011-01-10 12:55:44 -0800 | [diff] [blame] | 107 | #include <unistd.h> |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 108 | |
nstraz | 9d10051 | 2002-06-20 19:03:00 +0000 | [diff] [blame] | 109 | #include "random_range.h" |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * Structure used to hold file line offset. |
| 113 | */ |
| 114 | struct offset_t { |
| 115 | long used; |
| 116 | long offset; |
| 117 | }; |
| 118 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 119 | void usage(FILE * stream); |
nstraz | 9d10051 | 2002-06-20 19:03:00 +0000 | [diff] [blame] | 120 | void help(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | int rnd_file(FILE * infile, int numlines, long seed); |
| 122 | int get_numlines(FILE * infile); |
nstraz | 9d10051 | 2002-06-20 19:03:00 +0000 | [diff] [blame] | 123 | int rnd_insert(struct offset_t offsets[], long offset, int size); |
| 124 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 125 | #define DEF_SIZE 4096 /* default chunk size */ |
| 126 | #define MAX_LN_SZ 4096 /* max line size */ |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 127 | |
| 128 | #ifndef SEEK_SET |
| 129 | #define SEEK_SET 0 |
| 130 | #endif |
| 131 | |
nstraz | 9d10051 | 2002-06-20 19:03:00 +0000 | [diff] [blame] | 132 | char *Progname = NULL; |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 133 | |
| 134 | /*********************************************************************** |
| 135 | * MAIN |
| 136 | ***********************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 137 | int main(argc, argv) |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 138 | int argc; |
| 139 | char **argv; |
| 140 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 141 | FILE *infile; |
| 142 | int c; |
| 143 | long seed = -1; /* use time as seed */ |
| 144 | int lsize = DEF_SIZE; /* num lines to randomize */ |
| 145 | int getfilelines = 0; /* if set, count lines first */ |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 146 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 147 | if ((Progname = strrchr(argv[0], '/')) == NULL) |
| 148 | Progname = argv[0]; |
| 149 | else |
| 150 | Progname++; |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 151 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 152 | while ((c = getopt(argc, argv, "hgS:l:")) != EOF) { |
| 153 | switch (c) { |
| 154 | case 'h': |
| 155 | help(); |
| 156 | exit(0); |
| 157 | break; |
| 158 | case 'S': /* seed */ |
| 159 | if (sscanf(optarg, "%li", &seed) != 1) { |
| 160 | fprintf(stderr, |
| 161 | "%s: --S option argument is invalid\n", |
| 162 | Progname); |
| 163 | exit(1); |
| 164 | } |
| 165 | break; |
| 166 | |
| 167 | case 'l': /* number of lines */ |
| 168 | if (sscanf(optarg, "%i", &lsize) != 1) { |
| 169 | fprintf(stderr, |
| 170 | "%s: --s option argument is invalid\n", |
| 171 | Progname); |
| 172 | exit(1); |
| 173 | } |
| 174 | break; |
| 175 | |
| 176 | case 'g': |
| 177 | getfilelines++; |
| 178 | break; |
| 179 | |
| 180 | case '?': |
| 181 | usage(stderr); |
| 182 | exit(1); |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (optind + 1 != argc) { |
| 188 | fprintf(stderr, "%s: Missing argument.\n", Progname); |
| 189 | usage(stderr); |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 190 | exit(1); |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 193 | if (seed == -1) { |
| 194 | seed = time(0); |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 197 | if (strcmp(argv[argc - 1], "-") == 0) { |
| 198 | infile = stdin; |
| 199 | fprintf(stderr, "%s: Can not support stdin processing.\n", |
| 200 | Progname); |
| 201 | exit(2); |
| 202 | } else { |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 203 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 204 | if ((infile = fopen(argv[argc - 1], "r")) == NULL) { |
| 205 | fprintf(stderr, "%s: Unable to open file %s: %s\n", |
| 206 | Progname, argv[argc - 1], strerror(errno)); |
| 207 | exit(1); |
| 208 | } |
| 209 | |
| 210 | if (getfilelines) { |
| 211 | lsize = get_numlines(infile); |
| 212 | } |
| 213 | |
| 214 | rnd_file(infile, lsize, seed); |
| 215 | } |
| 216 | |
| 217 | exit(0); |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /*********************************************************************** |
| 221 | * Print usage message to stream. |
| 222 | ***********************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 223 | void usage(FILE * stream) |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 224 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 225 | fprintf(stream, |
| 226 | "Usage %s [-hg][-S seed][-l numlines] [files...]\n", Progname); |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 227 | |
| 228 | } |
| 229 | |
| 230 | /*********************************************************************** |
| 231 | * Print help message to stdout. |
| 232 | ***********************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 233 | void help() |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 234 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 235 | usage(stdout); |
| 236 | printf("This tool will print lines in random order (max line len %d).\n\ |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 237 | -h : print this help and exit\n\ |
| 238 | -g : count the number of lines in the file before randomizing\n\ |
| 239 | This option overrides -l option.\n\ |
| 240 | -l numlines : randoms lines in numlines chuncks (def %d)\n\ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 241 | -S seed : sets seed to seed (def time(0))\n", MAX_LN_SZ, DEF_SIZE); |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 242 | |
| 243 | } |
| 244 | |
| 245 | /*********************************************************************** |
| 246 | * counts the number of lines in already open file. |
| 247 | * Note: File must be seekable (not stdin or a pipe). |
| 248 | ***********************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 249 | int get_numlines(infile) |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 250 | FILE *infile; |
| 251 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 252 | char line[MAX_LN_SZ]; /* max size of a line */ |
| 253 | int cnt = 0; |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 254 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 255 | while (fgets(line, MAX_LN_SZ, infile) != NULL) { |
| 256 | cnt++; |
| 257 | } |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 258 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 259 | /* rewind the file */ |
| 260 | fseek(infile, 0, SEEK_SET); |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 261 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 262 | return cnt; |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /*********************************************************************** |
| 266 | * |
| 267 | * infile must be a fseekable file. Thus, it can not be stdin. |
| 268 | * It will read each line in the file, randomly saving the offset |
| 269 | * of each line in a array of struct offset_t. |
| 270 | * It will then print each line in the array stored order. |
| 271 | * |
| 272 | ***********************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 273 | int rnd_file(infile, numlines, seed) |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 274 | FILE *infile; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 275 | int numlines; /* can be more or less than num lines in file */ |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 276 | /* most opt randomized when num lines in files */ |
| 277 | /* or just a bit bigger */ |
| 278 | long seed; |
| 279 | { |
| 280 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 281 | char line[MAX_LN_SZ]; /* max size of a line */ |
| 282 | int cnt; |
| 283 | long coffset; /* current line offset */ |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 284 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 285 | struct offset_t *offsets; |
| 286 | int memsize; |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 287 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 288 | if (numlines <= 0) { /*use default */ |
| 289 | numlines = DEF_SIZE; |
| 290 | } |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 291 | |
| 292 | /* |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 293 | * Malloc space for numlines copies the offset_t structure. |
| 294 | * This is where the randomization takes place. |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 295 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 296 | memsize = sizeof(struct offset_t) * numlines; |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 297 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 298 | if ((offsets = (struct offset_t *)malloc(memsize)) == NULL) { |
| 299 | fprintf(stderr, "Unable to malloc(%d): errno:%d\n", memsize, |
| 300 | errno); |
| 301 | return -1; |
| 302 | } |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 303 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 304 | random_range_seed(seed); |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 305 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 306 | coffset = 0; |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 307 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 308 | while (!feof(infile)) { |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 309 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 310 | fseek(infile, coffset, SEEK_SET); |
| 311 | coffset = ftell(infile); |
| 312 | memset(offsets, 0, memsize); |
| 313 | cnt = 0; |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 314 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 315 | /* |
| 316 | * read the file in and place offset of each line randomly |
| 317 | * into offsets array. Only numlines line can be randomized |
| 318 | * at a time. |
| 319 | */ |
| 320 | while (cnt < numlines && fgets(line, MAX_LN_SZ, infile) != NULL) { |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 321 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 322 | if (rnd_insert(offsets, coffset, numlines) < 0) { |
| 323 | fprintf(stderr, |
| 324 | "%s:%d rnd_insert() returned -1 (fatal error)!\n", |
| 325 | __FILE__, __LINE__); |
| 326 | abort(); |
| 327 | } |
| 328 | cnt++; |
| 329 | |
| 330 | coffset = ftell(infile); |
| 331 | } |
| 332 | |
| 333 | if (cnt == 0) { |
| 334 | continue; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * print out lines based on offset. |
| 339 | */ |
| 340 | for (cnt = 0; cnt < numlines; cnt++) { |
| 341 | |
| 342 | if (offsets[cnt].used) { |
| 343 | fseek(infile, offsets[cnt].offset, SEEK_SET); |
| 344 | if (fgets(line, MAX_LN_SZ, infile) == NULL) |
| 345 | err(1, "fgets"); |
| 346 | fputs(line, stdout); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | } /* end of file */ |
| 351 | |
| 352 | return 0; |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /*********************************************************************** |
| 356 | * This function randomly inserts offset information into |
| 357 | * the offsets array. The array has a size of size. |
| 358 | * It will attempt 75 random array indexes before finding the first |
| 359 | * open array element. |
| 360 | * |
| 361 | ***********************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 362 | int rnd_insert(offsets, offset, size) |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 363 | struct offset_t offsets[]; |
nstraz | 9d10051 | 2002-06-20 19:03:00 +0000 | [diff] [blame] | 364 | long offset; |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 365 | int size; |
| 366 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 367 | int rand_num; |
| 368 | int quick = 0; |
| 369 | int ind; |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 370 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 371 | /* |
| 372 | * Loop looking for random unused index. |
| 373 | * It will only be attempted 75 times. |
| 374 | */ |
| 375 | while (quick < 75) { |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 376 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 377 | rand_num = random_range(0, size - 1, 1, NULL); |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 378 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 379 | if (!offsets[rand_num].used) { |
| 380 | offsets[rand_num].offset = offset; |
| 381 | offsets[rand_num].used++; |
| 382 | return rand_num; |
| 383 | } |
| 384 | quick++; |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 385 | } |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 386 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 387 | /* |
| 388 | * an randomly choosen index was not found, find |
| 389 | * first open index and use it. |
| 390 | */ |
| 391 | for (ind = 0; ind < size && offsets[ind].used != 0; ind++) ; /* do nothing */ |
nstraz | 9d10051 | 2002-06-20 19:03:00 +0000 | [diff] [blame] | 392 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 393 | if (ind >= size) { |
| 394 | /* |
| 395 | * If called with an array where all offsets are used, |
| 396 | * we won't be able to find an open array location. |
| 397 | * Thus, return -1 indicating the error. |
| 398 | * This should never happen if called correctly. |
| 399 | */ |
| 400 | return -1; |
| 401 | } |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 402 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 403 | offsets[ind].offset = offset; |
| 404 | offsets[ind].used++; |
| 405 | return ind; |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 406 | |
| 407 | } |
| 408 | |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 409 | /*********************************************************************** |
| 410 | * |
| 411 | * CODE NOT TESTED AT ALL - it must be tested before it is used. |
| 412 | * |
| 413 | * This function was written to allow rand_lines to work on non-seekable |
| 414 | * file (i.e stdin). |
| 415 | * |
| 416 | ***********************************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 417 | int rnd_stdin(infile, space, numlines, seed) |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 418 | FILE *infile; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 419 | int space; /* amount of space to use to read file into memory, */ |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 420 | /* randomized and print. randomize in chunks */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 421 | int numlines; /* can be more or less than num lines in file */ |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 422 | /* most opt randomized when num lines in files */ |
| 423 | /* or just a bit bigger */ |
| 424 | long seed; |
| 425 | { |
| 426 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 427 | char line[MAX_LN_SZ]; /* max size of a line */ |
| 428 | int cnt; /* offset printer counter */ |
| 429 | long loffset; /* last line address */ |
| 430 | char *buffer; /* malloc space for file reads */ |
| 431 | char *rdbuff; /* where to start read */ |
| 432 | long stopaddr; /* end of read space (address) */ |
| 433 | int rdsz; /* amount read */ |
| 434 | int sztord; |
| 435 | char *chr; /* buffer processing pointer */ |
| 436 | char *ptr; /* printing processing pointer */ |
| 437 | char *lptr; /* printing processing pointer */ |
| 438 | int loopcntl = 1; /* main loop control flag */ |
| 439 | struct offset_t *offsets; /* pointer to offset space */ |
| 440 | int memsize; /* amount of offset space to malloc */ |
| 441 | int newbuffer = 1; /* need new buffer */ |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 442 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 443 | if (numlines <= 0) { /*use default */ |
| 444 | numlines = DEF_SIZE; |
| 445 | } |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 446 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 447 | /* |
| 448 | * Malloc space for file contents |
| 449 | */ |
| 450 | if ((buffer = (char *)malloc(space)) == NULL) { |
| 451 | fprintf(stderr, "Unable to malloc(%d): errno:%d\n", space, |
| 452 | errno); |
| 453 | return -1; |
| 454 | } |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 455 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 456 | /* |
| 457 | * Malloc space for numlines copies the offset_t structure. |
| 458 | * This is where the randomization takes place. |
| 459 | */ |
| 460 | memsize = sizeof(struct offset_t) * numlines; |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 461 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 462 | if ((offsets = (struct offset_t *)malloc(memsize)) == NULL) { |
| 463 | fprintf(stderr, "Unable to malloc(%d): errno:%d\n", memsize, |
| 464 | errno); |
| 465 | return -1; |
| 466 | } |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 467 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 468 | random_range_seed(seed); |
| 469 | rdbuff = buffer; /* read into start of buffer */ |
| 470 | sztord = space; /* amount of space left in buffer */ |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 471 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 472 | /* |
| 473 | * Loop until read doesn't read anything |
| 474 | * If last line does not end in newline, it is not printed |
| 475 | */ |
| 476 | while (loopcntl) { |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 477 | /* |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 478 | * read in file up to space size |
| 479 | * only works if used as filter. |
| 480 | * The code will randomize one reads worth at a time. |
| 481 | * If typing in lines, read will read only one line - no randomizing. |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 482 | */ |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 483 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 484 | chr = buffer; |
| 485 | if ((rdsz = fread((void *)rdbuff, sztord, 1, infile)) == 0) { |
| 486 | fprintf(stderr, |
| 487 | "input file is empty, done randomizing\n"); |
| 488 | loopcntl = 0; |
| 489 | return 0; |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 492 | stopaddr = ((long)buffer + rdsz); |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 493 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 494 | loffset = (long)buffer; |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 495 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 496 | while (!newbuffer) { |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 497 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 498 | while ((long)chr < stopaddr && *chr != '\n') |
| 499 | chr++; |
| 500 | |
| 501 | chr++; |
| 502 | |
| 503 | if ((long)chr >= stopaddr) { |
| 504 | |
| 505 | fprintf(stderr, "end of read in buffer\n"); |
| 506 | |
| 507 | /* |
| 508 | * print out lines based on offset. |
| 509 | */ |
| 510 | for (cnt = 0; cnt < numlines; cnt++) { |
| 511 | |
| 512 | if (offsets[cnt].used) { |
| 513 | ptr = |
| 514 | (char *)offsets[cnt].offset; |
| 515 | /* |
| 516 | * copy buffer characters into line for printing |
| 517 | */ |
| 518 | lptr = line; |
| 519 | while (*ptr != '\n') |
| 520 | *lptr++ = *ptr++; |
| 521 | |
| 522 | printf("%s\n", line); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * move start of partically read line to beginning of buffer |
| 528 | * and adjust rdbuff to end of partically read line |
| 529 | */ |
| 530 | memcpy((void *)loffset, buffer, |
| 531 | (stopaddr - loffset)); |
| 532 | rdbuff = buffer + (stopaddr - loffset); |
| 533 | sztord = space - (stopaddr - loffset); |
| 534 | |
| 535 | newbuffer++; |
| 536 | } |
| 537 | |
| 538 | if (rnd_insert(offsets, loffset, numlines) < 0) { |
| 539 | fprintf(stderr, |
| 540 | "%s:%d rnd_insert() returned -1 (fatal error)!\n", |
| 541 | __FILE__, __LINE__); |
| 542 | abort(); |
| 543 | } |
| 544 | |
| 545 | loffset = (long)chr; |
| 546 | } |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 547 | } |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 548 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 549 | return 0; |
nstraz | ed8fe6b | 2001-01-22 17:50:31 +0000 | [diff] [blame] | 550 | |
| 551 | } |