summaryrefslogtreecommitdiff
path: root/src/libc
diff options
context:
space:
mode:
Diffstat (limited to 'src/libc')
-rw-r--r--src/libc/dirent.c20
-rw-r--r--src/libc/include/dirent.h1
2 files changed, 10 insertions, 11 deletions
diff --git a/src/libc/dirent.c b/src/libc/dirent.c
index c2d1b9c..61d8a48 100644
--- a/src/libc/dirent.c
+++ b/src/libc/dirent.c
@@ -4,22 +4,20 @@
#include <stdlib.h>
DIR *opendir(const char *name) {
- FILE *fp = NULL;
- DIR *dir = NULL;
- fp = fopen(name, "r");
- if (!fp) {
- goto err;
- }
+ return opendir_f(fopen(name, "r"));
+}
+
+DIR *opendir_f(FILE *fp) {
+ if (!fp) return NULL;
+
+ DIR *dir;
dir = calloc(1, sizeof *dir);
if (!dir) {
- goto err;
+ fclose(fp);
+ return NULL;
}
dir->fp = fp;
return dir;
-err:
- if (fp) fclose(fp);
- free(dir);
- return NULL;
}
int closedir(DIR *dir) {
diff --git a/src/libc/include/dirent.h b/src/libc/include/dirent.h
index 7c419d7..bfef9ec 100644
--- a/src/libc/include/dirent.h
+++ b/src/libc/include/dirent.h
@@ -12,5 +12,6 @@ typedef struct {
} DIR;
DIR *opendir(const char *name);
+DIR *opendir_f(FILE *fp);
int closedir(DIR *dir);
struct dirent *readdir(DIR *dir);