generic: hole punching followed by writes in the same range
[xfstests-dev.git] / src / fill.c
1 /*
2  * Copyright (c) 2000-2001 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23
24 /*
25  * fill pathname key nbyte
26  *
27  * use key to seed random number generator so data is deterministic
28  *
29  * lines are at most 72 bytes long so diff(1) does not choke
30  */
31
32 int
33 main(int argc, char **argv)
34 {
35     FILE                *f;
36     unsigned int        seed;
37     long                i;
38     int                 nbyte;
39     char                *hdr;
40     char                *hp;
41     char                c;
42
43     /* quick and dirty, no args checking */
44     if ((f = fopen(argv[1], "w")) == NULL) {
45         fprintf(stderr, "fill: cannot create \"%s\": %s\n", argv[1], strerror(errno));
46         exit(1);
47     }
48     seed = 0;
49     i = 0;
50     while (argv[2][i]) {
51         seed <<= 8;
52         seed |= argv[2][i];
53         i++;
54     }
55     srand(seed);
56
57     nbyte = atoi(argv[3]);
58
59     /*
60      * line format
61      *
62      * byte offset @ start of this line XXXXXXXXXXXX
63      * test iteration number XXXX
64      * key (usually file name) argv[2]
65      * random bytes to fill the line
66      */
67
68     hdr = (char *)malloc(12+1+4+1+strlen(argv[2])+1+1);
69     sprintf(hdr, "%012ld %04d %s ", (long int)0, 0, argv[2]);
70     hp = hdr;
71
72     for (i = 0; i < nbyte-1; i++) {
73         if (*hp) {
74             c = *hp;
75             hp++;
76         }
77         else if ((i % 72) == 71)
78             c = '\n';
79         else
80             c = 32+(rand() % (128-32));
81         fputc(c, f);
82         if (c == '\n') {
83             hp = hdr;
84             sprintf(hdr, "%012ld %04d %s ", i+1, 0, argv[2]);
85         }
86     }
87     fputc('\n', f);
88
89     exit(0);
90 }