summaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/posix/fdopen.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/posix/fdopen.c
parenta95b38b1b92b172e6cc4e8e56a88a30cc65907b0 (diff)
lib: Add mlibc
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/mlibc/tests/posix/fdopen.c')
-rw-r--r--lib/mlibc/tests/posix/fdopen.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/mlibc/tests/posix/fdopen.c b/lib/mlibc/tests/posix/fdopen.c
new file mode 100644
index 0000000..1066bfb
--- /dev/null
+++ b/lib/mlibc/tests/posix/fdopen.c
@@ -0,0 +1,37 @@
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#define TEST_FILE "fdopen.tmp"
+
+int main() {
+ int fd = open(TEST_FILE, O_CREAT | O_RDWR, 0666);
+ assert(fd >= 0);
+
+ char *str = "mlibc fdopen test";
+ assert(write(fd, str, strlen(str)));
+
+ // Seek to the beginning, then reopen with fdopen in append mode.
+ lseek(fd, 0, SEEK_SET);
+ FILE *file = fdopen(fd, "a");
+ assert(file);
+
+ // Append and close.
+ str = " appended";
+ fwrite(str, strlen(str), 1, file);
+ fflush(file);
+ fclose(file);
+
+ // Open it again and check that the append succeeded.
+ fd = open(TEST_FILE, O_RDONLY);
+ assert(fd >= 0);
+ file = fdopen(fd, "r");
+ assert(file);
+ str = "mlibc fdopen test appended";
+ char buf[100] = {0};
+ assert(fread(buf, 1, strlen(str), file));
+ assert(!strcmp(buf, str));
+ fclose(file);
+}