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

@ -6,28 +6,25 @@
/* 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>
#include <stdlib.h>
#include <unistd.h>
bool find_path(const t_str *s);
bool find_null(const t_str *s);
bool str_start_with(t_const_str s, t_const_str prefix);
t_error handle_redirections(t_spawn_info *info, t_process *process);
bool find_path(const t_str *s);
bool find_null(const t_str *s);
bool str_start_with(t_const_str s, t_const_str prefix);
t_error handle_redirections(t_spawn_info *info, t_process *process);
t_error spawn_process_exec(t_spawn_info info, t_process *process)
{
@ -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;
@ -54,7 +51,7 @@ t_error spawn_process_exec(t_spawn_info info, t_process *process)
}
t_error in_path(t_spawn_info *info, t_process *process, t_const_str path_raw,
t_string *s)
t_string *s)
{
t_vec_str path;
t_usize idx;
@ -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);
}