|
1 | 1 | #include <assert.h> |
2 | | -#include <stdio.h> |
| 2 | +#include <fcntl.h> |
3 | 3 | #include <stdlib.h> |
4 | 4 | #include <unistd.h> |
5 | 5 |
|
6 | 6 | int main() { |
7 | 7 | char buf[4]; |
8 | 8 | int fd; |
9 | | - FILE *file; |
10 | 9 | size_t size; |
11 | 10 |
|
12 | | - file = fopen("fs-tests.dir/pwrite.cleanup", "a+"); |
13 | | - assert(file != NULL); |
| 11 | + fd = open("fs-tests.dir/pwrite.cleanup", |
| 12 | + O_CREAT | O_TRUNC | O_WRONLY | O_APPEND); |
| 13 | + assert(fd != -1); |
14 | 14 |
|
15 | | - fd = fileno(file); |
| 15 | + size = write(fd, buf, 2); |
| 16 | + assert(size == 2); |
16 | 17 |
|
17 | | - size = fwrite(buf, 1, 4, file); |
18 | | - assert(size == sizeof(buf)); |
19 | | - fflush(file); |
| 18 | + // test if O_APPEND is working |
| 19 | + assert(lseek(fd, 0, SEEK_SET) == 0); |
| 20 | + size = write(fd, buf, 2); |
| 21 | + assert(size == 2); |
| 22 | + assert(lseek(fd, 0, SEEK_CUR) == 4); |
20 | 23 |
|
21 | | - size = pwrite(fd, buf, 4, 0); |
22 | | - assert(size == sizeof(buf)); |
23 | | - fflush(file); |
| 24 | + size = pwrite(fd, buf, 3, 0); |
| 25 | + assert(size == 3); |
24 | 26 |
|
25 | 27 | // fd_pwrite should write from offset 0 regardless of append. |
| 28 | + // (thus shouln't extend the file.) |
| 29 | + // it shouldn't move the file offset either. |
| 30 | + assert(lseek(fd, 0, SEEK_CUR) == 4); |
26 | 31 | assert(lseek(fd, 0, SEEK_END) == 4); |
27 | 32 |
|
28 | | - fclose(file); |
| 33 | + close(fd); |
29 | 34 |
|
30 | 35 | return EXIT_SUCCESS; |
31 | 36 | } |
0 commit comments