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