generic: new case to test getcwd(2)
[xfstests-dev.git] / src / devzero.c
1 /*
2  * Copyright (c) 2000-2003 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 "global.h"
20
21 int
22 main(int argc, char **argv)
23 {
24         off_t           offset = 0;
25         int             blksize = 4096;
26         long long       maxblks = -1;   /* no limit */
27         long long       nblks = 0;
28         int             value = 0;
29         int             sts = 0;
30         int             fd;
31         char            *z;
32         char            *path;
33         int             oflags = O_WRONLY;
34
35         char    *progname;
36
37         if (strrchr(argv[0],'/'))
38                 progname = strrchr(argv[0],'/')+1;
39         else
40                 progname = argv[0];
41
42         while ((fd = getopt(argc, argv, "b:n:o:v:ct")) != EOF) {
43                 switch (fd) {
44                 case 'b':
45                         blksize = atoi(optarg) * 512;
46                         break;
47                 case 'n':
48                         maxblks = atoll(optarg);
49                         break;
50                 case 'o':
51                         offset = atoll(optarg);
52                         break;
53                 case 'v':
54                         value = atoi(optarg);
55                         break;
56                 case 'c':
57                         oflags |= O_CREAT;
58                         break;
59                 case 't':
60                         oflags |= O_TRUNC;
61                         break;
62                 default:
63                         sts++;
64                 }
65         }
66         if (sts || argc - optind != 1) {
67                 fprintf(stderr,
68                         "Usage: %s [-b N*512] [-n N] [-o off] [-v val] "
69                                 " [-c] [-t] <dev/file>\n",
70                         progname);
71                 fprintf(stderr,"      -c: create -t: truncate\n");
72                 exit(1);
73         }
74
75         path = argv[optind];
76
77         if ((fd = open(path, oflags, 0600)) < 0) {
78                 fprintf(stderr,
79                         "error opening \"%s\": %s\n",
80                         path, strerror(errno));
81                 exit(1);
82         }
83         if ((lseek64(fd, offset, SEEK_SET)) < 0) {
84                 fprintf(stderr, "%s: error seeking to offset %llu "
85                                         "on \"%s\": %s\n",
86                         progname, (unsigned long long)offset, path, strerror(errno));
87                 exit(1);
88         }
89
90         if ((z = memalign(getpagesize(), blksize)) == NULL) {
91                 fprintf(stderr, "%s: can't memalign %u bytes: %s\n",
92                         progname, blksize, strerror(errno));
93                 exit(1);
94         }
95         memset(z, value, blksize);
96         errno = 0;
97         for (;;) {
98                 if (nblks++ == maxblks)
99                         break;
100                 if ((sts = write(fd, z, blksize)) < blksize) {
101                         if (errno == ENOSPC || sts >= 0)
102                                 break;
103                         fprintf(stderr, "%s: write failed: %s\n",
104                                 progname, strerror(errno));
105                         break;
106                 }
107         }
108         printf("Wrote %.2fKb (value 0x%x)\n",
109                 (double) ((--nblks) * blksize) / 1024, value);
110         free(z);
111         return 0;
112 }