1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
10 * Derived from fill.c, adds command line options, block boundary marking
11 * and allows me to tweak to suit fill2fs without breaking other qa tests.
12 * This version marks block boundaries (512, 1k, 4k and 16k) within the file
13 * using characters guaranteed not to appear anywhere else, this may allow
14 * simple checks in future which can inspect these offsets and ensure one of
15 * of the four characters is present. Also fixes off-by-one error in fill.c
16 * and is more careful about checking when write operations fail - this is
17 * needed by fill2fs to ensure that the number of bytes written is accurately
23 #define constpp char * const *
25 #define N(x) (sizeof(x)/sizeof(x[0]))
27 /* function prototypes */
28 static void illegal(char *, char *);
29 static void reqval(char, char * [], int);
30 static void respec(char, char * [], int);
31 static void unknown(char, char *);
32 static void usage(void);
35 char *dopts[] = { "nbytes", "linelength", "seed", "file", NULL };
36 enum { D_NBYTES, D_LINELENGTH, D_SEED, D_FILE, D_ISSET, D_NUM };
37 int dflags[D_NUM] = { 0 };
38 char *bopts[] = { "512", "1k", "4k", "16k" };
39 enum { B_512, B_1K, B_4K, B_16K, B_ISSET, B_NUM };
40 int bflags[B_NUM] = { 0 };
48 /* block boundaries which should be flagged in the output file */
49 /* flag is the character that should be used to indicate each type */
50 /* of block boundary */
59 static struct s_block_type btypes[] = {
60 { 0, 512, '!' }, /* 512 */
61 { 0, 1024, '"' }, /* 1k */
62 { 0, 4096, '#' }, /* 4k */
63 { 0, 16384, '$' }, /* 16k */
72 main(int argc, char **argv)
74 int status = 0; /* return status */
75 int c; /* current option character */
76 char *p; /* for getsubopt calls */
77 long nbytes; /* total number of bytes to write */
78 int dlen; /* length of normal output data line */
79 const char *dseed = NULL; /* input string for seeding rand */
80 unsigned int seed; /* seed for output data */
81 char *dfile = NULL; /* where to write output */
83 FILE *f; /* output file */
84 char *dbuf; /* output line buffer */
85 char bbuf[50]; /* block boundary string */
86 char *active = NULL; /* active buffer to copy out of */
87 size_t hlen; /* header length (bytes+key) in output */
89 char *hptr; /* pointer to end of header */
90 char *ptr; /* current position to copy from */
91 int blktype = 0; /* current block boundary type */
92 int boundary; /* set if current output char lies on */
101 progname = basename(argv[0]);
102 for (p = progname; *p; p++) {
107 nbytes = 1024 * 1024;
108 dlen = 73; /* includes the trailing newline */
110 while ((c = getopt(argc, argv, "d:b:")) != EOF) {
114 respec('d', NULL, 0);
120 switch (getsubopt(&p, (constpp)dopts, &value)) {
122 if (! value) reqval('d', dopts, D_NBYTES);
123 if (dflags[D_NBYTES]) respec('d', dopts, D_NBYTES);
124 dflags[D_NBYTES] = 1;
125 nbytes = strtol(value, &ptr, 10);
126 if (ptr == value) illegal(value, "d nbytes");
130 if (! value) reqval('d', dopts, D_LINELENGTH);
131 if (dflags[D_LINELENGTH]) respec('d', dopts, D_LINELENGTH);
132 dflags[D_LINELENGTH] = 1;
133 dlen = (int) strtol(value, &ptr, 10) + 1;
134 if (ptr == value) illegal(value, "d linelength");
138 if (! value) reqval('d', dopts, D_SEED);
139 if (dflags[D_SEED]) respec('D', dopts, D_SEED);
145 if (! value) reqval('d', dopts, D_FILE);
146 if (dflags[D_FILE]) respec('d', dopts, D_FILE);
159 respec('b', NULL, 0);
165 switch (getsubopt(&p, (constpp)bopts, &value)) {
167 if (value) illegal(value, "b 512");
168 if (bflags[B_512]) respec('b', bopts, B_512);
174 if (value) illegal(value, "b 1k");
175 if (bflags[B_1K]) respec('b', bopts, B_1K);
181 if (value) illegal(value, "b 4k");
182 if (bflags[B_4K]) respec('b', bopts, B_4K);
188 if (value) illegal(value, "b 16k");
189 if (bflags[B_16K]) respec('b', bopts, B_16K);
206 if (dflags[D_FILE] && optind != argc)
209 if (! dflags[D_FILE] && argc - optind != 1)
212 if (! dflags[D_FILE])
213 dfile = argv[optind];
215 if ((f = fopen(dfile, "w")) == NULL) {
216 fprintf(stderr, "fill2: cannot create \"%s\": %s\n",
217 dfile, strerror(errno));
222 if (! dflags[D_SEED])
225 /* seed is an ascii string */
236 /* normal data line format: XXXXXXXXXXXX CCCC...CCC CCCCCCCCCCCC...CCC */
237 /* block boundary format: CXXXXXXX */
239 dbuf = (char *) malloc(dlen + 1);
240 hlen = 12+1+strlen(dseed)+1;
244 for (i = 0; i < nbytes; i++) {
247 /* is this a block boundary? check largest to smallest */
249 for (j = N(btypes) - 1; j >= 0; j--) {
250 if (btypes[j].mark) {
251 /* first time through or just before a block boundary? */
252 if (i == 0 || i % btypes[j].size == btypes[j].size - 1) {
261 /* are there block boundaries to check? */
262 /* is this the first time through? */
263 /* block boundaries take priority over other output */
264 if (bflags[B_ISSET] && (i == 0 || boundary)) {
265 sprintf(bbuf, "%s%c%07ld\n",
267 btypes[blktype].flag,
268 /* remember i is one less than block boundary */
269 i ? (i+1) / btypes[blktype].size : 0);
273 /* are we at the start of a new line? */
275 || (active == bbuf && *ptr == '\0')
276 || (active == dbuf && l == dlen))
278 sprintf(dbuf, "%012ld %s ", i, dseed);
279 assert(*hptr == '\0');
280 for (ptr = hptr; ptr != dbuf + dlen - 1; ptr++) {
281 /* characters upto 126 '~' are used */
282 /* do not use !"#$ - leave free for use as block markers */
283 *ptr = 37 + (rand() % (127 - 37));
287 assert(ptr == dbuf + dlen);
293 /* continue copying from the active buffer */
296 /* output one new character from current buffer */
297 if (fputc(*ptr++, f) == EOF) {
298 fprintf(stderr, "fill2: could not write character to \"%s\": %s\n",
299 dfile, strerror(errno));
303 if (active == dbuf) l++;
309 /* close file and flush buffers - check if this fails */
310 if (fclose(f) != 0) {
311 fprintf(stderr, "fill2: fclose() on \"%s\" failed: %s\n",
312 dfile, strerror(errno));
321 * suboption checking functions
326 illegal(char *value, char *opt)
328 fprintf(stderr, "%s: Error: Illegal value \"%s\" for -%s option\n",
329 progname, value, opt);
333 reqval(char opt, char *tab[], int idx)
335 fprintf(stderr, "%s: Error: -%c %s option requires a value\n",
336 progname, opt, tab[idx]);
340 respec(char opt, char *tab[], int idx)
342 fprintf(stderr, "%s: Error: ", progname);
343 fprintf(stderr, "-%c ", opt);
344 if (tab) fprintf(stderr, "%s ", tab[idx]);
345 fprintf(stderr, "option respecified\n");
349 unknown(char opt, char *s)
351 fprintf(stderr, "%s: Error: Unknown option -%c %s\n", progname, opt, s);
357 fprintf(stderr, "Usage: %s [options] filename\n"
359 " -d [nbytes=num,linelength=num, output data properties\n"
360 " seed=string,file=name]\n"
361 " -b [512,1k,4k,16k] where to mark block boundaries\n", progname);