Fallout from refactoring and readdir wrappers.

This commit is contained in:
Ali Mashtizadeh 2015-01-02 16:13:53 -08:00
parent 582f83ca54
commit 03061ed968
6 changed files with 97 additions and 3 deletions

21
include/dirent.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef __DIRENT_H__
#define __DIRENT_H__
#include <sys/dirent.h>
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__ */

View File

@ -2,6 +2,8 @@
#ifndef __STDLIB_H__ #ifndef __STDLIB_H__
#define __STDLIB_H__ #define __STDLIB_H__
#include <sys/types.h>
#ifndef NULL #ifndef NULL
#define NULL ((void *)0) #define NULL ((void *)0)
#endif #endif
@ -10,5 +12,8 @@ int atexit(void (*function)(void));
void exit(int status); void exit(int status);
void abort(void); void abort(void);
void *malloc(size_t sz);
void free(void *buf);
#endif /* __STDLIB_H__ */ #endif /* __STDLIB_H__ */

View File

@ -8,6 +8,7 @@ src = [ ]
src_common = [ src_common = [
"assert.c", "assert.c",
"dir.c",
"exit.c", "exit.c",
"file.c", "file.c",
"string.c", "string.c",

61
lib/libc/dir.c Normal file
View File

@ -0,0 +1,61 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include <syscall.h>
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;
}

View File

@ -41,7 +41,7 @@ mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
} }
if (flags & MAP_ANON) { if (flags & MAP_ANON) {
realAddr = SystemMemMap(addr, len, prot | flags); realAddr = OSMemMap(addr, len, prot | flags);
} else { } else {
abort(); abort();
} }
@ -56,7 +56,7 @@ munmap(void *addr, size_t len)
{ {
// XXX: Update mappings // XXX: Update mappings
return SystemMemUnmap(addr, len); return OSMemUnmap(addr, len);
} }
int int
@ -64,7 +64,7 @@ mprotect(void *addr, size_t len, int prot)
{ {
// XXX: Update mappings // XXX: Update mappings
return SystemMemProtect(addr, len, prot); return OSMemProtect(addr, len, prot);
} }
int int

View File

@ -14,5 +14,11 @@ struct dirent {
char d_name[NAME_MAX]; 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__ */ #endif /* __SYS_DIRENT_H__ */