2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
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.
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.
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.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
33 #include <sys/types.h>
41 This program reads the file specified on the command line and
42 produces a new file on stdout which contains the lines from the
43 input file scrambled into a random order. This is useful for
44 'randomizing' a database to simulate the kind of I/O that will
45 occur when many records have been added and deleted over time.
48 #define FIELD_WIDTH 21
50 static char buffer[4096];
67 fprintf(stderr, "Usage: %s infile\n", argv[0]);
71 /* Read through the input file and count how many lines are present. */
73 if ((infile = fopen(argv[1], "r")) == NULL) {
74 fprintf(stderr, "can't open %s\n", argv[1]);
78 if (fgets(buffer, sizeof(buffer), infile) == NULL) {
81 fprintf(stderr, "Error on infile\n");
86 fprintf(stderr, "%d lines in input file\n", line_count);
88 /* Create a temp file. Copy the input file to the temp file,
89 prepending a random number in the range
95 if ((tmpfile = fopen(path, "w")) == NULL) {
96 fprintf(stderr, "error opening temp file %s\n", path);
100 srand48((long)getpid());
102 for (i = line_count - 1; i >= 0; i--) {
103 if (fgets(buffer, sizeof(buffer), infile) == NULL) {
106 fprintf(stderr, "Error on infile\n");
109 j = (int)(drand48() * (float)i);
110 fprintf(tmpfile, "%*d ", FIELD_WIDTH - 1, j);
111 fputs(buffer, tmpfile);
113 if (fclose(infile)) {
114 fprintf(stderr, "close of input file failed\n");
117 if (fclose(tmpfile)) {
118 fprintf(stderr, "close of temp file %s failed\n", path);
121 fprintf(stderr, "random mapping complete\n");
123 /* Use the system sort routine to sort the file into order on the
124 first field, effectively randomizing the lines.
127 sprintf(buffer, "sort +0 -1 -o %s %s", path, path);
128 if (system(buffer)) {
129 fprintf(stderr, "sort call failed\n");
132 fprintf(stderr, "sort complete\n");
134 /* Copy the temp file to stdout, removing the prepended field. */
136 if ((tmpfile = fopen(path, "rw")) == NULL) {
137 fprintf(stderr, "error reopening temp file %s\n", path);
141 for (i = 0; i < line_count; i++) {
142 if (fgets(buffer, sizeof(buffer), tmpfile) == NULL) {
143 if (!ferror(tmpfile))
145 fprintf(stderr, "Error on tmpfile\n");
148 if (fputs(buffer + FIELD_WIDTH, stdout) < 0) {
149 fprintf(stderr, "Error on outfile\n");
154 fprintf(stderr, "can't unlink %s\n", path);