-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_pattern.c
More file actions
61 lines (48 loc) · 936 Bytes
/
write_pattern.c
File metadata and controls
61 lines (48 loc) · 936 Bytes
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
/*
* Write a pattern to a file.
*
* (C) 2007 Valerie Henson <val@nmt.edu>
*/
#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define FILE_SIZE (32 * 1024 * 1024)
static char * cmd;
static void usage (void)
{
fprintf(stderr, "Usage: %s <file>\n", cmd);
exit(1);
}
int main (int argc, char * argv[])
{
int fd;
char * file;
char buf[4096];
int n = 0;
int written = 0;
cmd = argv[0];
if (argc != 2)
usage();
file = argv[1];
if ((fd = open(file, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
error(0, errno, "Cannot open file %s", file);
usage();
}
memset(buf, '5', sizeof(buf));
while (written < FILE_SIZE) {
n = write(fd, buf, sizeof(buf));
if (n == -1) {
error(0, errno, "Cannot write file %s", file);
usage();
}
written += n;
}
close(fd);
return 0;
}