blob: fdf3f792025cbf97ca30f7945eca2e250767bd38 [file] [log] [blame]
nstrazed8fe6b2001-01-22 17:50:31 +00001/*
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 Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
nstrazed8fe6b2001-01-22 17:50:31 +000022 *
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 */
nstraza700ef52002-09-16 15:02:57 +000033/* $Id: rand_lines.c,v 1.5 2002/09/16 15:02:57 nstraz Exp $ */
nstrazed8fe6b2001-01-22 17:50:31 +000034/**************************************************************
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 Cooper1e6f5a62010-12-19 09:58:10 -080047 * CO-PILOT(s) :
nstrazed8fe6b2001-01-22 17:50:31 +000048 *
49 * DATE STARTED : 05/94
50 *
51 * INPUT SPECIFICATIONS
52 * This tool will print lines of a file in random order.
nstraz9d100512002-06-20 19:03:00 +000053 * The max line length is 4096.
nstrazed8fe6b2001-01-22 17:50:31 +000054 * 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 Cooper1e6f5a62010-12-19 09:58:10 -080062 *
nstrazed8fe6b2001-01-22 17:50:31 +000063 * -l numlines : This option specifies to randomize file in
nstraz9d100512002-06-20 19:03:00 +000064 * numlines chucks. The default size is 4096.
nstrazed8fe6b2001-01-22 17:50:31 +000065 *
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080066 * -S seed : sets randomization seed to seed.
nstrazed8fe6b2001-01-22 17:50:31 +000067 * 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
nstraz9d100512002-06-20 19:03:00 +000090 * rrl 06/02 Fixed bug and some cleanup. Changed default chunk
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080091 * and line size to 4096 characters.
nstrazed8fe6b2001-01-22 17:50:31 +000092 *
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
nstraz9d100512002-06-20 19:03:00 +000097 * length is 4096 characters.
nstrazed8fe6b2001-01-22 17:50:31 +000098 *
99 **************************************************************/
100
Garrett Cooper83466312011-02-15 06:39:09 -0800101#include <err.h>
Garrett Cooper04f42072011-01-10 12:55:44 -0800102#include <errno.h>
nstrazed8fe6b2001-01-22 17:50:31 +0000103#include <stdio.h>
104#include <stdlib.h>
nstrazd5d51ca2001-02-28 17:41:59 +0000105#include <string.h>
nstrazed8fe6b2001-01-22 17:50:31 +0000106#include <time.h>
Garrett Cooper04f42072011-01-10 12:55:44 -0800107#include <unistd.h>
nstrazed8fe6b2001-01-22 17:50:31 +0000108
nstraz9d100512002-06-20 19:03:00 +0000109#include "random_range.h"
nstrazed8fe6b2001-01-22 17:50:31 +0000110
111/*
112 * Structure used to hold file line offset.
113 */
114struct offset_t {
115 long used;
116 long offset;
117};
118
Wanlong Gao354ebb42012-12-07 10:10:04 +0800119void usage(FILE * stream);
nstraz9d100512002-06-20 19:03:00 +0000120void help();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800121int rnd_file(FILE * infile, int numlines, long seed);
122int get_numlines(FILE * infile);
nstraz9d100512002-06-20 19:03:00 +0000123int rnd_insert(struct offset_t offsets[], long offset, int size);
124
Wanlong Gao354ebb42012-12-07 10:10:04 +0800125#define DEF_SIZE 4096 /* default chunk size */
126#define MAX_LN_SZ 4096 /* max line size */
nstrazed8fe6b2001-01-22 17:50:31 +0000127
128#ifndef SEEK_SET
129#define SEEK_SET 0
130#endif
131
nstraz9d100512002-06-20 19:03:00 +0000132char *Progname = NULL;
nstrazed8fe6b2001-01-22 17:50:31 +0000133
134/***********************************************************************
135 * MAIN
136 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800137int main(argc, argv)
nstrazed8fe6b2001-01-22 17:50:31 +0000138int argc;
139char **argv;
140{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 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 */
nstrazed8fe6b2001-01-22 17:50:31 +0000146
Wanlong Gao354ebb42012-12-07 10:10:04 +0800147 if ((Progname = strrchr(argv[0], '/')) == NULL)
148 Progname = argv[0];
149 else
150 Progname++;
nstrazed8fe6b2001-01-22 17:50:31 +0000151
Wanlong Gao354ebb42012-12-07 10:10:04 +0800152 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);
nstrazed8fe6b2001-01-22 17:50:31 +0000190 exit(1);
nstrazed8fe6b2001-01-22 17:50:31 +0000191 }
192
Wanlong Gao354ebb42012-12-07 10:10:04 +0800193 if (seed == -1) {
194 seed = time(0);
nstrazed8fe6b2001-01-22 17:50:31 +0000195 }
196
Wanlong Gao354ebb42012-12-07 10:10:04 +0800197 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 {
nstrazed8fe6b2001-01-22 17:50:31 +0000203
Wanlong Gao354ebb42012-12-07 10:10:04 +0800204 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);
nstrazed8fe6b2001-01-22 17:50:31 +0000218}
219
220/***********************************************************************
221 * Print usage message to stream.
222 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800223void usage(FILE * stream)
nstrazed8fe6b2001-01-22 17:50:31 +0000224{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800225 fprintf(stream,
226 "Usage %s [-hg][-S seed][-l numlines] [files...]\n", Progname);
nstrazed8fe6b2001-01-22 17:50:31 +0000227
228}
229
230/***********************************************************************
231 * Print help message to stdout.
232 ***********************************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800233void help()
nstrazed8fe6b2001-01-22 17:50:31 +0000234{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800235 usage(stdout);
236 printf("This tool will print lines in random order (max line len %d).\n\
nstrazed8fe6b2001-01-22 17:50:31 +0000237 -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 Gao354ebb42012-12-07 10:10:04 +0800241 -S seed : sets seed to seed (def time(0))\n", MAX_LN_SZ, DEF_SIZE);
nstrazed8fe6b2001-01-22 17:50:31 +0000242
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 Gao354ebb42012-12-07 10:10:04 +0800249int get_numlines(infile)
nstrazed8fe6b2001-01-22 17:50:31 +0000250FILE *infile;
251{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800252 char line[MAX_LN_SZ]; /* max size of a line */
253 int cnt = 0;
nstrazed8fe6b2001-01-22 17:50:31 +0000254
Wanlong Gao354ebb42012-12-07 10:10:04 +0800255 while (fgets(line, MAX_LN_SZ, infile) != NULL) {
256 cnt++;
257 }
nstrazed8fe6b2001-01-22 17:50:31 +0000258
Wanlong Gao354ebb42012-12-07 10:10:04 +0800259 /* rewind the file */
260 fseek(infile, 0, SEEK_SET);
nstrazed8fe6b2001-01-22 17:50:31 +0000261
Wanlong Gao354ebb42012-12-07 10:10:04 +0800262 return cnt;
nstrazed8fe6b2001-01-22 17:50:31 +0000263}
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 Gao354ebb42012-12-07 10:10:04 +0800273int rnd_file(infile, numlines, seed)
nstrazed8fe6b2001-01-22 17:50:31 +0000274FILE *infile;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800275int numlines; /* can be more or less than num lines in file */
nstrazed8fe6b2001-01-22 17:50:31 +0000276 /* most opt randomized when num lines in files */
277 /* or just a bit bigger */
278long seed;
279{
280
Wanlong Gao354ebb42012-12-07 10:10:04 +0800281 char line[MAX_LN_SZ]; /* max size of a line */
282 int cnt;
283 long coffset; /* current line offset */
nstrazed8fe6b2001-01-22 17:50:31 +0000284
Wanlong Gao354ebb42012-12-07 10:10:04 +0800285 struct offset_t *offsets;
286 int memsize;
nstrazed8fe6b2001-01-22 17:50:31 +0000287
Wanlong Gao354ebb42012-12-07 10:10:04 +0800288 if (numlines <= 0) { /*use default */
289 numlines = DEF_SIZE;
290 }
nstrazed8fe6b2001-01-22 17:50:31 +0000291
292 /*
Wanlong Gao354ebb42012-12-07 10:10:04 +0800293 * Malloc space for numlines copies the offset_t structure.
294 * This is where the randomization takes place.
nstrazed8fe6b2001-01-22 17:50:31 +0000295 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800296 memsize = sizeof(struct offset_t) * numlines;
nstrazed8fe6b2001-01-22 17:50:31 +0000297
Wanlong Gao354ebb42012-12-07 10:10:04 +0800298 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 }
nstrazed8fe6b2001-01-22 17:50:31 +0000303
Wanlong Gao354ebb42012-12-07 10:10:04 +0800304 random_range_seed(seed);
nstrazed8fe6b2001-01-22 17:50:31 +0000305
Wanlong Gao354ebb42012-12-07 10:10:04 +0800306 coffset = 0;
nstrazed8fe6b2001-01-22 17:50:31 +0000307
Wanlong Gao354ebb42012-12-07 10:10:04 +0800308 while (!feof(infile)) {
nstrazed8fe6b2001-01-22 17:50:31 +0000309
Wanlong Gao354ebb42012-12-07 10:10:04 +0800310 fseek(infile, coffset, SEEK_SET);
311 coffset = ftell(infile);
312 memset(offsets, 0, memsize);
313 cnt = 0;
nstrazed8fe6b2001-01-22 17:50:31 +0000314
Wanlong Gao354ebb42012-12-07 10:10:04 +0800315 /*
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) {
nstrazed8fe6b2001-01-22 17:50:31 +0000321
Wanlong Gao354ebb42012-12-07 10:10:04 +0800322 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;
nstrazed8fe6b2001-01-22 17:50:31 +0000353}
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 Gao354ebb42012-12-07 10:10:04 +0800362int rnd_insert(offsets, offset, size)
nstrazed8fe6b2001-01-22 17:50:31 +0000363struct offset_t offsets[];
nstraz9d100512002-06-20 19:03:00 +0000364long offset;
nstrazed8fe6b2001-01-22 17:50:31 +0000365int size;
366{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800367 int rand_num;
368 int quick = 0;
369 int ind;
nstrazed8fe6b2001-01-22 17:50:31 +0000370
Wanlong Gao354ebb42012-12-07 10:10:04 +0800371 /*
372 * Loop looking for random unused index.
373 * It will only be attempted 75 times.
374 */
375 while (quick < 75) {
nstrazed8fe6b2001-01-22 17:50:31 +0000376
Wanlong Gao354ebb42012-12-07 10:10:04 +0800377 rand_num = random_range(0, size - 1, 1, NULL);
nstrazed8fe6b2001-01-22 17:50:31 +0000378
Wanlong Gao354ebb42012-12-07 10:10:04 +0800379 if (!offsets[rand_num].used) {
380 offsets[rand_num].offset = offset;
381 offsets[rand_num].used++;
382 return rand_num;
383 }
384 quick++;
nstrazed8fe6b2001-01-22 17:50:31 +0000385 }
nstrazed8fe6b2001-01-22 17:50:31 +0000386
Wanlong Gao354ebb42012-12-07 10:10:04 +0800387 /*
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 */
nstraz9d100512002-06-20 19:03:00 +0000392
Wanlong Gao354ebb42012-12-07 10:10:04 +0800393 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 }
nstrazed8fe6b2001-01-22 17:50:31 +0000402
Wanlong Gao354ebb42012-12-07 10:10:04 +0800403 offsets[ind].offset = offset;
404 offsets[ind].used++;
405 return ind;
nstrazed8fe6b2001-01-22 17:50:31 +0000406
407}
408
nstrazed8fe6b2001-01-22 17:50:31 +0000409/***********************************************************************
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 Gao354ebb42012-12-07 10:10:04 +0800417int rnd_stdin(infile, space, numlines, seed)
nstrazed8fe6b2001-01-22 17:50:31 +0000418FILE *infile;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800419int space; /* amount of space to use to read file into memory, */
nstrazed8fe6b2001-01-22 17:50:31 +0000420 /* randomized and print. randomize in chunks */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800421int numlines; /* can be more or less than num lines in file */
nstrazed8fe6b2001-01-22 17:50:31 +0000422 /* most opt randomized when num lines in files */
423 /* or just a bit bigger */
424long seed;
425{
426
Wanlong Gao354ebb42012-12-07 10:10:04 +0800427 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 */
nstrazed8fe6b2001-01-22 17:50:31 +0000442
Wanlong Gao354ebb42012-12-07 10:10:04 +0800443 if (numlines <= 0) { /*use default */
444 numlines = DEF_SIZE;
445 }
nstrazed8fe6b2001-01-22 17:50:31 +0000446
Wanlong Gao354ebb42012-12-07 10:10:04 +0800447 /*
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 }
nstrazed8fe6b2001-01-22 17:50:31 +0000455
Wanlong Gao354ebb42012-12-07 10:10:04 +0800456 /*
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;
nstrazed8fe6b2001-01-22 17:50:31 +0000461
Wanlong Gao354ebb42012-12-07 10:10:04 +0800462 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 }
nstrazed8fe6b2001-01-22 17:50:31 +0000467
Wanlong Gao354ebb42012-12-07 10:10:04 +0800468 random_range_seed(seed);
469 rdbuff = buffer; /* read into start of buffer */
470 sztord = space; /* amount of space left in buffer */
nstrazed8fe6b2001-01-22 17:50:31 +0000471
Wanlong Gao354ebb42012-12-07 10:10:04 +0800472 /*
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) {
nstrazed8fe6b2001-01-22 17:50:31 +0000477 /*
Wanlong Gao354ebb42012-12-07 10:10:04 +0800478 * 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.
nstrazed8fe6b2001-01-22 17:50:31 +0000482 */
nstrazed8fe6b2001-01-22 17:50:31 +0000483
Wanlong Gao354ebb42012-12-07 10:10:04 +0800484 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;
nstrazed8fe6b2001-01-22 17:50:31 +0000490 }
491
Wanlong Gao354ebb42012-12-07 10:10:04 +0800492 stopaddr = ((long)buffer + rdsz);
nstrazed8fe6b2001-01-22 17:50:31 +0000493
Wanlong Gao354ebb42012-12-07 10:10:04 +0800494 loffset = (long)buffer;
nstrazed8fe6b2001-01-22 17:50:31 +0000495
Wanlong Gao354ebb42012-12-07 10:10:04 +0800496 while (!newbuffer) {
nstrazed8fe6b2001-01-22 17:50:31 +0000497
Wanlong Gao354ebb42012-12-07 10:10:04 +0800498 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 }
nstrazed8fe6b2001-01-22 17:50:31 +0000547 }
nstrazed8fe6b2001-01-22 17:50:31 +0000548
Wanlong Gao354ebb42012-12-07 10:10:04 +0800549 return 0;
nstrazed8fe6b2001-01-22 17:50:31 +0000550
551}