|
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", O_CREAT | O_TRUNC | O_WRONLY | O_APPEND); |
| 12 | + assert(fd != -1); |
14 | 13 |
|
15 |
| - fd = fileno(file); |
| 14 | + size = write(fd, buf, 2); |
| 15 | + assert(size == 2); |
16 | 16 |
|
17 |
| - size = fwrite(buf, 1, 4, file); |
18 |
| - assert(size == sizeof(buf)); |
19 |
| - fflush(file); |
| 17 | + // test if O_APPEND is working |
| 18 | + assert(lseek(fd, 0, SEEK_SET) == 0); |
| 19 | + size = write(fd, buf, 2); |
| 20 | + assert(size == 2); |
| 21 | + assert(lseek(fd, 0, SEEK_CUR) == 4); |
20 | 22 |
|
21 |
| - size = pwrite(fd, buf, 4, 0); |
22 |
| - assert(size == sizeof(buf)); |
23 |
| - fflush(file); |
| 23 | + size = pwrite(fd, buf, 3, 0); |
| 24 | + assert(size == 3); |
24 | 25 |
|
25 | 26 | // fd_pwrite should write from offset 0 regardless of append.
|
| 27 | + // (thus shouln't extend the file.) |
| 28 | + // it shouldn't move the file offset either. |
| 29 | + assert(lseek(fd, 0, SEEK_CUR) == 4); |
26 | 30 | assert(lseek(fd, 0, SEEK_END) == 4);
|
27 | 31 |
|
28 |
| - fclose(file); |
| 32 | + close(fd); |
29 | 33 |
|
30 | 34 | return EXIT_SUCCESS;
|
31 | 35 | }
|
0 commit comments