blob: 4cc0322e132712e6a9cfd700fbc9b8adc6ec4303 (
plain)
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
|
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int
file_write (char *filename, int filesize)
{
int fd, ret = 0;
int i = 0;
char buf[1024] = {'a',};
fd = open (filename, O_RDWR|O_CREAT|O_APPEND, 0600);
while (i < filesize) {
ret = write(fd, buf, sizeof(buf));
if (ret == -1) {
close (fd);
return ret;
}
i += sizeof(buf);
ret = fdatasync(fd);
if (ret) {
close (fd);
return ret;
}
}
ret = close(fd);
if (ret)
return ret;
return 0;
}
int
main (int argc, char **argv)
{
if (argc != 3) {
printf("Usage: %s <filename> <size(in bytes)>\n", argv[0]);
return EXIT_FAILURE;
}
printf ("argv[2] is %s\n", argv[2]);
if (file_write (argv[1], atoi(argv[2])) == -1)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
|