_scratch_mkfs_geom(): Filter out 'k' suffix from fs block size
[xfstests-dev.git] / dmapi / src / suite1 / cmd / security_hole2.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2001 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11
12 /*
13         To read unallocated disk blocks in a filesystem, do
14                 cc -o test thisfile.c
15                 ./test myfile
16                 cat myfile
17 */
18
19 extern  char    *sys_errlist[];
20 extern  int     optind;
21 extern  char    *optarg;
22
23 char    *Progname;
24
25
26 static void
27 usage(void)
28 {
29         int     i;
30
31         fprintf(stderr, "usage:\t%s [-s size] pathname\n", Progname);
32         fprintf(stderr, "\t-s size\t\tsize of file (default 10,000,000 bytes)\n");
33         exit(1);
34 }
35
36
37 int
38 main(
39         int     argc, 
40         char    **argv)
41 {
42         char    *pathname = NULL;
43         off_t   size = 10000000;
44         char    buff[1];
45         int     method = F_RESVSP;
46         flock_t flock;
47         int     opt;
48         int     fd;
49
50         Progname = strrchr(argv[0], '/');
51         if (Progname) {
52                 Progname++;
53         } else {
54                 Progname = argv[0];
55         }
56
57         /* Crack and validate the command line options. */
58
59         while ((opt = getopt(argc, argv, "s:")) != EOF) {
60                 switch (opt) {
61                 case 's':
62                         size = atol(optarg);
63                         break;
64                 case '?':
65                         usage();
66                 }
67         }
68         if (optind + 1 != argc)
69                 usage();
70         pathname = argv[optind];
71
72         /* Create the file and write one byte at a large offset to create a
73            big hole in the middle of the file.
74         */
75
76         if ((fd = open(pathname, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) {
77                 perror("open failed");
78                 exit(1);
79         }
80
81         /* First allocate uninitialized blocks. */
82
83         flock.l_whence = 0;
84         flock.l_start = 0;
85         flock.l_len = size;
86
87         if (fcntl(fd, method, &flock) < 0) {
88                 perror("fcntl failed");
89                 exit(1);
90         }
91
92         /* Now seek out and write the byte. */
93
94         if (lseek(fd, size, 0) < 0) {
95                 perror("lseek failed");
96                 exit(1);
97         }
98         buff[0] = '\0';
99         if (write(fd, buff, 1) != 1) {
100                 perror("write failed");
101                 exit(1);
102         }
103         printf("%s created\n", pathname);
104 }