unistd, dirent: definitions for directory functions

This commit is contained in:
Lephenixnoir 2021-12-21 18:47:01 +01:00
parent c2feb94710
commit e479393a9c
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495
6 changed files with 83 additions and 0 deletions

60
include/dirent.h Normal file
View file

@ -0,0 +1,60 @@
#ifndef __DIRENT_H__
# define __DIRENT_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <bits/types/DIR.h>
#include <sys/types.h>
#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__*/

View file

@ -23,6 +23,8 @@ extern int errno;
#define ENOTSUP 12 /* Operation not supported. */ #define ENOTSUP 12 /* Operation not supported. */
#define EBADF 13 /* Invalid file descriptor. */ #define EBADF 13 /* Invalid file descriptor. */
#define ESPIPE 14 /* File descriptor is unable to seek. */ #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 #ifdef __cplusplus
} }

View file

@ -25,6 +25,7 @@ typedef int32_t pid_t;
typedef int16_t mode_t; typedef int16_t mode_t;
typedef uint16_t dev_t; typedef uint16_t dev_t;
typedef uint16_t umode_t; typedef uint16_t umode_t;
typedef uint32_t ino_t;
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -0,0 +1,12 @@
#ifndef __BITS_TYPES_DIR_H__
# define __BITS_TYPES_DIR_H__
#include <stddef.h>
#include <stdint.h>
typedef struct {
/* Associated directory descriptor */
int fd;
} DIR;
#endif /*__BITS_TYPES_DIR_H__*/

View file

@ -35,6 +35,12 @@ extern int close(int __fd);
/* Remove a file. */ /* Remove a file. */
extern int unlink(const char *__path); 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. */ /* Kernel-style functions supported only by Vhex. */

View file

@ -17,6 +17,8 @@ static char *errno_strings [] = {
[ENOTSUP] = "Operation not supported", [ENOTSUP] = "Operation not supported",
[EBADF] = "Bad file descriptor", [EBADF] = "Bad file descriptor",
[ESPIPE] = "Illegal seek", [ESPIPE] = "Illegal seek",
[EISDIR] = "Is a directory",
[ENOTDIR] = "Not a directory",
}; };
char *strerror(int e) char *strerror(int e)