1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
| #include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#ifndef HOLE_FILE_NAME
#error You need to define HOLE_FILE_NAME
#endif
#define MAX_CHAR_LINE (256)
#define HOLE_GAP (1024*1024*1024) /* 1G */
int main(void)
{
char line[MAX_CHAR_LINE] = {0};
int rval = 0;
int fd = 0;
int i = 0;
off_t lseek_offset = 0;
ssize_t write_size = 0;
/* Open file */
fd = open(HOLE_FILE_NAME, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
if (fd == -1) {
fprintf(stderr, "%d: %s\n", __LINE__, strerror(errno));
return -1;
}
/* Get string from stdin */
fprintf(stderr, "Enter any text: ");
fgets(line, MAX_CHAR_LINE, stdin);
fprintf(stderr, "Get %s, used to create test hole file\n", line);
/* Implent hole */
for (i = 0; i < strlen(line); i++) {
write_size = write(fd, &line[i], sizeof(char));
if (write_size == -1) {
fprintf(stderr, "%d: %s\n", __LINE__, strerror(errno));
return -1;
}
/* file in the hole! */
lseek_offset = lseek(fd, HOLE_GAP, SEEK_CUR);
if (lseek_offset == -1) {
fprintf(stderr, "%d: %s\n", __LINE__, strerror(errno));
return -1;
}
}
/* Close */
rval = close(fd);
if (rval == -1) {
fprintf(stderr, "%d: %s\n", __LINE__, strerror(errno));
return -1;
}
return 0;
}
|