Implemented dirent.h on Horizon

This commit is contained in:
2022-01-08 13:19:58 +00:00
parent 86046975db
commit 794311a0d5
9 changed files with 172 additions and 35 deletions

View File

@@ -0,0 +1,14 @@
#ifndef DIRENT_H_
#define DIRENT_H_
#include <sys/_dirent.h>
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

View File

@@ -1,6 +1,8 @@
#ifndef LIMITS_H_
#define LIMITS_H_
#include <sys/_fconst.h>
#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_ */

View File

@@ -0,0 +1,95 @@
#include <dirent.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <mio/fs.h>
#include <mio/dirent.h>
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);
}

View File

@@ -0,0 +1,31 @@
#ifndef SYS_HORIZON_FCNTL_H_
#define SYS_HORIZON_FCNTL_H_
#include <sys/types.h>
#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

View File

@@ -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)

View File

@@ -0,0 +1,18 @@
#ifndef SYS_HORIZON_SYS__DIRENT_H_
#define SYS_HORIZON_SYS__DIRENT_H_
#include <sys/types.h>
#include <limits.h>
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

View File

@@ -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

View File

@@ -4,36 +4,4 @@
#include <machine/_stdint.h>
#include <mio/types.h>
/* 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

View File

@@ -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