2 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms 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,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Derived from fill.c, adds command line options, block boundary marking
23 * and allows me to tweak to suit fill2fs without breaking other qa tests.
24 * This version marks block boundaries (512, 1k, 4k and 16k) within the file
25 * using characters guaranteed not to appear anywhere else, this may allow
26 * simple checks in future which can inspect these offsets and ensure one of
27 * of the four characters is present. Also fixes off-by-one error in fill.c
28 * and is more careful about checking when write operations fail - this is
29 * needed by fill2fs to ensure that the number of bytes written is accurately
35 #define constpp char * const *
37 #define N(x) (sizeof(x)/sizeof(x[0]))
39 /* function prototypes */
40 static void illegal(char *, char *);
41 static void reqval(char, char * [], int);
42 static void respec(char, char * [], int);
43 static void unknown(char, char *);
44 static void usage(void);
47 char *dopts[] = { "nbytes", "linelength", "seed", "file", NULL };
48 enum { D_NBYTES, D_LINELENGTH, D_SEED, D_FILE, D_ISSET, D_NUM };
49 int dflags[D_NUM] = { 0 };
50 char *bopts[] = { "512", "1k", "4k", "16k" };
51 enum { B_512, B_1K, B_4K, B_16K, B_ISSET, B_NUM };
52 int bflags[B_NUM] = { 0 };
60 /* block boundaries which should be flagged in the output file */
61 /* flag is the character that should be used to indicate each type */
62 /* of block boundary */
71 static struct s_block_type btypes[] = {
72 { 0, 512, '!' }, /* 512 */
73 { 0, 1024, '"' }, /* 1k */
74 { 0, 4096, '#' }, /* 4k */
75 { 0, 16384, '$' }, /* 16k */
84 main(int argc, char **argv)
86 int status = 0; /* return status */
87 int c; /* current option character */
88 char *p; /* for getsubopt calls */
89 long nbytes; /* total number of bytes to write */
90 int dlen; /* length of normal output data line */
91 const char *dseed = NULL; /* input string for seeding rand */
92 unsigned int seed; /* seed for output data */
93 char *dfile = NULL; /* where to write output */
95 FILE *f; /* output file */
96 char *dbuf; /* output line buffer */
97 char bbuf[50]; /* block boundary string */
98 char *active = NULL; /* active buffer to copy out of */
99 size_t hlen; /* header length (bytes+key) in output */
101 char *hptr; /* pointer to end of header */
102 char *ptr; /* current position to copy from */
103 int blktype = 0; /* current block boundary type */
104 int boundary; /* set if current output char lies on */
113 progname = basename(argv[0]);
114 for (p = progname; *p; p++) {
119 nbytes = 1024 * 1024;
120 dlen = 73; /* includes the trailing newline */
122 while ((c = getopt(argc, argv, "d:b:")) != EOF) {
126 respec('d', NULL, 0);
132 switch (getsubopt(&p, (constpp)dopts, &value)) {
134 if (! value) reqval('d', dopts, D_NBYTES);
135 if (dflags[D_NBYTES]) respec('d', dopts, D_NBYTES);
136 dflags[D_NBYTES] = 1;
137 nbytes = strtol(value, &ptr, 10);
138 if (ptr == value) illegal(value, "d nbytes");
142 if (! value) reqval('d', dopts, D_LINELENGTH);
143 if (dflags[D_LINELENGTH]) respec('d', dopts, D_LINELENGTH);
144 dflags[D_LINELENGTH] = 1;
145 dlen = (int) strtol(value, &ptr, 10) + 1;
146 if (ptr == value) illegal(value, "d linelength");
150 if (! value) reqval('d', dopts, D_SEED);
151 if (dflags[D_SEED]) respec('D', dopts, D_SEED);
157 if (! value) reqval('d', dopts, D_FILE);
158 if (dflags[D_FILE]) respec('d', dopts, D_FILE);
171 respec('b', NULL, 0);
177 switch (getsubopt(&p, (constpp)bopts, &value)) {
179 if (value) illegal(value, "b 512");
180 if (bflags[B_512]) respec('b', bopts, B_512);
186 if (value) illegal(value, "b 1k");
187 if (bflags[B_1K]) respec('b', bopts, B_1K);
193 if (value) illegal(value, "b 4k");
194 if (bflags[B_4K]) respec('b', bopts, B_4K);
200 if (value) illegal(value, "b 16k");
201 if (bflags[B_16K]) respec('b', bopts, B_16K);
218 if (dflags[D_FILE] && optind != argc)
221 if (! dflags[D_FILE] && argc - optind != 1)
224 if (! dflags[D_FILE])
225 dfile = argv[optind];
227 if ((f = fopen(dfile, "w")) == NULL) {
228 fprintf(stderr, "fill2: cannot create \"%s\": %s\n",
229 dfile, strerror(errno));
234 if (! dflags[D_SEED])
237 /* seed is an ascii string */
248 /* normal data line format: XXXXXXXXXXXX CCCC...CCC CCCCCCCCCCCC...CCC */
249 /* block boundary format: CXXXXXXX */
251 dbuf = (char *) malloc(dlen + 1);
252 hlen = 12+1+strlen(dseed)+1;
256 for (i = 0; i < nbytes; i++) {
259 /* is this a block boundary? check largest to smallest */
261 for (j = N(btypes) - 1; j >= 0; j--) {
262 if (btypes[j].mark) {
263 /* first time through or just before a block boundary? */
264 if (i == 0 || i % btypes[j].size == btypes[j].size - 1) {
273 /* are there block boundaries to check? */
274 /* is this the first time through? */
275 /* block boundaries take priority over other output */
276 if (bflags[B_ISSET] && (i == 0 || boundary)) {
277 sprintf(bbuf, "%s%c%07ld\n",
279 btypes[blktype].flag,
280 /* remember i is one less than block boundary */
281 i ? (i+1) / btypes[blktype].size : 0);
285 /* are we at the start of a new line? */
287 || (active == bbuf && *ptr == '\0')
288 || (active == dbuf && l == dlen))
290 sprintf(dbuf, "%012ld %s ", i, dseed);
291 assert(*hptr == '\0');
292 for (ptr = hptr; ptr != dbuf + dlen - 1; ptr++) {
293 /* characters upto 126 '~' are used */
294 /* do not use !"#$ - leave free for use as block markers */
295 *ptr = 37 + (rand() % (127 - 37));
299 assert(ptr == dbuf + dlen);
305 /* continue copying from the active buffer */
308 /* output one new character from current buffer */
309 if (fputc(*ptr++, f) == EOF) {
310 fprintf(stderr, "fill2: could not write character to \"%s\": %s\n",
311 dfile, strerror(errno));
315 if (active == dbuf) l++;
321 /* close file and flush buffers - check if this fails */
322 if (fclose(f) != 0) {
323 fprintf(stderr, "fill2: fclose() on \"%s\" failed: %s\n",
324 dfile, strerror(errno));
333 * suboption checking functions
338 illegal(char *value, char *opt)
340 fprintf(stderr, "%s: Error: Illegal value \"%s\" for -%s option\n",
341 progname, value, opt);
345 reqval(char opt, char *tab[], int idx)
347 fprintf(stderr, "%s: Error: -%c %s option requires a value\n",
348 progname, opt, tab[idx]);
352 respec(char opt, char *tab[], int idx)
354 fprintf(stderr, "%s: Error: ", progname);
355 fprintf(stderr, "-%c ", opt);
356 if (tab) fprintf(stderr, "%s ", tab[idx]);
357 fprintf(stderr, "option respecified\n");
361 unknown(char opt, char *s)
363 fprintf(stderr, "%s: Error: Unknown option -%c %s\n", progname, opt, s);
369 fprintf(stderr, "Usage: %s [options] filename\n"
371 " -d [nbytes=num,linelength=num, output data properties\n"
372 " seed=string,file=name]\n"
373 " -b [512,1k,4k,16k] where to mark block boundaries\n", progname);