summaryrefslogtreecommitdiff
path: root/src/libc/dirent.c
diff options
context:
space:
mode:
authordzwdz2023-08-14 18:51:07 +0200
committerdzwdz2023-08-14 18:51:07 +0200
commit642b5fb0007b64c77d186fcb018d571152ee1d47 (patch)
tree1c466461f3602d306be309a053edae558ef2568e /src/libc/dirent.c
parent8050069c57b729c18c19b1a03ab6e4bf63b4735e (diff)
reorganization: first steps
Diffstat (limited to 'src/libc/dirent.c')
-rw-r--r--src/libc/dirent.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/libc/dirent.c b/src/libc/dirent.c
new file mode 100644
index 0000000..c2d1b9c
--- /dev/null
+++ b/src/libc/dirent.c
@@ -0,0 +1,55 @@
+#include <dirent.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+DIR *opendir(const char *name) {
+ FILE *fp = NULL;
+ DIR *dir = NULL;
+ fp = fopen(name, "r");
+ if (!fp) {
+ goto err;
+ }
+ dir = calloc(1, sizeof *dir);
+ if (!dir) {
+ goto err;
+ }
+ dir->fp = fp;
+ return dir;
+err:
+ if (fp) fclose(fp);
+ free(dir);
+ return NULL;
+}
+
+int closedir(DIR *dir) {
+ fclose(dir->fp);
+ free(dir);
+ return 0;
+}
+
+struct dirent *readdir(DIR *dir) {
+ int i = 0;
+ char *buf = dir->dent.d_name;
+ for (;;) {
+ int c = fgetc(dir->fp);
+ if (c == EOF) {
+ if (i == 0) return NULL;
+ else break;
+ }
+ if (c == '\0') {
+ break;
+ }
+ if (i == sizeof(dir->dent.d_name)-1) {
+ /* overflow */
+ for (;;) {
+ c = fgetc(dir->fp);
+ if (c == EOF || c == '\0') break;
+ }
+ return errno = ENAMETOOLONG, NULL;
+ }
+ buf[i++] = c;
+ }
+ buf[i] = '\0';
+ return &dir->dent;
+}