3 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * Further, this software is distributed without any warranty that it is
14 * free of the rightful claim of any third person regarding infringement
15 * or the like. Any license provided herein, whether implied or
16 * otherwise, applies only to this software file. Patent licenses, if
17 * any, provided herein do not apply to combinations of this program with
18 * other software, or any other product whatsoever.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write the Free Software Foundation, Inc., 59
22 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
24 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
25 * Mountain View, CA 94043, or:
29 * For further information regarding this notice, see:
31 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
37 * Derived from fill.c, adds command line options, block boundary marking
38 * and allows me to tweak to suit fill2fs without breaking other qa tests.
39 * This version marks block boundaries (512, 1k, 4k and 16k) within the file
40 * using characters guaranteed not to appear anywhere else, this may allow
41 * simple checks in future which can inspect these offsets and ensure one of
42 * of the four characters is present. Also fixes off-by-one error in fill.c
43 * and is more careful about checking when write operations fail - this is
44 * needed by fill2fs to ensure that the number of bytes written is accurately
47 * To compile standalone on IRIX: cc -DTOOL_ONLY -o fill2 fill2.c -lgen
61 #include <platform_defs.h>
63 #define constpp char * const *
64 #endif /* TOOL_ONLY */
66 #define N(x) (sizeof(x)/sizeof(x[0]))
68 /* function prototypes */
69 static void illegal(char *, char *);
70 static void reqval(char, char * [], int);
71 static void respec(char, char * [], int);
72 static void unknown(char, char *);
73 static void usage(void);
74 static char *progname;
76 char *dopts[] = { "nbytes", "linelength", "seed", "file", NULL };
77 enum { D_NBYTES, D_LINELENGTH, D_SEED, D_FILE, D_ISSET, D_NUM };
78 int dflags[D_NUM] = { 0 };
79 char *bopts[] = { "512", "1k", "4k", "16k" };
80 enum { B_512, B_1K, B_4K, B_16K, B_ISSET, B_NUM };
81 int bflags[B_NUM] = { 0 };
89 /* block boundaries which should be flagged in the output file */
90 /* flag is the character that should be used to indicate each type */
91 /* of block boundary */
100 static struct s_block_type btypes[] = {
101 { 0, 512, '!' }, /* 512 */
102 { 0, 1024, '"' }, /* 1k */
103 { 0, 4096, '#' }, /* 4k */
104 { 0, 16384, '$' }, /* 16k */
113 main(int argc, char **argv)
115 int status = 0; /* return status */
116 int c; /* current option character */
117 char *p; /* for getsubopt calls */
118 long nbytes; /* total number of bytes to write */
119 int dlen; /* length of normal output data line */
120 const char *dseed; /* input string for seeding rand */
121 unsigned int seed; /* seed for output data */
122 char *dfile; /* where to write output */
124 FILE *f; /* output file */
125 char *dbuf; /* output line buffer */
126 char bbuf[50]; /* block boundary string */
127 char *active; /* active buffer to copy out of */
128 size_t hlen; /* header length (bytes+key) in output */
130 char *hptr; /* pointer to end of header */
131 char *ptr; /* current position to copy from */
132 int blktype; /* current block boundary type */
133 int boundary; /* set if current output char lies on */
141 progname = basename(argv[0]);
142 nbytes = 1024 * 1024;
143 dlen = 73; /* includes the trailing newline */
145 while ((c = getopt(argc, argv, "d:b:")) != EOF) {
149 respec('d', NULL, 0);
155 switch (getsubopt(&p, (constpp)dopts, &value)) {
157 if (! value) reqval('d', dopts, D_NBYTES);
158 if (dflags[D_NBYTES]) respec('d', dopts, D_NBYTES);
159 dflags[D_NBYTES] = 1;
160 nbytes = strtol(value, &ptr, 10);
161 if (ptr == value) illegal(value, "d nbytes");
165 if (! value) reqval('d', dopts, D_LINELENGTH);
166 if (dflags[D_LINELENGTH]) respec('d', dopts, D_LINELENGTH);
167 dflags[D_LINELENGTH] = 1;
168 dlen = (int) strtol(value, &ptr, 10) + 1;
169 if (ptr == value) illegal(value, "d linelength");
173 if (! value) reqval('d', dopts, D_SEED);
174 if (dflags[D_SEED]) respec('D', dopts, D_SEED);
180 if (! value) reqval('d', dopts, D_FILE);
181 if (dflags[D_FILE]) respec('d', dopts, D_FILE);
194 respec('b', NULL, 0);
200 switch (getsubopt(&p, (constpp)bopts, &value)) {
202 if (value) illegal(value, "b 512");
203 if (bflags[B_512]) respec('b', bopts, B_512);
209 if (value) illegal(value, "b 1k");
210 if (bflags[B_1K]) respec('b', bopts, B_1K);
216 if (value) illegal(value, "b 4k");
217 if (bflags[B_4K]) respec('b', bopts, B_4K);
223 if (value) illegal(value, "b 16k");
224 if (bflags[B_16K]) respec('b', bopts, B_16K);
241 if (dflags[D_FILE] && optind != argc)
244 if (! dflags[D_FILE] && argc - optind != 1)
247 if (! dflags[D_FILE])
248 dfile = argv[optind];
250 if ((f = fopen(dfile, "w")) == NULL) {
251 fprintf(stderr, "fill2: cannot create \"%s\": %s\n",
252 dfile, strerror(errno));
257 if (! dflags[D_SEED])
260 /* seed is an ascii string */
271 /* normal data line format: XXXXXXXXXXXX CCCC...CCC CCCCCCCCCCCC...CCC */
272 /* block boundary format: CXXXXXXX */
274 dbuf = (char *) malloc(dlen + 1);
275 hlen = 12+1+strlen(dseed)+1;
279 for (i = 0; i < nbytes; i++) {
282 /* is this a block boundary? check largest to smallest */
284 for (j = N(btypes) - 1; j >= 0; j--) {
285 if (btypes[j].mark) {
286 /* first time through or just before a block boundary? */
287 if (i == 0 || i % btypes[j].size == btypes[j].size - 1) {
296 /* are there block boundaries to check? */
297 /* is this the first time through? */
298 /* block boundaries take priority over other output */
299 if (bflags[B_ISSET] && (i == 0 || boundary)) {
300 sprintf(bbuf, "%s%c%07ld\n",
302 btypes[blktype].flag,
303 /* remember i is one less than block boundary */
304 i ? (i+1) / btypes[blktype].size : 0);
308 /* are we at the start of a new line? */
310 || (active == bbuf && *ptr == '\0')
311 || (active == dbuf && l == dlen))
313 sprintf(dbuf, "%012ld %s ", i, dseed);
314 assert(*hptr == '\0');
315 for (ptr = hptr; ptr != dbuf + dlen - 1; ptr++) {
316 /* characters upto 126 '~' are used */
317 /* do not use !"#$ - leave free for use as block markers */
318 *ptr = 37 + (rand() % (127 - 37));
322 assert(ptr == dbuf + dlen);
328 /* continue copying from the active buffer */
331 /* output one new character from current buffer */
332 if (fputc(*ptr++, f) == EOF) {
333 fprintf(stderr, "fill2: could not write character to \"%s\": %s\n",
334 dfile, strerror(errno));
338 if (active == dbuf) l++;
344 /* close file and flush buffers - check if this fails */
345 if (fclose(f) != 0) {
346 fprintf(stderr, "fill2: fclose() on \"%s\" failed: %s\n",
347 dfile, strerror(errno));
356 * suboption checking functions
361 illegal(char *value, char *opt)
363 fprintf(stderr, "%s: Error: Illegal value \"%s\" for -%s option\n",
364 progname, value, opt);
368 reqval(char opt, char *tab[], int idx)
370 fprintf(stderr, "%s: Error: -%c %s option requires a value\n",
371 progname, opt, tab[idx]);
375 respec(char opt, char *tab[], int idx)
377 fprintf(stderr, "%s: Error: ", progname);
378 fprintf(stderr, "-%c ", opt);
379 if (tab) fprintf(stderr, "%s ", tab[idx]);
380 fprintf(stderr, "option respecified\n");
384 unknown(char opt, char *s)
386 fprintf(stderr, "%s: Error: Unknown option -%c %s\n", progname, opt, s);
392 fprintf(stderr, "Usage: %s [options] filename\n"
394 " -d [nbytes=num,linelength=num, output data properties\n"
395 " seed=string,file=name]\n"
396 " -b [512,1k,4k,16k] where to mark block boundaries\n", progname);