ead7390e2d9da85a387cf289718500ecf13a3249
[xfstests-dev.git] / dmapi / src / suite1 / cmd / make_sparse.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 /*
20  *
21  */
22
23 #include <sys/types.h>
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <malloc.h>
28 #include <stdio.h>
29 #include <unistd.h>
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 char *  Progname;
35
36
37 static void
38 Usage(void)
39 {
40         fprintf(stderr,"Usage: %s filename\n", Progname);
41         exit(1);
42 }
43
44
45 int
46 main(
47         int     argc,
48         char    **argv)
49 {
50         char    *pathname;
51         u_int   buflen;
52         char    *buf;
53         ssize_t offset;
54         ssize_t count;
55         int     fd;
56         int     i;
57
58         Progname = argv[0];
59
60         if (argc != 2)
61                 Usage();
62         pathname = argv[1];
63
64         /* Create the file and make it a regular file. */
65
66         if ((fd = open(pathname, O_RDWR|O_CREAT|O_EXCL, 0600)) < 0) {
67                 fprintf(stderr,"%s: Cannot open %s, %s\n", Progname,
68                         pathname, strerror(errno));
69                 exit(1);
70         }
71
72         /* Malloc and zero a buffer to use for writes. */
73
74         buflen = 1;
75         if ((buf = malloc(buflen)) == NULL) {
76                 fprintf(stderr,"%s: malloc(%d) returned NULL\n",
77                         Progname, buflen);
78                 exit(1);
79         }
80         memset(buf, '\0', buflen);
81
82         for (i = 0; i < 200; i += 2) {
83                 offset = i * 65536;
84                 if (lseek(fd, offset, SEEK_SET) < 0) {
85                         fprintf(stderr, "seek to %zd failed, %s\n", offset,
86                                 strerror(errno));
87                         exit(1);
88                 }
89                 if ((count = write(fd, buf, buflen)) < 0) {
90                         fprintf(stderr, "write of %d bytes failed at offset "
91                                 "%zd, , %s\n", buflen, offset, strerror(errno));
92                         exit(1);
93                 }
94                 if (count != buflen) {
95                         fprintf(stderr, "expected to write %d bytes at offset "
96                                 "%zd, actually wrote %zd\n", buflen, offset,
97                                 count);
98                         exit(1);
99                 }
100         }
101         exit(0);
102 }