-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfshog.c
More file actions
86 lines (73 loc) · 1.72 KB
/
fshog.c
File metadata and controls
86 lines (73 loc) · 1.72 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <errno.h>
#include <error.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define buf_size_in_MB 32
#define buf_size (1024 * 1024 * buf_size_in_MB)
const static char buf [buf_size];
static void
print_usage(const char *prog, int status)
{
fprintf(stderr, "Usage:\n\t%s file sizeInGB\n", prog);
exit (status);
}
int
main (int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr, "wrong number of arguments: %d\n", argc);
print_usage(argv[0], 1);
}
const char *fname = argv[1];
char *p;
errno = 0;
unsigned long int sizeInGB = strtoul(argv[2], &p, 10);
if (sizeInGB == 0 || sizeInGB == ULONG_MAX)
{
if (errno)
{
perror(argv[2]);
print_usage (argv[0], 1);
}
else if (p == argv[2])
{
fprintf (stderr, "an integer expected: %s\n", argv[2]);
print_usage (argv[0], 1);
}
}
int fd = open(fname, O_WRONLY|O_CREAT);
if (fd < 0)
error(1, errno, "failed to open: %s\n", fname);
if (unlink(fname) < 0)
error(1, errno, "failed to unlink: %s\n", fname);
{
if (open(fname, O_WRONLY|O_CREAT) < 0)
error(1, errno, "failed to create: %s\n", fname);
}
fprintf(stderr, "start hogging");
int sizeInXMB = sizeInGB * (1024 / buf_size_in_MB);
int i;
for (i = 0; i < sizeInXMB; i++)
{
int r = write(fd, buf, buf_size);
if (r == buf_size)
fputc('.', stderr);
else if (r < 0)
error(1, errno, "failed to write\n");
else
{
fprintf(stderr, "write buffer %d MB it too large: %d\n",
buf_size_in_MB, r);
exit (1);
}
}
fprintf(stderr, "done. entring sleep.\npress [return] for exiting: ");
getchar ();
return 0;
}