2 * Copyright (c) 2000-2003 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/
36 * Derived from fill.c, adds command line options, block boundary marking
37 * and allows me to tweak to suit fill2fs without breaking other qa tests.
38 * This version marks block boundaries (512, 1k, 4k and 16k) within the file
39 * using characters guaranteed not to appear anywhere else, this may allow
40 * simple checks in future which can inspect these offsets and ensure one of
41 * of the four characters is present. Also fixes off-by-one error in fill.c
42 * and is more careful about checking when write operations fail - this is
43 * needed by fill2fs to ensure that the number of bytes written is accurately
49 #define constpp char * const *
51 #define N(x) (sizeof(x)/sizeof(x[0]))
53 /* function prototypes */
54 static void illegal(char *, char *);
55 static void reqval(char, char * [], int);
56 static void respec(char, char * [], int);
57 static void unknown(char, char *);
58 static void usage(void);
61 char *dopts[] = { "nbytes", "linelength", "seed", "file", NULL };
62 enum { D_NBYTES, D_LINELENGTH, D_SEED, D_FILE, D_ISSET, D_NUM };
63 int dflags[D_NUM] = { 0 };
64 char *bopts[] = { "512", "1k", "4k", "16k" };
65 enum { B_512, B_1K, B_4K, B_16K, B_ISSET, B_NUM };
66 int bflags[B_NUM] = { 0 };
74 /* block boundaries which should be flagged in the output file */
75 /* flag is the character that should be used to indicate each type */
76 /* of block boundary */
85 static struct s_block_type btypes[] = {
86 { 0, 512, '!' }, /* 512 */
87 { 0, 1024, '"' }, /* 1k */
88 { 0, 4096, '#' }, /* 4k */
89 { 0, 16384, '$' }, /* 16k */
98 main(int argc, char **argv)
100 int status = 0; /* return status */
101 int c; /* current option character */
102 char *p; /* for getsubopt calls */
103 long nbytes; /* total number of bytes to write */
104 int dlen; /* length of normal output data line */
105 const char *dseed = NULL; /* input string for seeding rand */
106 unsigned int seed; /* seed for output data */
107 char *dfile = NULL; /* where to write output */
109 FILE *f; /* output file */
110 char *dbuf; /* output line buffer */
111 char bbuf[50]; /* block boundary string */
112 char *active = NULL; /* active buffer to copy out of */
113 size_t hlen; /* header length (bytes+key) in output */
115 char *hptr; /* pointer to end of header */
116 char *ptr; /* current position to copy from */
117 int blktype = 0; /* current block boundary type */
118 int boundary; /* set if current output char lies on */
127 progname = basename(argv[0]);
128 for (p = progname; *p; p++) {
133 nbytes = 1024 * 1024;
134 dlen = 73; /* includes the trailing newline */
136 while ((c = getopt(argc, argv, "d:b:")) != EOF) {
140 respec('d', NULL, 0);
146 switch (getsubopt(&p, (constpp)dopts, &value)) {
148 if (! value) reqval('d', dopts, D_NBYTES);
149 if (dflags[D_NBYTES]) respec('d', dopts, D_NBYTES);
150 dflags[D_NBYTES] = 1;
151 nbytes = strtol(value, &ptr, 10);
152 if (ptr == value) illegal(value, "d nbytes");
156 if (! value) reqval('d', dopts, D_LINELENGTH);
157 if (dflags[D_LINELENGTH]) respec('d', dopts, D_LINELENGTH);
158 dflags[D_LINELENGTH] = 1;
159 dlen = (int) strtol(value, &ptr, 10) + 1;
160 if (ptr == value) illegal(value, "d linelength");
164 if (! value) reqval('d', dopts, D_SEED);
165 if (dflags[D_SEED]) respec('D', dopts, D_SEED);
171 if (! value) reqval('d', dopts, D_FILE);
172 if (dflags[D_FILE]) respec('d', dopts, D_FILE);
185 respec('b', NULL, 0);
191 switch (getsubopt(&p, (constpp)bopts, &value)) {
193 if (value) illegal(value, "b 512");
194 if (bflags[B_512]) respec('b', bopts, B_512);
200 if (value) illegal(value, "b 1k");
201 if (bflags[B_1K]) respec('b', bopts, B_1K);
207 if (value) illegal(value, "b 4k");
208 if (bflags[B_4K]) respec('b', bopts, B_4K);
214 if (value) illegal(value, "b 16k");
215 if (bflags[B_16K]) respec('b', bopts, B_16K);
232 if (dflags[D_FILE] && optind != argc)
235 if (! dflags[D_FILE] && argc - optind != 1)
238 if (! dflags[D_FILE])
239 dfile = argv[optind];
241 if ((f = fopen(dfile, "w")) == NULL) {
242 fprintf(stderr, "fill2: cannot create \"%s\": %s\n",
243 dfile, strerror(errno));
248 if (! dflags[D_SEED])
251 /* seed is an ascii string */
262 /* normal data line format: XXXXXXXXXXXX CCCC...CCC CCCCCCCCCCCC...CCC */
263 /* block boundary format: CXXXXXXX */
265 dbuf = (char *) malloc(dlen + 1);
266 hlen = 12+1+strlen(dseed)+1;
270 for (i = 0; i < nbytes; i++) {
273 /* is this a block boundary? check largest to smallest */
275 for (j = N(btypes) - 1; j >= 0; j--) {
276 if (btypes[j].mark) {
277 /* first time through or just before a block boundary? */
278 if (i == 0 || i % btypes[j].size == btypes[j].size - 1) {
287 /* are there block boundaries to check? */
288 /* is this the first time through? */
289 /* block boundaries take priority over other output */
290 if (bflags[B_ISSET] && (i == 0 || boundary)) {
291 sprintf(bbuf, "%s%c%07ld\n",
293 btypes[blktype].flag,
294 /* remember i is one less than block boundary */
295 i ? (i+1) / btypes[blktype].size : 0);
299 /* are we at the start of a new line? */
301 || (active == bbuf && *ptr == '\0')
302 || (active == dbuf && l == dlen))
304 sprintf(dbuf, "%012ld %s ", i, dseed);
305 assert(*hptr == '\0');
306 for (ptr = hptr; ptr != dbuf + dlen - 1; ptr++) {
307 /* characters upto 126 '~' are used */
308 /* do not use !"#$ - leave free for use as block markers */
309 *ptr = 37 + (rand() % (127 - 37));
313 assert(ptr == dbuf + dlen);
319 /* continue copying from the active buffer */
322 /* output one new character from current buffer */
323 if (fputc(*ptr++, f) == EOF) {
324 fprintf(stderr, "fill2: could not write character to \"%s\": %s\n",
325 dfile, strerror(errno));
329 if (active == dbuf) l++;
335 /* close file and flush buffers - check if this fails */
336 if (fclose(f) != 0) {
337 fprintf(stderr, "fill2: fclose() on \"%s\" failed: %s\n",
338 dfile, strerror(errno));
347 * suboption checking functions
352 illegal(char *value, char *opt)
354 fprintf(stderr, "%s: Error: Illegal value \"%s\" for -%s option\n",
355 progname, value, opt);
359 reqval(char opt, char *tab[], int idx)
361 fprintf(stderr, "%s: Error: -%c %s option requires a value\n",
362 progname, opt, tab[idx]);
366 respec(char opt, char *tab[], int idx)
368 fprintf(stderr, "%s: Error: ", progname);
369 fprintf(stderr, "-%c ", opt);
370 if (tab) fprintf(stderr, "%s ", tab[idx]);
371 fprintf(stderr, "option respecified\n");
375 unknown(char opt, char *s)
377 fprintf(stderr, "%s: Error: Unknown option -%c %s\n", progname, opt, s);
383 fprintf(stderr, "Usage: %s [options] filename\n"
385 " -d [nbytes=num,linelength=num, output data properties\n"
386 " seed=string,file=name]\n"
387 " -b [512,1k,4k,16k] where to mark block boundaries\n", progname);