_scratch_mkfs_geom(): Filter out 'k' suffix from fs block size
[xfstests-dev.git] / dmapi / src / suite1 / cmd / make_sparse.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2001 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6
7 /*
8  *
9  */
10
11 #include <sys/types.h>
12
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <malloc.h>
16 #include <stdio.h>
17 #include <unistd.h>
18
19 #include <stdlib.h>
20 #include <string.h>
21
22 char *  Progname;
23
24
25 static void
26 Usage(void)
27 {
28         fprintf(stderr,"Usage: %s filename\n", Progname);
29         exit(1);
30 }
31
32
33 int
34 main(
35         int     argc,
36         char    **argv)
37 {
38         char    *pathname;
39         u_int   buflen;
40         char    *buf;
41         ssize_t offset;
42         ssize_t count;
43         int     fd;
44         int     i;
45
46         Progname = argv[0];
47
48         if (argc != 2)
49                 Usage();
50         pathname = argv[1];
51
52         /* Create the file and make it a regular file. */
53
54         if ((fd = open(pathname, O_RDWR|O_CREAT|O_EXCL, 0600)) < 0) {
55                 fprintf(stderr,"%s: Cannot open %s, %s\n", Progname,
56                         pathname, strerror(errno));
57                 exit(1);
58         }
59
60         /* Malloc and zero a buffer to use for writes. */
61
62         buflen = 1;
63         if ((buf = malloc(buflen)) == NULL) {
64                 fprintf(stderr,"%s: malloc(%d) returned NULL\n",
65                         Progname, buflen);
66                 exit(1);
67         }
68         memset(buf, '\0', buflen);
69
70         for (i = 0; i < 200; i += 2) {
71                 offset = i * 65536;
72                 if (lseek(fd, offset, SEEK_SET) < 0) {
73                         fprintf(stderr, "seek to %zd failed, %s\n", offset,
74                                 strerror(errno));
75                         exit(1);
76                 }
77                 if ((count = write(fd, buf, buflen)) < 0) {
78                         fprintf(stderr, "write of %d bytes failed at offset "
79                                 "%zd, , %s\n", buflen, offset, strerror(errno));
80                         exit(1);
81                 }
82                 if (count != buflen) {
83                         fprintf(stderr, "expected to write %d bytes at offset "
84                                 "%zd, actually wrote %zd\n", buflen, offset,
85                                 count);
86                         exit(1);
87                 }
88         }
89         exit(0);
90 }