dmapi: fix build failure if libdm is provided via -ldm
[xfstests-dev.git] / dmapi / src / suite1 / cmd / security_hole2.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 <fcntl.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23
24 /*
25         To read unallocated disk blocks in a filesystem, do
26                 cc -o test thisfile.c
27                 ./test myfile
28                 cat myfile
29 */
30
31 extern  char    *sys_errlist[];
32 extern  int     optind;
33 extern  char    *optarg;
34
35 char    *Progname;
36
37
38 static void
39 usage(void)
40 {
41         int     i;
42
43         fprintf(stderr, "usage:\t%s [-s size] pathname\n", Progname);
44         fprintf(stderr, "\t-s size\t\tsize of file (default 10,000,000 bytes)\n");
45         exit(1);
46 }
47
48
49 int
50 main(
51         int     argc, 
52         char    **argv)
53 {
54         char    *pathname = NULL;
55         off_t   size = 10000000;
56         char    buff[1];
57         int     method = F_RESVSP;
58         flock_t flock;
59         int     opt;
60         int     fd;
61
62         Progname = strrchr(argv[0], '/');
63         if (Progname) {
64                 Progname++;
65         } else {
66                 Progname = argv[0];
67         }
68
69         /* Crack and validate the command line options. */
70
71         while ((opt = getopt(argc, argv, "s:")) != EOF) {
72                 switch (opt) {
73                 case 's':
74                         size = atol(optarg);
75                         break;
76                 case '?':
77                         usage();
78                 }
79         }
80         if (optind + 1 != argc)
81                 usage();
82         pathname = argv[optind];
83
84         /* Create the file and write one byte at a large offset to create a
85            big hole in the middle of the file.
86         */
87
88         if ((fd = open(pathname, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) {
89                 perror("open failed");
90                 exit(1);
91         }
92
93         /* First allocate uninitialized blocks. */
94
95         flock.l_whence = 0;
96         flock.l_start = 0;
97         flock.l_len = size;
98
99         if (fcntl(fd, method, &flock) < 0) {
100                 perror("fcntl failed");
101                 exit(1);
102         }
103
104         /* Now seek out and write the byte. */
105
106         if (lseek(fd, size, 0) < 0) {
107                 perror("lseek failed");
108                 exit(1);
109         }
110         buff[0] = '\0';
111         if (write(fd, buff, 1) != 1) {
112                 perror("write failed");
113                 exit(1);
114         }
115         printf("%s created\n", pathname);
116 }