diff --git a/photon/libc/include/dirent.h b/photon/libc/include/dirent.h new file mode 100644 index 0000000..e629d7f --- /dev/null +++ b/photon/libc/include/dirent.h @@ -0,0 +1,14 @@ +#ifndef DIRENT_H_ +#define DIRENT_H_ + +#include + +extern DIR *opendir(const char *path); +extern int closedir(DIR *d); +extern struct dirent *readdir(DIR *d); +extern int readdir_r(DIR *d, struct dirent *entry, struct dirent **result); +extern void rewinddir(DIR *d); +extern void seekdir(DIR *d, long int off); +extern long int telldir(DIR *d); + +#endif diff --git a/photon/libc/include/limits.h b/photon/libc/include/limits.h index 6e98aa7..190af41 100644 --- a/photon/libc/include/limits.h +++ b/photon/libc/include/limits.h @@ -1,6 +1,8 @@ #ifndef LIMITS_H_ #define LIMITS_H_ +#include + #define CHAR_BIT 8 #define SCHAR_MIN -127 #define SCHAR_MAX 127 @@ -30,4 +32,6 @@ #define INT64_MAX LLONG_MAX #define UINT64_MAX ULLONG_MAX +#define NAME_MAX __FILENAME_MAX + #endif /* LIMITS_H_ */ diff --git a/photon/libc/sys/horizon/dirent.c b/photon/libc/sys/horizon/dirent.c new file mode 100644 index 0000000..f55614b --- /dev/null +++ b/photon/libc/sys/horizon/dirent.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +DIR *opendir(const char *path) +{ + int fd = open(path, O_DIRECTORY); + if (fd < 0) { + return NULL; + } + + DIR *out = malloc(sizeof(*out)); + if (!out) { + __set_errno(ENOMEM); + return NULL; + } + + memset(out, 0x0, sizeof(*out)); + out->fd = fd; + + return out; +} + +int closedir(DIR *d) +{ + close(d->fd); + free(d); + return 0; +} + +struct dirent *readdir(DIR *d) +{ + mio_dirent dent; + int r = mio_getdents(d->fd, &dent, 1); + if (r < 1) { + __set_errno(-r); + return NULL; + } + + size_t name_max = sizeof(d->current_dent.d_name); + if (sizeof(dent.d_name) < name_max) { + name_max = sizeof(dent.d_name); + } + + d->current_dent.d_ino = dent.d_ino; + d->current_dent.d_type = dent.d_type; + memcpy(d->current_dent.d_name, dent.d_name, name_max); + + return &d->current_dent; +} + +int readdir_r(DIR *d, struct dirent *entry, struct dirent **result) +{ + mio_dirent dent; + int r = mio_getdents(d->fd, &dent, 1); + if (r < 1) { + *result = NULL; + __set_errno(-r); + return -1; + } + + size_t name_max = sizeof(d->current_dent.d_name); + if (sizeof(dent.d_name) < name_max) { + name_max = sizeof(dent.d_name); + } + + d->current_dent.d_ino = dent.d_ino; + d->current_dent.d_type = dent.d_type; + memcpy(d->current_dent.d_name, dent.d_name, name_max); + memcpy(entry, &d->current_dent, sizeof(*entry)); + *result = entry; + + return 0; +} + +void rewinddir(DIR *d) +{ + mio_seek(d->fd, 0, SEEK_SET); +} + +void seekdir(DIR *d, long int off) +{ + mio_seek(d->fd, off, SEEK_SET); +} + +long int telldir(DIR *d) +{ + return mio_seek(d->fd, 0, SEEK_CUR); +} diff --git a/photon/libc/sys/horizon/fcntl.h b/photon/libc/sys/horizon/fcntl.h new file mode 100644 index 0000000..78787b5 --- /dev/null +++ b/photon/libc/sys/horizon/fcntl.h @@ -0,0 +1,31 @@ +#ifndef SYS_HORIZON_FCNTL_H_ +#define SYS_HORIZON_FCNTL_H_ + +#include + +#define O_ACCMODE 0003 +#define O_RDONLY 00 +#define O_WRONLY 01 +#define O_RDWR 02 +#define O_CREAT 0100 +#define O_EXCL 0200 +#define O_NOCTTY 0400 +#define O_TRUNC 01000 +#define O_APPEND 02000 +#define O_NONBLOCK 04000 +#define O_NDELAY O_NONBLOCK +#define O_SYNC 04010000 +#define O_FSYNC O_SYNC +#define O_ASYNC 020000 +#define O_LARGEFILE 0100000 + +#define O_DIRECTORY 0200000 +#define O_NOFOLLOW 0400000 +#define O_CLOEXEC 02000000 +#define O_DIRECT 040000 +#define O_NOATIME 01000000 +#define O_PATH 010000000 +#define O_DSYNC 010000 +#define O_TMPFILE (020000000 | __O_DIRECTORY) + +#endif diff --git a/photon/libc/sys/horizon/fio.c b/photon/libc/sys/horizon/fio.c index c910f84..5f3fa69 100644 --- a/photon/libc/sys/horizon/fio.c +++ b/photon/libc/sys/horizon/fio.c @@ -67,6 +67,10 @@ unsigned int __fio_read(struct __io_file *f, char *buf, unsigned int sz) unsigned int __fio_write(struct __io_file *f, const char *buf, unsigned int sz) { +#if 1 + mx_console_write(buf, sz); + return sz; +#else for (unsigned int i = 0; i < sz; i++) { if (f->buf_idx >= __FIO_BUFSZ) { __fio_flush(f); @@ -79,6 +83,7 @@ unsigned int __fio_write(struct __io_file *f, const char *buf, unsigned int sz) } return sz; +#endif } int __fio_ungetc(struct __io_file *f, char c) diff --git a/photon/libc/sys/horizon/sys/_dirent.h b/photon/libc/sys/horizon/sys/_dirent.h new file mode 100644 index 0000000..6925c4e --- /dev/null +++ b/photon/libc/sys/horizon/sys/_dirent.h @@ -0,0 +1,18 @@ +#ifndef SYS_HORIZON_SYS__DIRENT_H_ +#define SYS_HORIZON_SYS__DIRENT_H_ + +#include +#include + +struct dirent { + ino_t d_ino; + unsigned char d_type; + char d_name[NAME_MAX + 1]; +}; + +typedef struct { + int fd; + struct dirent current_dent; +} DIR; + +#endif diff --git a/photon/libc/sys/horizon/sys/_fconst.h b/photon/libc/sys/horizon/sys/_fconst.h index 1733158..202a7db 100644 --- a/photon/libc/sys/horizon/sys/_fconst.h +++ b/photon/libc/sys/horizon/sys/_fconst.h @@ -1,7 +1,7 @@ -#ifndef SYS_LINUX_SYS__FCONST_H_ -#define SYS_LINUX_SYS__FCONST_H_ +#ifndef SYS_HORIZON_SYS__FCONST_H_ +#define SYS_HORIZON_SYS__FCONST_H_ -#define __FILENAME_MAX 1024 +#define __FILENAME_MAX 255 #define __SEEK_SET 0 #define __SEEK_CUR 1 diff --git a/photon/libc/sys/horizon/sys/types.h b/photon/libc/sys/horizon/sys/types.h index 4a12c7a..50e8620 100644 --- a/photon/libc/sys/horizon/sys/types.h +++ b/photon/libc/sys/horizon/sys/types.h @@ -4,36 +4,4 @@ #include #include -/* TODO move to mio/types.h */ -typedef signed long long blkcnt_t; -typedef signed long long blksize_t; -typedef unsigned long long clock_t; -typedef unsigned int clockid_t; -typedef unsigned long long fsblkcnt_t; -typedef unsigned long long fsfilcnt_t; -typedef unsigned int gid_t; -typedef unsigned int id_t; -typedef unsigned int key_t; -typedef unsigned int mode_t; -typedef unsigned long long nlink_t; -typedef signed long long off_t; -typedef __int64_t off64_t; -typedef signed int pid_t; -typedef unsigned long long time_t; -typedef unsigned int timer_t; -typedef unsigned int uid_t; -typedef unsigned long useconds_t; -typedef unsigned long long dev_t; - -#ifndef __size_t_defined -#define __size_t_defined -typedef __uintptr_t size_t; -#endif - -#ifndef __ssize_t_defined -#define __ssize_t_defined -typedef __intptr_t ssize_t; -#endif - - #endif diff --git a/photon/libc/sys/horizon/unistd.h b/photon/libc/sys/horizon/unistd.h index 11b83de..acec6d4 100644 --- a/photon/libc/sys/horizon/unistd.h +++ b/photon/libc/sys/horizon/unistd.h @@ -16,4 +16,6 @@ extern ssize_t read(int fd, void *buf, size_t count); extern ssize_t write(int fd, const void *buf, size_t count); extern int close(int fd); +extern off_t lseek(int fd, off_t off, int whence); + #endif