aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/ansi/fopen.c
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2024-03-07 17:28:00 -0500
committerIan Moffett <ian@osmora.org>2024-03-07 17:28:32 -0500
commitbd5969fc876a10b18613302db7087ef3c40f18e1 (patch)
tree7c2b8619afe902abf99570df2873fbdf40a4d1a1 /lib/mlibc/tests/ansi/fopen.c
parenta95b38b1b92b172e6cc4e8e56a88a30cc65907b0 (diff)
lib: Add mlibc
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/mlibc/tests/ansi/fopen.c')
-rw-r--r--lib/mlibc/tests/ansi/fopen.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/mlibc/tests/ansi/fopen.c b/lib/mlibc/tests/ansi/fopen.c
new file mode 100644
index 0000000..f4818b2
--- /dev/null
+++ b/lib/mlibc/tests/ansi/fopen.c
@@ -0,0 +1,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;
+}