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
|
#include <stdio.h>
#include <assert.h>
#include <string.h>
#ifdef USE_HOST_LIBC
#define TEST_FILE "fopen-host-libc.tmp"
#else
#define TEST_FILE "fopen.tmp"
#endif
int main() {
FILE *file;
char str[] = "mlibc fopen test";
char str2[] = " mlibc appending";
char completestr[] = "mlibc fopen test mlibc appending";
char buffer[100];
char buffer2[100];
// Clear all the buffers to zero.
memset(buffer, 0, sizeof(buffer));
memset(buffer2, 0, sizeof(buffer2));
// Open the file for writing.
file = fopen(TEST_FILE, "w");
assert(file);
// Write string minus null terminator, flush and close.
fwrite(str, 1, sizeof(str) - 1, file);
fflush(file);
fclose(file);
// Open the file for reading.
file = fopen(TEST_FILE, "r");
assert(file);
// Verify that we read back the written string and close the file.
assert(fread(buffer, 1, sizeof(str) - 1, file));
assert(!strcmp(buffer, str));
fclose(file);
// Open the file in appending mode, append string 2 (minus the null terminator) to the file, flush and close.
file = fopen(TEST_FILE, "a");
fwrite(str2, 1, sizeof(str2) - 1, file);
fflush(file);
fclose(file);
// Open the file for reading again, verify the contents, close the file and return.
file = fopen(TEST_FILE, "r");
assert(fread(buffer2, 1, sizeof(completestr) - 1, file));
assert(!strcmp(buffer2, completestr));
fclose(file);
// Check that stdout, stdin and stderr can be closed by the application (issue #12).
fclose(stdout);
fclose(stdin);
fclose(stderr);
return 0;
}
|