summaryrefslogtreecommitdiff
path: root/user/lib/fopen.c
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-04-30 10:50:37 -0400
committerFreya Murphy <freya@freyacat.org>2025-04-30 11:25:35 -0400
commitc920ea363635ae03e3c3575271d9acbe29d63e6d (patch)
tree50b8c0ca17f3973713fd0d9acb9d904e9f8fd1bb /user/lib/fopen.c
parentmake malloc work (diff)
downloadcomus-c920ea363635ae03e3c3575271d9acbe29d63e6d.tar.gz
comus-c920ea363635ae03e3c3575271d9acbe29d63e6d.tar.bz2
comus-c920ea363635ae03e3c3575271d9acbe29d63e6d.zip
inflate / deflate
Diffstat (limited to '')
-rw-r--r--user/lib/fopen.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/user/lib/fopen.c b/user/lib/fopen.c
new file mode 100644
index 0000000..cb2529f
--- /dev/null
+++ b/user/lib/fopen.c
@@ -0,0 +1,37 @@
+#include <stdio.h>
+#include <unistd.h>
+
+FILE *fopen(const char *restrict filename, const char *restrict modes)
+{
+ int flags = 0;
+ int fd;
+
+ char c;
+ while (c = *modes, c) {
+ switch (c) {
+ case 'r':
+ flags |= O_RDONLY;
+ break;
+ case 'w':
+ flags |= O_CREATE | O_WRONLY;
+ break;
+ case 'a':
+ flags |= O_APPEND;
+ break;
+ case '+':
+ flags |= O_RDWR;
+ break;
+ default:
+ return NULL;
+ }
+ }
+
+ if (flags == 0)
+ return NULL;
+
+ fd = open(filename, flags);
+ if (fd < 0)
+ return NULL;
+
+ return (FILE *)(uintptr_t)fd;
+}