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