From e479393a9c18c99295ec11ce5a59b440a3709917 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Tue, 21 Dec 2021 18:47:01 +0100 Subject: [PATCH] unistd, dirent: definitions for directory functions --- include/dirent.h | 60 ++++++++++++++++++++++++++++ include/errno.h | 2 + include/sys/types.h | 1 + include/target/gint/bits/types/DIR.h | 12 ++++++ include/unistd.h | 6 +++ src/libc/string/strerror.c | 2 + 6 files changed, 83 insertions(+) create mode 100644 include/dirent.h create mode 100644 include/target/gint/bits/types/DIR.h diff --git a/include/dirent.h b/include/dirent.h new file mode 100644 index 0000000..99773ac --- /dev/null +++ b/include/dirent.h @@ -0,0 +1,60 @@ +#ifndef __DIRENT_H__ +# define __DIRENT_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#define DT_UNKNOWN 0 /* Unknown type */ +#define DT_REG 1 /* Regular file */ +#define DT_DIR 2 /* Directory */ + +/* In gint d_ino is unused; but it's required by POSIX.1. */ +struct dirent { + /* Inode number */ + ino_t d_ino; + /* Type, to avoid doing an additional stat() on the file */ + unsigned char d_type; + /* NUL-terminated file name */ + char d_name[]; +}; + +/* The underlying syscall to access directory entries is getdents(2). But it + has no unified interface and no glibc wrapper on Linux; it's basically + hidden. So we define the API directly at the level of readdir(3). gint does + not implement any lower-level interface and directly associates a DIR * with + a directory file descriptor. */ + +/* Open a directory to inspect its contents. */ +extern DIR *opendir(const char *__name); + +/* Get the directory file descriptory associated with __dp. */ +extern int dirfd(DIR *__dp); + +/* Open a directory file descriptor to use as a directory stream. */ +extern DIR *fdopendir(int __fd); + +/* Read an entry from an open directory. */ +extern struct dirent *readdir(DIR *__dp); + +/* Get the current position within the directory. The particular value should + not be interpreted; it is just suitable to use in seekdir(). */ +extern long telldir(DIR *__dp); + +/* Seek into the directory; __pos should have been returned by telldir(). */ +extern void seekdir(DIR *__dp, long __pos); + +/* Rewind a directory to its start. */ +extern void rewinddir(DIR *__dp); + +/* Close an open directory. */ +extern int closedir(DIR *__dp); + +#ifdef __cplusplus +} +#endif + +#endif /*__DIRENT_H__*/ diff --git a/include/errno.h b/include/errno.h index 995df33..7941df6 100644 --- a/include/errno.h +++ b/include/errno.h @@ -23,6 +23,8 @@ extern int errno; #define ENOTSUP 12 /* Operation not supported. */ #define EBADF 13 /* Invalid file descriptor. */ #define ESPIPE 14 /* File descriptor is unable to seek. */ +#define EISDIR 15 /* File descriptor is a directory. */ +#define ENOTDIR 16 /* File descriptor is not a directory. */ #ifdef __cplusplus } diff --git a/include/sys/types.h b/include/sys/types.h index f287609..25c39eb 100644 --- a/include/sys/types.h +++ b/include/sys/types.h @@ -25,6 +25,7 @@ typedef int32_t pid_t; typedef int16_t mode_t; typedef uint16_t dev_t; typedef uint16_t umode_t; +typedef uint32_t ino_t; #ifdef __cplusplus } diff --git a/include/target/gint/bits/types/DIR.h b/include/target/gint/bits/types/DIR.h new file mode 100644 index 0000000..4b61693 --- /dev/null +++ b/include/target/gint/bits/types/DIR.h @@ -0,0 +1,12 @@ +#ifndef __BITS_TYPES_DIR_H__ +# define __BITS_TYPES_DIR_H__ + +#include +#include + +typedef struct { + /* Associated directory descriptor */ + int fd; +} DIR; + +#endif /*__BITS_TYPES_DIR_H__*/ diff --git a/include/unistd.h b/include/unistd.h index ae04257..a641d15 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -35,6 +35,12 @@ extern int close(int __fd); /* Remove a file. */ extern int unlink(const char *__path); +/* Create a directory. */ +extern int mkdir(const char *__path, mode_t __mode); + +/* Remove an empty directory. */ +extern int rmdir(const char *__path); + /* Kernel-style functions supported only by Vhex. */ diff --git a/src/libc/string/strerror.c b/src/libc/string/strerror.c index ca56fd1..1271e5a 100644 --- a/src/libc/string/strerror.c +++ b/src/libc/string/strerror.c @@ -17,6 +17,8 @@ static char *errno_strings [] = { [ENOTSUP] = "Operation not supported", [EBADF] = "Bad file descriptor", [ESPIPE] = "Illegal seek", + [EISDIR] = "Is a directory", + [ENOTDIR] = "Not a directory", }; char *strerror(int e)