stdme os revamped

This commit is contained in:
Maieul BOYER 2024-08-01 07:26:32 +02:00
parent f29e4ad7ef
commit 2c9a3ee834
No known key found for this signature in database
11 changed files with 285 additions and 274 deletions

View file

@ -30,6 +30,7 @@ convert/numbers_to_str \
fs/directory \
fs/fd \
fs/file \
fs/file_dup \
fs/fs_internal \
fs/getters \
fs/putfd \

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 15:12:18 by maiboyer #+# #+# */
/* Updated: 2024/07/11 18:56:59 by maiboyer ### ########.fr */
/* Updated: 2024/08/01 07:25:10 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -254,6 +254,9 @@ void put_string_fd(t_fd *fd, t_const_str string);
/// @note will fail silently if the fd is not open in write mode
void put_char_fd(t_fd *fd, t_u8 c);
/// @brief Duplicate a t_fd using the dup syscall
/// @note will return NULL in case of error (either fd is null or dup failed)
t_fd *dup_fd(t_fd *fd);
/* _____ _____ _____ ______ _____ _______ ____ _______ __
| __ \_ _| __ \| ____/ ____|__ __/ __ \| __ \ \ / /
| | | || | | |__) | |__ | | | | | | | | |__) \ \_/ /

137
stdme/include/me/os/os.h Normal file
View file

@ -0,0 +1,137 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* os.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 15:43:08 by maiboyer #+# #+# */
/* Updated: 2024/08/01 06:32:58 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef OS_H
# define OS_H
# include "me/fs/fs.h"
# include "me/types.h"
# include "me/vec/vec_str.h"
# include "me/vec/vec_u8.h"
/// @brief A process ID
typedef int t_pid;
/// @brief A process exit_code
typedef int t_exit_code;
/// @brief Redirection for spawning a process
typedef union u_redirection t_redirection;
/// @brief Pipe structure
typedef struct s_pipe t_pipe;
/// @brief Spawn information
typedef struct s_spawn_info t_spawn_info;
/// @brief Process information
typedef struct s_process t_process;
/// @struct Process output
/// @brief Process output information
typedef struct s_process_output t_process_output;
enum e_redirection
{
R_INHERITED = 0,
R_PIPED = 1,
R_FD = 2,
};
union u_redirection
{
enum e_redirection tag;
struct s_fd_redirection
{
enum e_redirection tag;
t_fd *fd;
} fd;
struct s_piped_redirection
{
enum e_redirection tag;
} piped;
struct s_inherited_redirection
{
enum e_redirection tag;
} inherited;
};
struct s_spawn_info
{
t_redirection stdin;
t_redirection stdout;
t_redirection stderr;
t_vec_str arguments;
t_vec_str environement;
t_str binary_path;
void (*forked_free)(void *);
void *forked_free_args;
};
struct s_process
{
t_fd *stdin;
t_fd *stdout;
t_fd *stderr;
t_pid pid;
};
struct s_process_output
{
t_pid pid;
t_vec_u8 stdout;
t_vec_u8 stderr;
t_exit_code exit_code;
};
struct s_pipe
{
t_fd *read;
t_fd *write;
};
/// @brief Spawn a new process with the given information
/// @param info the information to spawn the process
/// @param process data returned by the function
/// @return true if an error occured, false otherwise
t_error spawn_process(t_spawn_info info,
t_process *process);
/// @brief Create a pipe
/// @param[out] out the created pipe
/// @return the error
t_error create_pipe(t_pipe *out);
/// @brief Create a piped redirection
/// @param void
/// @return the redirection
static inline t_redirection piped(void)
{
return ((t_redirection){.tag = R_PIPED});
}
/// @brief Create an inherited redirection (inherit from the parent process)
/// @param void
/// @return the redirection
static inline t_redirection inherited(void)
{
return ((t_redirection){.tag = R_INHERITED});
}
/// @brief Create a file descriptor redirection
/// @param fd file descriptor to redirect
/// @return the redirection
static inline t_redirection fd(t_fd *fd)
{
return ((t_redirection){.fd = {.tag = R_FD, fd}});
}
#endif /* PROCESS_H */

View file

@ -1,173 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 15:43:08 by maiboyer #+# #+# */
/* Updated: 2024/07/11 18:59:04 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PROCESS_H
# define PROCESS_H
# include "me/types.h"
# include "me/vec/vec_str.h"
# include "me/vec/vec_u8.h"
typedef t_i32 t_pid;
typedef t_i32 t_exit_code;
enum e_redirection
{
R_INHERITED = 0,
R_PIPED = 1,
R_FD = 2,
};
union u_redirection
{
struct s_fd_redirection
{
int value;
} fd;
struct s_piped_redirection
{
} piped;
struct s_inherited_redirection
{
} inherited;
};
/// @brief Redirection for spawning a process
typedef struct s_redirection
{
enum e_redirection tag;
union u_redirection vals;
} t_redirection;
/// @brief Create a piped redirection
/// @param void
/// @return the redirection
static inline t_redirection piped(void)
{
return ((t_redirection){
.tag = R_PIPED,
});
}
/// @brief Create an inherited redirection (inherit from the parent process)
/// @param void
/// @return the redirection
static inline t_redirection inherited(void)
{
return ((t_redirection){
.tag = R_INHERITED,
});
}
/// @brief Create a file descriptor redirection
/// @param fd file descriptor to redirect
/// @return the redirection
static inline t_redirection fd(int fd)
{
return ((t_redirection){.tag = R_FD, \
.vals = (union u_redirection){\
.fd = {.value = fd}, \
}});
}
/// @brief Wrapped file descriptor tag
enum e_wrapped_fd_tag
{
READ_ONLY,
WRITE_ONLY,
READ_WRITE,
INVALID,
};
/// @brief Wrapped file descriptor
union u_wrapped_fd
{
struct s_read_only
{
int fd;
} ro;
struct s_write_only
{
int fd;
} wo;
struct s_read_write
{
int fd;
} rw;
};
/// @brief Wrapped file descriptor
typedef struct s_wrapped_fd
{
enum e_wrapped_fd_tag tag;
union u_wrapped_fd vals;
} t_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(int fd)
{
return ((t_wrapped_fd){.tag = READ_ONLY, \
.vals = (union u_wrapped_fd){\
.ro = {.fd = 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(int fd)
{
return ((t_wrapped_fd){.tag = WRITE_ONLY,
.vals = (union u_wrapped_fd){.wo = {.fd = fd}}});
}
/// @brief Spawn information
typedef struct s_spawn_info
{
t_redirection stdin;
t_redirection stdout;
t_redirection stderr;
t_vec_str arguments;
t_vec_str environement;
t_str binary_path;
void (*forked_free)(void *);
void *forked_free_args;
} t_spawn_info;
/// @brief Process information
typedef struct s_process
{
t_wrapped_fd stdin;
t_wrapped_fd stdout;
t_wrapped_fd stderr;
t_pid pid;
} t_process;
/// @struct Process output
/// @brief Process output information
typedef struct s_process_output
{
t_pid pid;
t_vec_u8 stdout;
t_vec_u8 stderr;
t_exit_code exit_code;
} t_process_output;
/// @brief Spawn a new process with the given information
/// @param info the information to spawn the process
/// @param process data returned by the function
/// @return true if an error occured, false otherwise
t_error spawn_process(t_spawn_info info,
t_process *process);
#endif /* PROCESS_H */

View file

@ -1,30 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipe.h :+: :+: :+: */
/* file_dup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 17:57:29 by maiboyer #+# #+# */
/* Updated: 2024/01/04 17:59:30 by maiboyer ### ########.fr */
/* Created: 2024/08/01 06:59:44 by maiboyer #+# #+# */
/* Updated: 2024/08/01 07:03:32 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PIPE_H
# define PIPE_H
#include "me/fs/fs.h"
#include "me/str/str.h"
#include "me/types.h"
#include <unistd.h>
# include "me/types.h"
/// @brief Pipe structure
typedef struct s_pipe
t_fd *dup_fd(t_fd *fd)
{
int read;
int write;
} t_pipe;
struct s_file_slot *slot;
int tmp;
/// @brief Create a pipe
/// @param[out] out the created pipe
/// @return the error
t_error create_pipe(t_pipe *out);
#endif /* PIPE_H */
if (fd == NULL)
return (NULL);
slot = get_unused_fd_slot();
if (slot == NULL)
return (NULL);
tmp = dup(fd->fd);
if (tmp == -1)
return (NULL);
slot->ty = SLOT_FD;
slot->slot.fd.fd = tmp;
slot->slot.fd.perms = fd->perms;
slot->slot.fd.type = fd->type;
slot->slot.fd.name = str_clone(fd->name);
return (&slot->slot.fd);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 11:08:03 by maiboyer #+# #+# */
/* Updated: 2024/07/30 16:34:34 by rparodi ### ########.fr */
/* Updated: 2024/08/01 06:38:36 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,8 +23,8 @@
# define BASE_PATH "/no_base_path_defined/"
#endif
// #if defined(PRINT_BACKTRACE) || defined(BACKTRACE_DEEP) || true
#if true // TO_REMOVE
#if defined(PRINT_BACKTRACE) || defined(BACKTRACE_DEEP)
// #if true // TO_REMOVE
# ifndef BACKTRACE_DEEP
# define BACKTRACE_DEEP 256
# endif

View file

@ -6,19 +6,51 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 17:59:48 by maiboyer #+# #+# */
/* Updated: 2024/01/04 18:01:42 by maiboyer ### ########.fr */
/* Updated: 2024/08/01 06:56:14 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/os/pipe.h"
#include "me/fs/fs.h"
#include "me/os/os.h"
#include "me/str/str.h"
static t_error _pipe_get_fd_slot(struct s_file_slot **read,
struct s_file_slot **write)
{
(*read) = get_unused_fd_slot();
if ((*read) == NULL)
return (ERROR);
(*read)->ty = SLOT_FD;
(*write) = get_unused_fd_slot();
if ((*write) == NULL)
return ((*read)->ty = SLOT_UNUSED, ERROR);
(*write)->ty = SLOT_FD;
return (NO_ERROR);
}
t_error create_pipe(t_pipe *out)
{
int fds[2];
int fd[2];
struct s_file_slot *read;
struct s_file_slot *write;
if (pipe(fds))
if (_pipe_get_fd_slot(&read, &write))
return (ERROR);
out->read = fds[0];
out->write = fds[1];
if (pipe(fd))
{
read->ty = SLOT_UNUSED;
write->ty = SLOT_UNUSED;
return (ERROR);
}
read->slot.fd.fd = fd[0];
write->slot.fd.fd = fd[0];
read->slot.fd.type = FD_PIPE;
write->slot.fd.type = FD_PIPE;
read->slot.fd.perms = FD_READ;
write->slot.fd.perms = FD_WRITE;
read->slot.fd.name = str_clone("<pipe[read]>");
write->slot.fd.name = str_clone("<pipe[write]>");
out->read = &read->slot.fd;
out->write = &write->slot.fd;
return (NO_ERROR);
}

View file

@ -6,18 +6,15 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
/* Updated: 2024/07/30 16:08:20 by rparodi ### ########.fr */
/* Updated: 2024/08/01 07:14:29 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/os/process.h"
#include "me/string/string.h"
#include "me/mem/mem.h"
#include "me/os/os.h"
#include "me/printf/printf.h"
#include "me/os/pipe.h"
#include "me/str/str.h"
#include "me/str/str.h"
#include "me/str/str.h"
#include "me/string/string.h"
#include "me/types.h"
#include "me/vec/vec_str.h"
#include <stdio.h>
@ -35,15 +32,15 @@ t_error spawn_process_exec(t_spawn_info info, t_process *process)
if (info.forked_free)
info.forked_free(info.forked_free_args);
dup2(info.stdin.vals.fd.value, 0);
dup2(info.stdout.vals.fd.value, 1);
dup2(info.stderr.vals.fd.value, 2);
close(process->stdin.vals.ro.fd);
close(process->stdout.vals.ro.fd);
close(process->stderr.vals.ro.fd);
close(info.stdin.vals.fd.value);
close(info.stdout.vals.fd.value);
close(info.stderr.vals.fd.value);
dup2(info.stdin.fd.fd->fd, 0);
dup2(info.stdout.fd.fd->fd, 1);
dup2(info.stderr.fd.fd->fd, 2);
close_fd(process->stdin);
close_fd(process->stdout);
close_fd(process->stderr);
close_fd(info.stdin.fd.fd);
close_fd(info.stdout.fd.fd);
close_fd(info.stderr.fd.fd);
if (!vec_str_any(&info.arguments, find_null, &res) && res)
vec_str_push(&info.arguments, NULL);
res = false;
@ -82,8 +79,8 @@ t_error find_binary(t_spawn_info *info, t_process *process)
if (info->binary_path == NULL)
return (ERROR);
s = string_new(256);
if (str_start_with(info->binary_path, "/") || \
str_find_chr(info->binary_path, '/') != NULL)
if (str_start_with(info->binary_path, "/")
|| str_find_chr(info->binary_path, '/') != NULL)
string_push(&s, info->binary_path);
else
{
@ -101,18 +98,17 @@ t_error find_binary(t_spawn_info *info, t_process *process)
return (string_free(s), ERROR);
}
static void cleanup(t_spawn_info info, t_process *process, \
bool cleanup_process)
static void cleanup(t_spawn_info info, t_process *process, bool cleanup_process)
{
if (cleanup_process && process->stdin.tag != INVALID)
close(process->stdin.vals.ro.fd);
if (cleanup_process && process->stdout.tag != INVALID)
close(process->stdout.vals.ro.fd);
if (cleanup_process && process->stderr.tag != INVALID)
close(process->stderr.vals.ro.fd);
close(info.stdin.vals.fd.value);
close(info.stdout.vals.fd.value);
close(info.stderr.vals.fd.value);
if (cleanup_process && process->stdin)
close_fd(process->stdin);
if (cleanup_process && process->stdout)
close_fd(process->stdout);
if (cleanup_process && process->stderr)
close_fd(process->stderr);
close_fd(info.stdin.fd.fd);
close_fd(info.stdout.fd.fd);
close_fd(info.stderr.fd.fd);
vec_str_free(info.arguments);
vec_str_free(info.environement);
mem_free(info.binary_path);
@ -131,7 +127,7 @@ t_error spawn_process(t_spawn_info info, t_process *process)
{
cleanup(info, process, false);
if (process->pid == -1)
return (printf("pid\n"), ERROR);
return (ERROR);
}
return (NO_ERROR);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 22:25:44 by maiboyer #+# #+# */
/* Updated: 2024/07/10 18:05:03 by maiboyer ### ########.fr */
/* Updated: 2024/08/01 06:37:51 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,16 +19,6 @@ bool find_null(const t_str *s)
return (s == NULL);
}
bool str_start_with(t_const_str s, t_const_str prefix)
{
while (*prefix && *s)
{
if (*prefix++ != *s++)
return (false);
}
return (*prefix == '\0');
}
bool find_path(const t_str *s)
{
t_str ss;

View file

@ -6,31 +6,31 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 22:27:00 by maiboyer #+# #+# */
/* Updated: 2024/01/04 23:01:03 by maiboyer ### ########.fr */
/* Updated: 2024/08/01 07:14:10 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/fs/fs.h"
#include "me/os/os.h"
#include "me/types.h"
#include "me/os/process.h"
#include "me/os/pipe.h"
void handle_redirections_second(t_spawn_info *info, t_process *process)
void handle_redirections_inherited(t_spawn_info *info, t_process *process)
{
(void)(process);
if (info->stdin.tag == R_INHERITED)
{
info->stdin = fd(dup(0));
process->stdin = ro(dup(0));
info->stdin = fd(dup_fd(get_stdin()));
process->stdin = dup_fd(get_stdin());
}
if (info->stdout.tag == R_INHERITED)
{
info->stdout = fd(dup(1));
process->stdout = wo(dup(1));
info->stdout = fd(dup_fd(get_stdout()));
process->stdout = dup_fd(get_stdout());
}
if (info->stderr.tag == R_INHERITED)
{
info->stderr = fd(dup(2));
process->stderr = wo(dup(2));
info->stderr = fd(dup_fd(get_stderr()));
process->stderr = dup_fd(get_stderr());
}
}
@ -38,28 +38,28 @@ void handle_redirections_fds(t_spawn_info *info, t_process *process)
{
if (info->stdin.tag == R_FD)
{
info->stdin = fd(dup(info->stdin.vals.fd.value));
process->stdin = wo(dup(info->stdin.vals.fd.value));
info->stdin = fd(dup_fd(info->stdin.fd.fd));
process->stdin = dup_fd(info->stdin.fd.fd);
}
if (info->stdout.tag == R_FD)
{
info->stdout = fd(dup(info->stdout.vals.fd.value));
process->stdout = ro(dup(info->stdout.vals.fd.value));
info->stdout = fd(dup_fd(info->stdout.fd.fd));
process->stdout = dup_fd(info->stdout.fd.fd);
}
if (info->stderr.tag == R_FD)
{
info->stderr = fd(dup(info->stderr.vals.fd.value));
process->stderr = ro(dup(info->stderr.vals.fd.value));
info->stderr = fd(dup_fd(info->stderr.fd.fd));
process->stderr = dup_fd(info->stderr.fd.fd);
}
}
static inline void redirection_inner(t_spawn_info *info, t_process *process)
{
process->stderr.tag = INVALID;
process->stdout.tag = INVALID;
process->stdin.tag = INVALID;
process->stderr = NULL;
process->stdout = NULL;
process->stdin = NULL;
handle_redirections_fds(info, process);
handle_redirections_second(info, process);
handle_redirections_inherited(info, process);
}
t_error handle_redirections(t_spawn_info *info, t_process *process)
@ -71,21 +71,21 @@ t_error handle_redirections(t_spawn_info *info, t_process *process)
{
if (create_pipe(&pipe_fd))
return (ERROR);
process->stdin = wo(pipe_fd.write);
process->stdin = pipe_fd.write;
info->stdin = fd(pipe_fd.read);
}
if (info->stdout.tag == R_PIPED)
{
if (create_pipe(&pipe_fd))
return (ERROR);
process->stdout = ro(pipe_fd.read);
process->stdout = pipe_fd.read;
info->stdout = fd(pipe_fd.write);
}
if (info->stderr.tag == R_PIPED)
{
if (create_pipe(&pipe_fd))
return (ERROR);
process->stderr = ro(pipe_fd.read);
process->stderr = pipe_fd.read;
info->stderr = fd(pipe_fd.write);
}
return (NO_ERROR);

28
stdme/vendor/mlx.h vendored
View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mlx.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ol <ol@epitech.net> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2000/08/01 16:37:50 by ol #+# #+# */
/* Updated: 2024/08/01 06:44:00 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
/*
** mlx.h for MinilibX in
**
@ -44,7 +56,9 @@ void *mlx_new_window(void *mlx_ptr, int size_x, int size_y, char *title);
** return void *0 if failed
*/
int mlx_clear_window(void *mlx_ptr, void *win_ptr);
int mlx_pixel_put(void *mlx_ptr, void *win_ptr, int x, int y, int color);
//int mlx_pixel_put(void *mlx_ptr, void *win_ptr, int x, int y, int color);
int mlx_pixel_put(void *mlx_ptr, void *win_ptr, ...);
/*
** origin for x & y is top left corner of the window
** y down is positive
@ -67,8 +81,10 @@ char *mlx_get_data_addr(void *img_ptr, int *bits_per_pixel, int *size_line,
** endian : 0 = sever X is little endian, 1 = big endian
** for mlx_new_image2, 2nd arg of mlx_get_data_addr is number_of_planes
*/
int mlx_put_image_to_window(void *mlx_ptr, void *win_ptr, void *img_ptr,
int x, int y);
//int mlx_put_image_to_window(void *mlx_ptr, void *win_ptr, void *img_ptr,
// int x, int y);
int mlx_put_image_to_window(void *mlx_ptr, void *win_ptr, void *img_ptr, \
...);
int mlx_get_color_value(void *mlx_ptr, int color);
/*
@ -97,8 +113,10 @@ int mlx_loop_end(void *mlx_ptr);
** Usually asked...
*/
int mlx_string_put(void *mlx_ptr, void *win_ptr, int x, int y, int color,
char *string);
//int mlx_string_put(void *mlx_ptr, void *win_ptr, int x, int y, int color,
// char *string);
int mlx_string_put(void *mlx_ptr, void *win_ptr, ...);
void mlx_set_font(void *mlx_ptr, void *win_ptr, char *name);
void *mlx_xpm_to_image(void *mlx_ptr, char **xpm_data, int *width,
int *height);