Updated linenoise and printf to better work

This commit is contained in:
Maieul BOYER 2024-07-07 19:50:33 +02:00
parent 0e18e20181
commit 8808f81221
No known key found for this signature in database
28 changed files with 657 additions and 579 deletions

View file

@ -6,14 +6,13 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 15:12:18 by maiboyer #+# #+# */
/* Updated: 2024/05/24 15:03:40 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:50:18 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FS_H
#define FS_H
#include "me/fs/read_to_vec.h"
#include "me/types.h"
#include <dirent.h>
#include <fcntl.h>
@ -78,9 +77,9 @@ typedef enum e_file_open_option
/// `G` means group
/// `U` means user
/// `ALL` means all
/// There are the raw permission, you can combine them to get the permission you want
/// And there are "aliases" that are common permission set
/// @note you can combine them with the `|` operator
/// There are the raw permission, you can combine them to get the permission you
/// want And there are "aliases" that are common permission set
/// @note you can combine them with the `|` operator
typedef enum e_file_perm
{
FP_OEXEC = 1 << 0,
@ -139,7 +138,6 @@ union u_file_slot {
t_file file;
};
/// @brief File slot structure
/// ty: the type of the slot
/// slot: the slot itself
@ -164,46 +162,44 @@ typedef t_const_str t_mode;
/// @note this is a simple typedef because I hate the struct keyword
typedef struct stat t_stat;
/// @brief Directory entry structure
/// @note this is a simple typedef because I hate the struct keyword and it is always behind a pointer
/// @note this is a simple typedef because I hate the struct keyword and it is
/// always behind a pointer
typedef struct dirent *t_dir_entry;
/*_____ _ _ _______ ______ _____ _ _ _
|_ _| \ | |__ __| ____| __ \| \ | | /\ | |
| | | \| | | | | |__ | |__) | \| | / \ | |
| | | . ` | | | | __| | _ /| . ` | / /\ \ | |
_| |_| |\ | | | | |____| | \ \| |\ |/ ____ \| |____
|_____|_| \_| |_| |______|_| \_\_| \_/_/ \_\______|
/*_____ _ _ _______ ______ _____ _ _ _
|_ _| \ | |__ __| ____| __ \| \ | | /\ | |
| | | \| | | | | |__ | |__) | \| | / \ | |
| | | . ` | | | | __| | _ /| . ` | / /\ \ | |
_| |_| |\ | | | | |____| | \ \| |\ |/ ____ \| |____
|_____|_| \_| |_| |______|_| \_\_| \_/_/ \_\______|
*/
/// @brief Get the fd arrays object
/// @return pointer to the files's array
/// @note internal function used to get the files array
t_fd_array *get_fd_arrays(void);
t_fd_array *get_fd_arrays(void);
/// @brief Get the unused fd slot object
/// @return pointer to the unused file slot
/// @note Will abort if no slot is available
struct s_file_slot *get_unused_fd_slot(void);
/// @brief Close all slots
/// @note This is probably NOT what you want
void close_all_slots(void);
void close_all_slots(void);
/// @note Close the given slot
/// @param[in] slot the slot to close
/// @note this is probably NOT what you want
void close_slot(struct s_file_slot *slot);
void close_slot(struct s_file_slot *slot);
/* ______ _____
| ____| __ \
/* ______ _____
| ____| __ \
| |__ | | | |
| __| | | | |
| | | |__| |
|_| |_____/
|_| |_____/
*/
/// @brief Open a file descriptor
@ -212,8 +208,8 @@ void close_slot(struct s_file_slot *slot);
/// @param open_options the options to open the file
/// @param fileperm the file permission
/// @return the file descriptor* on success, NULL otherwise
t_fd *open_fd(t_str name, t_fd_perm perms, t_file_open_option open_options,
t_file_perm file_perm);
t_fd *open_fd(t_str name, t_fd_perm perms, t_file_open_option open_options,
t_file_perm file_perm);
/// @brief Read from a file descriptor
/// @param[in] fd the file descriptor
@ -241,29 +237,28 @@ t_error stat_fd(t_fd *fd, t_stat *stat);
/// @brief Close a file descriptor
/// @param[in] fd the file descriptor
/// @note Will close the file descriptor and free the slot
void close_fd(t_fd *fd);
void close_fd(t_fd *fd);
/// @brief write a number to a file descriptor
/// @note will fail silently if the fd is not open in write mode
void put_number_fd(t_fd *fd, t_u64 number);
void put_number_fd(t_fd *fd, t_u64 number);
/// @brief write a string to a file descriptor
/// @note will fail silently if the fd is not open in write mode
void put_string_fd(t_fd *fd, t_const_str string);
void put_string_fd(t_fd *fd, t_const_str string);
/// @brief write a char to a file descriptor
/// @note will fail silently if the fd is not open in write mode
void put_char_fd(t_fd *fd, t_u8 c);
void put_char_fd(t_fd *fd, t_u8 c);
/* _____ _____ _____ ______ _____ _______ ____ _______ __
| __ \_ _| __ \| ____/ ____|__ __/ __ \| __ \ \ / /
| | | || | | |__) | |__ | | | | | | | | |__) \ \_/ /
| | | || | | _ /| __|| | | | | | | | _ / \ /
| |__| || |_| | \ \| |___| |____ | | | |__| | | \ \ | |
|_____/_____|_| \_\______\_____| |_| \____/|_| \_\ |_|
| | | || | | |__) | |__ | | | | | | | | |__) \ \_/ /
| | | || | | _ /| __|| | | | | | | | _ / \ /
| |__| || |_| | \ \| |___| |____ | | | |__| | | \ \ | |
|_____/_____|_| \_\______\_____| |_| \____/|_| \_\ |_|
*/
/// @brief Open a file
/// @param[in] name the name of the file
/// @param[out] dir the file structure to fill
@ -283,11 +278,11 @@ t_error read_dir(t_dir *dir, t_dir_entry *out);
/// @note Will close the directory and free the slot
void close_dir(t_dir *dir);
/*______ _____ _ ______
/*______ _____ _ ______
| ____|_ _| | | ____|
| |__ | | | | | |__
| __| | | | | | __|
| | _| |_| |____| |____
| |__ | | | | | |__
| __| | | | | | __|
| | _| |_| |____| |____
|_| |_____|______|______|
*/
@ -304,7 +299,8 @@ t_error open_file(t_str name, t_mode mode, t_file **file);
/// @param[in] size the size of the buffer
/// @param[out] read_count the number of bytes read
/// @return true on error, false otherwise
t_error read_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *read_count);
t_error read_file(t_file *file, t_u8 *buffer, t_usize size,
t_isize *read_count);
/// @brief Write to a file
/// @param[in] file the file to write to
@ -313,11 +309,25 @@ t_error read_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *read_count)
/// @param[out] write_count the number of bytes written
/// @return true on error, false otherwise
/// @note write_count can be NULL
t_error write_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *write_count);
t_error write_file(t_file *file, t_u8 *buffer, t_usize size,
t_isize *write_count);
/// @brief Close the underlying file stream
/// @param[in] file the file to close
/// @note Will close the file and free the slot
void close_file(t_file *file);
/* _____ ______ _______ _______ ______ _____ _____
/ ____| ____|__ __|__ __| ____| __ \ / ____|
| | __| |__ | | | | | |__ | |__) | (___
| | |_ | __| | | | | | __| | _ / \___ \
| |__| | |____ | | | | | |____| | \ \ ____) |
\_____|______| |_| |_| |______|_| \_\_____/
*/
//TODO: Documentation!
t_fd *get_stdin(void);
t_fd *get_stdout(void);
t_fd *get_stderr(void);
#endif /* FS_H */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 15:43:08 by maiboyer #+# #+# */
/* Updated: 2024/01/06 18:39:58 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 18:25:23 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -29,14 +29,14 @@ enum e_redirection
union u_redirection
{
struct s_fd
struct s_fd_redirection
{
int value;
} fd;
struct s_piped
struct s_piped_redirection
{
} piped;
struct s_inherited
struct s_inherited_redirection
{
} inherited;
};

View file

@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* _internal_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 17:36:38 by maiboyer #+# #+# */
/* Updated: 2024/07/07 18:01:17 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef _INTERNAL_PRINTF_H
#define _INTERNAL_PRINTF_H
#include "me/fs/fs.h"
#include "me/string/string.h"
#include "me/types.h"
typedef enum e_printf_flags t_printf_flags;
typedef enum e_printf_type t_printf_type;
typedef struct s_fprintf_arg t_fprintf_arg;
typedef struct s_printf_args t_printf_arg;
typedef struct s_printf_extra_args t_printf_extra_args;
typedef struct s_sprintf_arg t_sprintf_arg;
typedef void (*t_printf_func)(t_const_str to_write, t_usize to_write_len,
void *p_args);
struct s_fprintf_arg
{
t_usize total_print;
t_fd *fd;
};
struct s_sprintf_arg
{
t_usize total_print;
t_string *buffer;
};
enum e_printf_flags
{
PRECISION = 1 << 1,
ALIGN = 1 << 2,
ZERO_ALIGN = 1 << 3,
SIGN = 1 << 4,
};
enum e_printf_type
{
CHAR = 1 << 0,
STR = 1 << 1,
U64 = 1 << 2,
I64 = 1 << 3,
VOID_PTR = 1 << 4,
I32 = 1 << 5,
U32 = 1 << 6,
};
struct s_printf_extra_args
{
t_u64 precision;
t_u64 align;
bool left_align;
bool space_align;
bool pretty;
};
struct s_printf_args
{
void *argument;
void *p_args;
t_printf_extra_args extra;
t_printf_flags flags;
};
void me_printf_write(t_const_str to_write, t_usize to_write_len, void *p_args);
void me_printf_append_string(t_const_str to_write, t_usize to_write_len,
void *p_args);
#endif /* _INTERNAL_PRINTF_H */

View file

@ -6,14 +6,13 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:18:19 by maiboyer #+# #+# */
/* Updated: 2023/11/18 19:11:23 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:38:52 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FORMATTER_H
# define FORMATTER_H
# include "me/printf/printf.h"
# include "me/types.h"
# include "me/printf/_internal_printf.h"
void printf_x_low(t_printf_arg data, t_printf_func f);
void printf_x_up(t_printf_arg data, t_printf_func f);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 17:58:41 by maiboyer #+# #+# */
/* Updated: 2023/12/01 21:24:21 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:38:49 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,7 @@
# define UTILS_H
# include "me/printf/matchers/matchers.h"
# include "me/printf/printf.h"
# include "me/printf/_internal_printf.h"
# include "me/types.h"
# include <stdarg.h>

View file

@ -6,14 +6,14 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:09:07 by maiboyer #+# #+# */
/* Updated: 2023/11/18 18:10:33 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:38:46 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MATCHERS_H
# define MATCHERS_H
# include "me/printf/printf.h"
# include "me/printf/_internal_printf.h"
# include "me/types.h"
# include <stdarg.h>
# define PRINTF_BUFFER_CHUNK 20

View file

@ -6,100 +6,72 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:10:27 by maiboyer #+# #+# */
/* Updated: 2024/07/05 19:54:25 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:37:02 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PRINTF_H
# define PRINTF_H
# include "me/types.h"
# include <stdarg.h>
#define PRINTF_H
#include "me/types.h"
#include <stdarg.h>
# ifndef FS_H
#ifndef FS_H
typedef struct s_fd t_fd;
# endif
#endif
typedef struct s_fprintf_arg
{
t_usize total_print;
int fd;
} t_fprintf_arg;
typedef enum e_printf_flags
{
PRECISION = 1 << 1,
ALIGN = 1 << 2,
ZERO_ALIGN = 1 << 3,
SIGN = 1 << 4,
} t_printf_flags;
typedef enum e_printf_type
{
CHAR = 1 << 0,
STR = 1 << 1,
U64 = 1 << 2,
I64 = 1 << 3,
VOID_PTR = 1 << 4,
I32 = 1 << 5,
U32 = 1 << 6,
} t_printf_type;
typedef struct s_printf_extra_args
{
t_u64 precision;
t_u64 align;
bool left_align;
bool space_align;
bool pretty;
} t_printf_extra_args;
typedef struct s_printf_args
{
void *argument;
void *p_args;
t_printf_extra_args extra;
t_printf_flags flags;
} t_printf_arg;
typedef void (*t_printf_func)(t_const_str to_write,
t_usize to_write_len, void *p_args);
#ifndef STRING_H
typedef struct s_string t_string;
#endif
/// @brief Print a formatted string to stdout
/// @param fmt the format string
/// @param ... the arguments to format
/// @return the number of characters printed
t_usize me_printf(t_const_str fmt, ...);
t_usize me_printf(t_const_str fmt, ...);
/// @brief Print a formatted string to a stderr
/// @param fmt the format string
/// @param ... the arguments to format
/// @return the number of characters printed
t_usize me_eprintf(t_const_str fmt, ...);
t_usize me_eprintf(t_const_str fmt, ...);
/// @brief Print a formatted string to a stdout
/// @param fmt the format string
/// @param args the arguments to format as a va_list
/// @return the number of characters printed
t_usize me_vprintf(t_const_str fmt, va_list *args);
t_usize me_vprintf(t_const_str fmt, va_list *args);
/// @brief Print a formatted string to a stderr
/// @param fmt the format string
/// @param args the arguments to format as a va_list
/// @return the number of characters printed
t_usize me_veprintf(t_const_str fmt, va_list *args);
t_usize me_veprintf(t_const_str fmt, va_list *args);
/// @brief Print a formatted string to the given fd
/// @param fmt the format string
/// @param ... the arguments to format
/// @return the number of characters printed
t_usize me_printf_fd(t_fd *, t_const_str fmt, ...);
t_usize me_printf_fd(t_fd *, t_const_str fmt, ...);
/// @brief Print a formatted string to the given fd
/// @param fmt the format string
/// @param args the arguments to format as a va_list
/// @return the number of characters printed
t_usize me_vprintf_fd(t_fd *, t_const_str fmt, va_list *args);
t_usize me_vprintf_fd(t_fd *, t_const_str fmt, va_list *args);
/// @brief print a formatted string to a buffer
/// @param buffer the buffer to append to
/// @param fmt the format string
/// @param ... the arguments to format
/// @return the number of characters printed
t_usize me_printf_str(t_string *buffer, t_const_str fmt, ...);
/// @brief print a formatted string to a buffer
/// @param buffer the buffer to append to
/// @param fmt the format string
/// @param args the arguments to format
/// @return the number of characters printed
t_usize me_vprintf_str(t_string *buffer, t_const_str fmt, va_list *args);
#endif

View file

@ -73,6 +73,7 @@ os/pipe
os/process
os/process_inner
os/process_inner2
printf/callbacks
printf/formatter/char
printf/formatter/decimal
printf/formatter/hex
@ -85,6 +86,8 @@ printf/formatter/utils3
printf/formatter/utils_numbers
printf/matchers
printf/printf
printf/printf_fd
printf/printf_str
printf/vprintf
str/str_clone
str/str_compare

View file

@ -6,19 +6,20 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 15:53:50 by maiboyer #+# #+# */
/* Updated: 2024/05/30 16:02:12 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 19:17:05 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/types.h"
#include "me/fs/fs.h"
#include "me/mem/mem.h"
#include "me/str/str.h"
#include "me/types.h"
#include "unistd.h"
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>
t_fd_array *get_fd_arrays(void)
{
@ -45,7 +46,7 @@ struct s_file_slot *get_unused_fd_slot(void)
return (NULL);
}
void close_all_slots(void)
__attribute__((destructor(201))) void close_all_slots(void)
{
t_usize i;
t_fd_array *arr;
@ -63,30 +64,30 @@ void close_slot(struct s_file_slot *slot)
if (slot->ty == SLOT_UNUSED)
;
else if (slot->ty == SLOT_FD)
close_fd(&slot->slot.fd);
(mem_free(slot->slot.fd.name), close_fd(&slot->slot.fd));
else if (slot->ty == SLOT_DIR)
close_dir(&slot->slot.dir);
(mem_free(slot->slot.dir.name), close_dir(&slot->slot.dir));
else if (slot->ty == SLOT_FILE)
close_file(&slot->slot.file);
(mem_free(slot->slot.file.name), close_file(&slot->slot.file));
else
(void)!write(2, "Unknown SLOT type", 17);
mem_set_zero(slot, sizeof(*slot));
}
/* ______ _____
| ____| __ \
/* ______ _____
| ____| __ \
| |__ | | | |
| __| | | | |
| | | |__| |
|_| |_____/
|_| |_____/
*/
t_fd *open_fd(t_str name, t_fd_perm perms, t_file_open_option open_options,
t_file_perm file_perm)
t_fd *open_fd(t_str name, t_fd_perm perms, t_file_open_option open_options,
t_file_perm file_perm)
{
t_fd *fd;
struct s_file_slot *slot;
int actual_perms;
t_fd *fd;
struct s_file_slot *slot;
int actual_perms;
if (perms & FD_READ && perms & FD_WRITE)
actual_perms = O_RDWR;
@ -113,7 +114,8 @@ t_error read_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *read_count)
{
t_isize ret;
if (fd == NULL || buffer == NULL || read_count == NULL || fd->fd == -1 || !(fd->perms & FD_READ))
if (fd == NULL || buffer == NULL || read_count == NULL || fd->fd == -1 ||
!(fd->perms & FD_READ))
return (ERROR);
ret = read(fd->fd, buffer, size);
if (ret == -1)
@ -149,13 +151,13 @@ t_error stat_fd(t_fd *fd, t_stat *stat)
void close_fd(t_fd *fd)
{
struct s_file_slot *slot;
struct s_file_slot *slot;
if (fd == NULL)
return ;
return;
if (close(fd->fd) == -1)
return ;
slot = (void*)(fd) - offsetof(struct s_file_slot, slot.fd);
return;
slot = (void *)(fd)-offsetof(struct s_file_slot, slot.fd);
mem_set_zero(slot, sizeof(*slot));
}
@ -163,7 +165,7 @@ void close_fd(t_fd *fd)
void put_number_fd(t_fd *fd, t_u64 number)
{
t_u8 buffer[INLINE_BUFFER_SIZE];
t_u8 buffer[INLINE_BUFFER_SIZE];
t_usize i;
i = 0;
@ -178,7 +180,7 @@ void put_number_fd(t_fd *fd, t_u64 number)
void put_string_fd(t_fd *fd, t_const_str string)
{
write_fd(fd, (t_u8*)string, str_len(string), NULL);
write_fd(fd, (t_u8 *)string, str_len(string), NULL);
}
void put_char_fd(t_fd *fd, t_u8 c)
@ -188,16 +190,16 @@ void put_char_fd(t_fd *fd, t_u8 c)
/* _____ _____ _____ ______ _____ _______ ____ _______ __
| __ \_ _| __ \| ____/ ____|__ __/ __ \| __ \ \ / /
| | | || | | |__) | |__ | | | | | | | | |__) \ \_/ /
| | | || | | _ /| __|| | | | | | | | _ / \ /
| |__| || |_| | \ \| |___| |____ | | | |__| | | \ \ | |
|_____/_____|_| \_\______\_____| |_| \____/|_| \_\ |_|
| | | || | | |__) | |__ | | | | | | | | |__) \ \_/ /
| | | || | | _ /| __|| | | | | | | | _ / \ /
| |__| || |_| | \ \| |___| |____ | | | |__| | | \ \ | |
|_____/_____|_| \_\______\_____| |_| \____/|_| \_\ |_|
*/
t_error open_dir(t_str name, t_dir **dir)
{
t_dir *out;
struct s_file_slot *slot;
t_dir *out;
struct s_file_slot *slot;
slot = get_unused_fd_slot();
out = &slot->slot.dir;
@ -226,28 +228,28 @@ t_error read_dir(t_dir *dir, t_dir_entry *out)
void close_dir(t_dir *dir)
{
struct s_file_slot *slot;
struct s_file_slot *slot;
if (dir == NULL)
return ;
return;
if (closedir(dir->ptr) == -1)
return ;
slot = (void*)(dir) - offsetof(struct s_file_slot, slot.dir);
return;
slot = (void *)(dir)-offsetof(struct s_file_slot, slot.dir);
mem_set_zero(slot, sizeof(*slot));
}
/*______ _____ _ ______
/*______ _____ _ ______
| ____|_ _| | | ____|
| |__ | | | | | |__
| __| | | | | | __|
| | _| |_| |____| |____
| |__ | | | | | |__
| __| | | | | | __|
| | _| |_| |____| |____
|_| |_____|______|______|
*/
t_error open_file(t_str name, t_mode mode, t_file **file)
{
t_file *out;
struct s_file_slot *slot;
t_file *out;
struct s_file_slot *slot;
slot = get_unused_fd_slot();
out = &slot->slot.file;
@ -260,7 +262,8 @@ t_error open_file(t_str name, t_mode mode, t_file **file)
return (NO_ERROR);
}
t_error write_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *write_count)
t_error write_file(t_file *file, t_u8 *buffer, t_usize size,
t_isize *write_count)
{
t_isize ret;
t_isize fake_ret;
@ -280,7 +283,8 @@ t_error read_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *read_count)
{
t_isize ret;
if (file == NULL || buffer == NULL || read_count == NULL || file->ptr == NULL)
if (file == NULL || buffer == NULL || read_count == NULL ||
file->ptr == NULL)
return (ERROR);
ret = fread(buffer, size, 1, file->ptr);
if (ret == -1)
@ -291,12 +295,77 @@ t_error read_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *read_count)
void close_file(t_file *file)
{
struct s_file_slot *slot;
struct s_file_slot *slot;
if (file == NULL)
return ;
return;
if (fclose(file->ptr) == -1)
return ;
slot = (void*)(file) - offsetof(struct s_file_slot, slot.file);
return;
slot = (void *)(file)-offsetof(struct s_file_slot, slot.file);
mem_set_zero(slot, sizeof(*slot));
}
/* _____ ______ _______ _______ ______ _____ _____
/ ____| ____|__ __|__ __| ____| __ \ / ____|
| | __| |__ | | | | | |__ | |__) | (___
| | |_ | __| | | | | | __| | _ / \___ \
| |__| | |____ | | | | | |____| | \ \ ____) |
\_____|______| |_| |_| |______|_| \_\_____/
*/
t_fd *get_stdin(void)
{
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
if (value == NULL)
{
slot = get_unused_fd_slot();
out = &slot->slot.fd;
out->fd = STDIN_FILENO;
out->perms = FD_READ;
out->name = str_clone("<stdin>");
slot->ty = SLOT_FD;
value = out;
}
return (value);
}
t_fd *get_stdout(void)
{
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
if (value == NULL)
{
slot = get_unused_fd_slot();
out = &slot->slot.fd;
out->fd = STDOUT_FILENO;
out->perms = FD_WRITE;
out->name = str_clone("<stdout>");
slot->ty = SLOT_FD;
value = out;
}
return (value);
}
t_fd *get_stderr(void)
{
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
if (value == NULL)
{
slot = get_unused_fd_slot();
out = &slot->slot.fd;
out->fd = STDERR_FILENO;
out->perms = FD_WRITE;
out->name = str_clone("<stderr>");
slot->ty = SLOT_FD;
value = out;
}
return (value);
}

View file

@ -6,17 +6,16 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 18:26:27 by maiboyer #+# #+# */
/* Updated: 2024/05/22 15:21:28 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 19:10:07 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "aq/internal_vg_funcs.h"
#include "me/types.h"
#include "aq/allocator.h"
#include "aq/internal_vg_funcs.h"
#include "aq/libc_wrapper.h"
#include "aq/melloc.h"
#include "me/types.h"
#include <stdio.h>
t_allocator *global_allocator(void)
{
@ -31,6 +30,7 @@ t_allocator *global_allocator(void)
return (&global_alloc);
}
__attribute__((destructor(200)))
void uninit_global_allocator(void)
{
t_allocator *allocator;

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 13:08:52 by maiboyer #+# #+# */
/* Updated: 2024/05/22 15:05:19 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 19:11:39 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,9 +15,12 @@
#include "me/types.h"
#include <stdlib.h>
// If you are looking at why some stuff aren't closed, they are using the
// __attribute__((dtor)) to be run at exit for example:
// - close_all_slots
// - uninit global allocator
void me_exit(t_i32 exit_code)
{
close_all_slots();
uninit_global_allocator();
(get_stdin(), get_stdout(), get_stderr());
exit(exit_code);
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* callbacks.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 18:01:52 by maiboyer #+# #+# */
/* Updated: 2024/07/07 18:03:28 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/fs/fs.h"
#include "me/printf/_internal_printf.h"
#include "me/string/string.h"
#include "me/types.h"
void me_printf_append_string(t_const_str to_write, t_usize to_write_len,
void *p_args)
{
t_sprintf_arg *arg;
arg = p_args;
arg->total_print += to_write_len;
string_push(arg->buffer, to_write);
}
void me_printf_write(t_const_str to_write, t_usize to_write_len, void *p_args)
{
t_fprintf_arg *arg;
arg = (t_fprintf_arg *)p_args;
write_fd(arg->fd, (t_u8 *)to_write, to_write_len, NULL);
arg->total_print += to_write_len;
}

View file

@ -6,13 +6,12 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 18:12:11 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:43:13 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:39:40 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/str/str.h"
#include "me/str/str.h"
#include <stdlib.h>

View file

@ -6,14 +6,13 @@
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 01:44:35 by maix #+# #+# */
/* Updated: 2024/05/14 18:43:24 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:39:44 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/str/str.h"
#include "me/str/str.h"
#include <stdio.h>

View file

@ -6,13 +6,12 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:16:16 by maiboyer #+# #+# */
/* Updated: 2023/12/11 19:19:03 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:40:18 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/str/str.h"
#define HEX_INLINE_BUF 17

View file

@ -6,13 +6,12 @@
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 01:19:18 by maix #+# #+# */
/* Updated: 2023/12/11 19:17:23 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:40:18 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/str/str.h"
#include <stdio.h>
#define OCT_INLINE_BUF 23

View file

@ -6,13 +6,12 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:16:16 by maiboyer #+# #+# */
/* Updated: 2023/12/11 19:20:42 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:40:18 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/str/str.h"
#define PTR_INLINE_BUF 17

View file

@ -6,14 +6,13 @@
/* By: maix <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 01:44:35 by maix #+# #+# */
/* Updated: 2024/05/14 18:43:39 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:40:18 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/mem/mem.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/str/str.h"
#include "me/str/str.h"
#include <stdio.h>

View file

@ -6,16 +6,14 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 17:57:04 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:43:44 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:40:18 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/string/string.h"
#include "me/mem/mem.h"
#include "me/convert/atoi.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/matchers/matchers.h"
#include "me/printf/printf.h"
#include "me/str/str.h"
#include "me/str/str.h"
#include "me/types.h"

View file

@ -6,14 +6,12 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:00:07 by maiboyer #+# #+# */
/* Updated: 2023/12/01 21:48:22 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:40:18 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/string/string.h"
#include "me/char/char.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/types.h"
void set_var_for_pad_and_stuff(t_pad_and_stuff_args *a, t_printf_arg *d)

View file

@ -6,16 +6,15 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 21:05:47 by maiboyer #+# #+# */
/* Updated: 2024/05/14 18:43:56 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:40:12 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/mem/mem.h"
#include "me/printf/formatter/utils.h"
void handle_weird_precision_stuff(t_printf_arg *data, t_prec_strs strs,
t_usize value)
void handle_weird_precision_stuff(t_printf_arg *data, t_prec_strs strs,
t_usize value)
{
if (!value && data->extra.precision == 0 && (data->flags & PRECISION))
{

View file

@ -6,14 +6,13 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:07:40 by maiboyer #+# #+# */
/* Updated: 2023/12/11 19:11:51 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 17:39:37 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/mem/mem.h"
#include "me/printf/formatter/formatter.h"
#include "me/printf/matchers/matchers.h"
#include "me/printf/printf.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

View file

@ -6,85 +6,37 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/11 17:50:56 by maiboyer #+# #+# */
/* Updated: 2024/02/09 14:58:10 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 18:01:58 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/string/string.h"
#include "me/fs/write.h"
#include "me/printf/formatter/formatter.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/matchers/matchers.h"
#include "me/printf/printf.h"
#include "me/str/str.h"
#include "me/fs/fs.h"
#include "me/fs/write.h"
#include "me/printf/_internal_printf.h"
#include "me/types.h"
#include <limits.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
// p_args is an t_string;
static void me_printf_add_to_string(t_const_str to_write, t_usize to_write_len,
void *p_args)
t_usize me_printf(t_const_str fmt, ...)
{
t_string *out_buf;
va_list args;
t_usize res;
out_buf = (t_string *)p_args;
(void)(to_write_len);
string_push(out_buf, to_write);
}
t_str me_printf_str(t_const_str fmt, va_list *arguments)
{
t_string out;
out = string_new(str_len(fmt));
if (out.buf == NULL)
{
return (NULL);
}
me_printf_str_inner(fmt, &me_printf_add_to_string, arguments, (void *)&out);
return (out.buf);
}
void me_printf_write(t_const_str to_write, t_usize to_write_len,
void *p_args)
{
t_fprintf_arg *arg;
arg = (t_fprintf_arg *)p_args;
me_write(arg->fd, (t_u8 *)to_write, to_write_len);
arg->total_print += to_write_len;
}
t_usize me_printf(t_const_str fmt, ...)
{
va_list args;
t_fprintf_arg passthru;
passthru = (t_fprintf_arg){
.fd = 1,
.total_print = 0,
};
va_start(args, fmt);
me_printf_str_inner(fmt, &me_printf_write, &args, (void *)&passthru);
res = me_vprintf(fmt, &args);
va_end(args);
return (passthru.total_print);
return (res);
}
t_usize me_eprintf(t_const_str fmt, ...)
t_usize me_eprintf(t_const_str fmt, ...)
{
va_list args;
t_fprintf_arg passthru;
va_list args;
t_usize res;
passthru = (t_fprintf_arg){
.fd = 2,
.total_print = 0,
};
va_start(args, fmt);
me_printf_str_inner(fmt, &me_printf_write, &args, (void *)&passthru);
res = me_veprintf(fmt, &args);
va_end(args);
return (passthru.total_print);
return (res);
}
/*

View file

@ -17,31 +17,25 @@
#include "me/types.h"
#include <stdarg.h>
void me_printf_write(t_const_str to_write, t_usize to_write_len, void *p_args);
t_usize me_vprintf_fd(t_fd *fd, t_const_str fmt, va_list *args)
{
t_fprintf_arg passthru;
passthru = (t_fprintf_arg){
.fd = fd->fd,
.total_print = 0,
};
if (fd == NULL || fmt == NULL || args == NULL)
return (0);
passthru.fd = fd;
passthru.total_print = 0;
me_printf_str_inner(fmt, &me_printf_write, args, (void *)&passthru);
return (passthru.total_print);
}
t_usize me_printf_fd(t_fd *fd, t_const_str fmt, ...)
{
va_list args;
t_fprintf_arg passthru;
va_list args;
t_usize res;
passthru = (t_fprintf_arg){
.fd = fd->fd,
.total_print = 0,
};
va_start(args, fmt);
me_printf_str_inner(fmt, &me_printf_write, &args, (void *)&passthru);
res = me_vprintf_fd(fd, fmt, &args);
va_end(args);
return (passthru.total_print);
return (res);
}

View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* printf_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 17:27:50 by maiboyer #+# #+# */
/* Updated: 2024/07/07 18:03:40 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/printf/_internal_printf.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/printf.h"
#include "me/string/string.h"
#include "me/types.h"
#include <stdarg.h>
t_usize me_vprintf_str(t_string *buf, t_const_str fmt, va_list *args)
{
t_sprintf_arg passthru;
if (buf == NULL || fmt == NULL || args == NULL)
return (0);
passthru.buffer = buf;
passthru.total_print = 0;
me_printf_str_inner(fmt, &me_printf_append_string, args, &passthru);
return (passthru.total_print);
}
t_usize me_printf_str(t_string *buf, t_const_str fmt, ...)
{
t_usize res;
va_list args;
va_start(args, fmt);
res = me_vprintf_str(buf, fmt, &args);
va_end(args);
return (res);
}

View file

@ -6,46 +6,38 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 14:57:28 by maiboyer #+# #+# */
/* Updated: 2024/02/09 15:00:39 by maiboyer ### ########.fr */
/* Updated: 2024/07/07 18:00:14 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/string/string.h"
#include "me/fs/write.h"
#include "me/printf/formatter/formatter.h"
#include "me/fs/fs.h"
#include "me/printf/formatter/utils.h"
#include "me/printf/matchers/matchers.h"
#include "me/printf/printf.h"
#include "me/str/str.h"
#include "me/types.h"
#include <limits.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
void me_printf_write(t_const_str to_write, t_usize to_write_len,
void *p_args);
void me_printf_write(t_const_str to_write, t_usize to_write_len, void *p_args);
t_usize me_vprintf(t_const_str fmt, va_list *args)
t_usize me_vprintf(t_const_str fmt, va_list *args)
{
t_fprintf_arg passthru;
t_fprintf_arg passthru;
passthru = (t_fprintf_arg){
.fd = 1,
.total_print = 0,
};
if (fmt == NULL || args == NULL)
return (0);
passthru.fd = get_stdout();
passthru.total_print = 0;
me_printf_str_inner(fmt, &me_printf_write, args, (void *)&passthru);
return (passthru.total_print);
}
t_usize me_veprintf(t_const_str fmt, va_list *args)
t_usize me_veprintf(t_const_str fmt, va_list *args)
{
t_fprintf_arg passthru;
t_fprintf_arg passthru;
passthru = (t_fprintf_arg){
.fd = 2,
.total_print = 0,
};
if (fmt == NULL || args == NULL)
return (0);
passthru.fd = get_stderr();
passthru.total_print = 0;
me_printf_str_inner(fmt, &me_printf_write, args, (void *)&passthru);
return (passthru.total_print);
}