This commit is contained in:
Maix0 2024-05-24 15:28:30 +02:00
parent 6d83a2c196
commit b1cfc0ee71
31 changed files with 133 additions and 89 deletions

View file

@ -15,6 +15,6 @@
# include "me/types.h"
bool me_close(t_file file, t_i32 *error);
bool me_close(int file, t_i32 *error);
#endif

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 15:12:18 by maiboyer #+# #+# */
/* Updated: 2024/05/19 17:08:38 by maiboyer ### ########.fr */
/* Updated: 2024/05/24 15:03:40 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,10 +15,11 @@
#include "me/types.h"
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#if !defined(FILE_SLOT_LEN) || FILE_SLOT_LEN < 64
#if (!defined(FILE_SLOT_LEN)) || FILE_SLOT_LEN < 64
# ifdef FILE_SLOT_LEN
# undef FILE_SLOT_LEN
# endif
@ -47,9 +48,37 @@ typedef enum e_fd_perm
FD_WRITE = 1 << 1,
} t_fd_perm;
typedef enum e_file_open_option
{
FD_CREATE = O_CREAT,
FD_EXCLUSIVE = O_EXCL,
FD_CLOSE_ON_EXEC = O_CLOEXEC,
FD_TRUNCATE = O_TRUNC,
FD_NON_BLOCKING = O_NONBLOCK,
} t_file_open_option;
typedef enum e_file_perm
{
FP_OEXEC = 1 << 0,
FP_OWRITE = 1 << 1,
FP_OREAD = 1 << 2,
FP_GEXEC = 1 << 3,
FP_GWRITE = 1 << 3,
FP_GREAD = 1 << 5,
FP_UEXEC = 1 << 6,
FP_UWRITE = 1 << 7,
FP_UREAD = 1 << 8,
FP_ALL_READ = FP_UREAD | FP_GREAD | FP_OREAD,
FP_ALL_WRITE = FP_UWRITE | FP_GWRITE | FP_OWRITE,
FP_ALL_EXEC = FP_UEXEC | FP_GEXEC | FP_OEXEC,
FP_DEFAULT = FP_UWRITE | FP_ALL_READ,
FP_DEFAULT_EXEC = FP_UWRITE | FP_ALL_EXEC | FP_ALL_READ,
} t_file_perm;
typedef struct s_fd
{
char *name;
t_str name;
int fd;
t_fd_perm perms;
t_fd_type type;
@ -58,19 +87,19 @@ typedef struct s_fd
typedef struct s_dir
{
DIR *ptr;
char *name;
t_str name;
} t_dir;
typedef struct s_file_data
typedef struct s_file
{
FILE *ptr;
char *name;
} t_file_data;
t_str name;
} t_file;
union u_file_slot {
t_fd fd;
t_dir dir;
t_file_data file;
t_fd fd;
t_dir dir;
t_file file;
};
struct s_file_slot
@ -84,8 +113,26 @@ typedef struct s_fd_array
struct s_file_slot storage[FILE_SLOT_LEN];
} t_fd_array;
typedef struct stat t_stat;
typedef struct dirent *t_dir_entry;
/// @brief Get the fd arrays object
/// @return fd_arrays
/// @note internal function used to get the fd arrays
t_fd_array *get_fd_arrays(void);
struct s_file_slot *get_unused_fd_slot(void);
void close_all_fds(void);
void close_all_slots(void);
void close_slot(struct s_file_slot *slot);
t_fd *open_fd(t_str name, t_fd_perm perms, t_file_open_option open_options,
t_file_perm file_perm);
t_error read_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *read_count);
t_error write_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *read_count);
t_error stat_fd(t_fd *fd, t_stat *stat);
void close_fd(t_fd *fd);
t_error open_dir(t_str name, t_dir *dir);
t_error read_dir(t_dir *dir, t_dir_entry *out);
t_error close_dir(t_dir *dir);
#endif /* FS_H */

View file

@ -15,8 +15,8 @@
# include "me/types.h"
t_error me_open(t_const_str path, bool read, bool write, t_file *file_out);
t_error me_open_truncate(t_const_str path, t_file *file_out);
t_error me_open_create(t_const_str path, t_file *file_out);
t_error me_open(t_const_str path, bool read, bool write, int *file_out);
t_error me_open_truncate(t_const_str path, int *file_out);
t_error me_open_create(t_const_str path, int *file_out);
#endif

View file

@ -15,6 +15,6 @@
# include "me/types.h"
void me_putchar_fd(char chr, t_file file);
void me_putchar_fd(char chr, int file);
#endif

View file

@ -15,6 +15,6 @@
# include "me/types.h"
void me_putendl_fd(t_str str, t_file file);
void me_putendl_fd(t_str str, int file);
#endif

View file

@ -15,6 +15,6 @@
# include "me/types.h"
void me_putnbr_fd(t_i32 n, t_file file);
void me_putnbr_fd(t_i32 n, int file);
#endif

View file

@ -15,6 +15,6 @@
# include "me/types.h"
void me_putstr_fd(t_str str, t_file file);
void me_putstr_fd(t_str str, int file);
#endif

View file

@ -15,6 +15,6 @@
# include "me/types.h"
t_usize me_read(t_file fd, t_u8 *buffer, t_i64 buffer_max, bool *eof_out);
t_usize me_read(int fd, t_u8 *buffer, t_i64 buffer_max, bool *eof_out);
#endif

View file

@ -15,6 +15,6 @@
# include "me/types.h"
bool me_write(t_file fd, t_u8 *buffer, t_i64 size);
bool me_write(int fd, t_u8 *buffer, t_i64 size);
#endif

View file

@ -26,7 +26,7 @@
typedef struct s_static_buffer
{
t_file fd;
int fd;
bool used;
char buf[BUFFER_SIZE + 1];
bool init;
@ -38,6 +38,6 @@ typedef struct s_copy_flags
bool empty_read;
} t_copy_flags;
t_string get_next_line(t_file fd, bool *error);
t_string get_next_line(int fd, bool *error);
#endif

View file

@ -18,8 +18,8 @@
/// @brief Pipe structure
typedef struct s_pipe
{
t_file read;
t_file write;
int read;
int write;
} t_pipe;
/// @brief Create a pipe

View file

@ -31,7 +31,7 @@ union u_redirection
{
struct s_fd
{
t_file value;
int value;
} fd;
struct s_piped
{
@ -71,7 +71,7 @@ static inline t_redirection inherited(void)
/// @brief Create a file descriptor redirection
/// @param fd file descriptor to redirect
/// @return the redirection
static inline t_redirection fd(t_file fd)
static inline t_redirection fd(int fd)
{
return ((t_redirection){.tag = R_FD, \
.vals = (union u_redirection){.fd = {.value = fd},}});
@ -91,15 +91,15 @@ union u_wrapped_fd
{
struct s_read_only
{
t_file fd;
int fd;
} ro;
struct s_write_only
{
t_file fd;
int fd;
} wo;
struct s_read_write
{
t_file fd;
int fd;
} rw;
};
@ -113,7 +113,7 @@ typedef struct s_wrapped_fd
/// @brief Create a Read only wrapped file descriptor
/// @param fd file descriptor to wrap
/// @return the wrapped file descriptor
static inline t_wrapped_fd ro(t_file fd)
static inline t_wrapped_fd ro(int fd)
{
return ((t_wrapped_fd){.tag = READ_ONLY,
.vals = (union u_wrapped_fd){
@ -124,7 +124,7 @@ static inline t_wrapped_fd ro(t_file fd)
/// @brief Create a Write only wrapped file descriptor
/// @param fd file descriptor to wrap
/// @return the wrapped file descriptor
static inline t_wrapped_fd wo(t_file fd)
static inline t_wrapped_fd wo(int fd)
{
return ((t_wrapped_fd){.tag = WRITE_ONLY,
.vals = (union u_wrapped_fd){.wo = {.fd = fd}}});

View file

@ -19,7 +19,7 @@
typedef struct s_fprintf_arg
{
t_usize total_print;
t_file fd;
int fd;
} t_fprintf_arg;
typedef enum e_printf_flags

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 14:31:12 by maiboyer #+# #+# */
/* Updated: 2024/05/16 16:12:21 by maiboyer ### ########.fr */
/* Updated: 2024/05/24 14:45:04 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -53,8 +53,6 @@ typedef float t_f32;
/// @brief a 64 bit floating point number
typedef double t_f64;
/// @brief a file descriptor
typedef int t_file;
/// @brief a boolean value that represents an error
/// @note true is an error, false is no error
typedef bool t_error;