From 03061ed9686cdadd0f67a0449101b61e0ac0fc15 Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Fri, 2 Jan 2015 16:13:53 -0800 Subject: [PATCH] Fallout from refactoring and readdir wrappers. --- include/dirent.h | 21 +++++++++++++++ include/stdlib.h | 5 ++++ lib/libc/SConscript | 1 + lib/libc/dir.c | 61 +++++++++++++++++++++++++++++++++++++++++++ lib/libc/posix/mman.c | 6 ++--- sys/include/dirent.h | 6 +++++ 6 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 include/dirent.h create mode 100644 lib/libc/dir.c diff --git a/include/dirent.h b/include/dirent.h new file mode 100644 index 0000000..7870ef5 --- /dev/null +++ b/include/dirent.h @@ -0,0 +1,21 @@ + +#ifndef __DIRENT_H__ +#define __DIRENT_H__ + +#include + +typedef struct DIR { + int fd; + uint64_t offset; + struct dirent de; +} DIR; + +DIR *opendir(const char *); +struct dirent *readdir(DIR *); +void rewinddir(DIR *); +void seekdir(DIR *, long); +long telldir(DIR *); +int closedir(DIR *); + +#endif /* __DIRENT_H__ */ + diff --git a/include/stdlib.h b/include/stdlib.h index 07ab9cd..75e442c 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -2,6 +2,8 @@ #ifndef __STDLIB_H__ #define __STDLIB_H__ +#include + #ifndef NULL #define NULL ((void *)0) #endif @@ -10,5 +12,8 @@ int atexit(void (*function)(void)); void exit(int status); void abort(void); +void *malloc(size_t sz); +void free(void *buf); + #endif /* __STDLIB_H__ */ diff --git a/lib/libc/SConscript b/lib/libc/SConscript index 39a1be8..dde6ef8 100644 --- a/lib/libc/SConscript +++ b/lib/libc/SConscript @@ -8,6 +8,7 @@ src = [ ] src_common = [ "assert.c", + "dir.c", "exit.c", "file.c", "string.c", diff --git a/lib/libc/dir.c b/lib/libc/dir.c new file mode 100644 index 0000000..c38716b --- /dev/null +++ b/lib/libc/dir.c @@ -0,0 +1,61 @@ + +#include +#include +#include +#include +#include +#include +#include + +DIR * +opendir(const char *path) +{ + DIR *d = (DIR *)malloc(sizeof(DIR)); + + d->fd = OSOpen(path, 0); + if (d->fd < 0) { + free(d); + return NULL; + } + + return d; +} + +struct dirent * +readdir(DIR *d) +{ + int status = OSReadDir(d->fd, (char *)&d->de, sizeof(struct dirent), &d->offset); + if (status == 0) { + return NULL; + } + + return &d->de; +} + +void +rewinddir(DIR *d) +{ + d->offset = 0; +} + +void +seekdir(DIR *d, long offset) +{ + d->offset = offset; +} + +long +telldir(DIR *d) +{ + return d->offset; +} + +int +closedir(DIR *d) +{ + close(d->fd); + free(d); + + return 0; +} + diff --git a/lib/libc/posix/mman.c b/lib/libc/posix/mman.c index f0610a8..34159aa 100644 --- a/lib/libc/posix/mman.c +++ b/lib/libc/posix/mman.c @@ -41,7 +41,7 @@ mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset) } if (flags & MAP_ANON) { - realAddr = SystemMemMap(addr, len, prot | flags); + realAddr = OSMemMap(addr, len, prot | flags); } else { abort(); } @@ -56,7 +56,7 @@ munmap(void *addr, size_t len) { // XXX: Update mappings - return SystemMemUnmap(addr, len); + return OSMemUnmap(addr, len); } int @@ -64,7 +64,7 @@ mprotect(void *addr, size_t len, int prot) { // XXX: Update mappings - return SystemMemProtect(addr, len, prot); + return OSMemProtect(addr, len, prot); } int diff --git a/sys/include/dirent.h b/sys/include/dirent.h index 22c229e..263da7f 100644 --- a/sys/include/dirent.h +++ b/sys/include/dirent.h @@ -14,5 +14,11 @@ struct dirent { char d_name[NAME_MAX]; }; +#define DT_UNKNOWN 0 +#define DT_REG 1 +#define DT_DIR 2 +#define DT_LNK 3 +#define DT_FIFO 4 + #endif /* __SYS_DIRENT_H__ */