Moved files a bit to reflect their use

This commit is contained in:
Maieul BOYER 2024-08-02 19:27:52 +02:00
parent 7ac90bac55
commit 72b90debcc
No known key found for this signature in database
18 changed files with 149 additions and 124 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
/* Updated: 2024/08/01 07:14:29 by maiboyer ### ########.fr */
/* Updated: 2024/08/02 19:11:00 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,9 +21,8 @@
#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);
bool _find_path(const t_str *s);
bool _find_null(const t_str *s);
t_error handle_redirections(t_spawn_info *info, t_process *process);
t_error spawn_process_exec(t_spawn_info info, t_process *process)
@ -41,10 +40,10 @@ t_error spawn_process_exec(t_spawn_info info, t_process *process)
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)
if (!vec_str_any(&info.arguments, _find_null, &res) && res)
vec_str_push(&info.arguments, NULL);
res = false;
if (!vec_str_any(&info.environement, find_null, &res) && res)
if (!vec_str_any(&info.environement, _find_null, &res) && res)
vec_str_push(&info.environement, NULL);
execve(info.binary_path, info.arguments.buffer, info.environement.buffer);
return (NO_ERROR);
@ -79,12 +78,11 @@ 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_find_chr(info->binary_path, '/') != NULL)
string_push(&s, info->binary_path);
else
{
if (vec_str_find(&info->environement, find_path, &p_idx))
if (vec_str_find(&info->environement, _find_path, &p_idx))
return (string_free(s), ERROR);
if (in_path(info, process, info->environement.buffer[p_idx], &s))
return (ERROR);
@ -98,7 +96,8 @@ 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 _process_cleanup(t_spawn_info info, t_process *process, \
bool cleanup_process)
{
if (cleanup_process && process->stdin)
close_fd(process->stdin);
@ -116,16 +115,18 @@ static void cleanup(t_spawn_info info, t_process *process, bool cleanup_process)
t_error spawn_process(t_spawn_info info, t_process *process)
{
if (process == NULL)
return (ERROR);
if (handle_redirections(&info, process))
return (cleanup(info, process, true), ERROR);
return (_process_cleanup(info, process, true), ERROR);
if (find_binary(&info, process))
return (cleanup(info, process, true), ERROR);
return (_process_cleanup(info, process, true), ERROR);
process->pid = fork();
if (process->pid == 0)
(spawn_process_exec(info, process), exit(1));
else
{
cleanup(info, process, false);
_process_cleanup(info, process, false);
if (process->pid == -1)
return (ERROR);
}

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process_inner2.c :+: :+: :+: */
/* process_redirection.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 22:27:00 by maiboyer #+# #+# */
/* Updated: 2024/08/01 07:14:10 by maiboyer ### ########.fr */
/* Updated: 2024/08/02 19:10:51 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,7 @@
#include "me/os/os.h"
#include "me/types.h"
void handle_redirections_inherited(t_spawn_info *info, t_process *process)
t_error _handle_redirections_inherited(t_spawn_info *info, t_process *process)
{
(void)(process);
if (info->stdin.tag == R_INHERITED)
@ -32,9 +32,10 @@ void handle_redirections_inherited(t_spawn_info *info, t_process *process)
info->stderr = fd(dup_fd(get_stderr()));
process->stderr = dup_fd(get_stderr());
}
return (NO_ERROR);
}
void handle_redirections_fds(t_spawn_info *info, t_process *process)
t_error _handle_redirections_fds(t_spawn_info *info, t_process *process)
{
if (info->stdin.tag == R_FD)
{
@ -51,22 +52,13 @@ void handle_redirections_fds(t_spawn_info *info, t_process *process)
info->stderr = fd(dup_fd(info->stderr.fd.fd));
process->stderr = dup_fd(info->stderr.fd.fd);
}
return (NO_ERROR);
}
static inline void redirection_inner(t_spawn_info *info, t_process *process)
{
process->stderr = NULL;
process->stdout = NULL;
process->stdin = NULL;
handle_redirections_fds(info, process);
handle_redirections_inherited(info, process);
}
t_error handle_redirections(t_spawn_info *info, t_process *process)
t_error _handle_redirections_pipe(t_spawn_info *info, t_process *process)
{
t_pipe pipe_fd;
redirection_inner(info, process);
if (info->stdin.tag == R_PIPED)
{
if (create_pipe(&pipe_fd))
@ -90,3 +82,17 @@ t_error handle_redirections(t_spawn_info *info, t_process *process)
}
return (NO_ERROR);
}
t_error handle_redirections(t_spawn_info *info, t_process *process)
{
process->stderr = NULL;
process->stdout = NULL;
process->stdin = NULL;
if (_handle_redirections_fds(info, process))
return (ERROR);
if (_handle_redirections_inherited(info, process))
return (ERROR);
if (_handle_redirections_pipe(info, process))
return (ERROR);
return (NO_ERROR);
}