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);
}