update: fixed error on pipe+redirection

This commit is contained in:
maix0 2024-10-11 22:20:06 +02:00
parent 1dfb7f7ef7
commit baa9af918f
9 changed files with 34 additions and 37 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 11:08:03 by maiboyer #+# #+# */
/* Updated: 2024/08/04 14:28:13 by maiboyer ### ########.fr */
/* Updated: 2024/10/11 22:15:08 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,10 +6,11 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
/* Updated: 2024/10/11 11:47:20 by maiboyer ### ########.fr */
/* Updated: 2024/10/11 22:19:32 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "fcntl.h"
#include "me/fs/fs.h"
#include "me/mem/mem.h"
#include "me/os/os.h"
@ -29,6 +30,7 @@ t_error handle_redirections(t_spawn_info *info, t_process *process);
t_error spawn_process_exec(t_spawn_info info, t_process *process)
{
(void)(process);
if (info.forked_free)
info.forked_free(info.forked_free_args);
signal(SIGINT, SIG_DFL);
@ -36,12 +38,12 @@ t_error spawn_process_exec(t_spawn_info info, t_process *process)
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 (info.stdin.fd.fd->fd != 0)
close_fd(info.stdin.fd.fd);
if (info.stdout.fd.fd->fd != 1)
close_fd(info.stdout.fd.fd);
if (info.stderr.fd.fd->fd != 2)
close_fd(info.stderr.fd.fd);
vec_str_push(&info.arguments, NULL);
vec_str_push(&info.environement, NULL);
execve(info.binary_path, info.arguments.buffer, info.environement.buffer);
@ -62,7 +64,6 @@ t_error in_path(t_spawn_info *info, t_process *process, t_const_str path_raw,
{
string_clear(s);
me_printf_str(s, "%s/%s", path.buffer[idx++], info->binary_path);
printf("testing %s\n", s->buf);
if (access(s->buf, X_OK) == 0)
return (vec_str_free(path), NO_ERROR);
}
@ -105,9 +106,12 @@ static void _process_cleanup(t_spawn_info info, t_process *process, \
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);
if (info.stdin.fd.fd->fd != 0)
close_fd(info.stdin.fd.fd);
if (info.stdout.fd.fd->fd != 1)
close_fd(info.stdout.fd.fd);
if (info.stderr.fd.fd->fd != 2)
close_fd(info.stderr.fd.fd);
vec_str_free(info.arguments);
vec_str_free(info.environement);
mem_free(info.binary_path);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 22:27:00 by maiboyer #+# #+# */
/* Updated: 2024/09/06 15:28:26 by rparodi ### ########.fr */
/* Updated: 2024/10/11 22:10:05 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,18 +19,18 @@ t_error _handle_redirections_inherited(t_spawn_info *info, t_process *process)
(void)(process);
if (info->stdin.tag == R_INHERITED)
{
info->stdin = fd(dup_fd(get_stdin()));
process->stdin = dup_fd(get_stdin());
info->stdin = fd(get_stdin());
process->stdin = NULL;
}
if (info->stdout.tag == R_INHERITED)
{
info->stdout = fd(dup_fd(get_stdout()));
process->stdout = dup_fd(get_stdout());
info->stdout = fd(get_stdout());
process->stdout = NULL;
}
if (info->stderr.tag == R_INHERITED)
{
info->stderr = fd(dup_fd(get_stderr()));
process->stderr = dup_fd(get_stderr());
info->stderr = fd(get_stderr());
process->stderr = NULL;
}
return (NO_ERROR);
}
@ -40,17 +40,17 @@ t_error _handle_redirections_fds(t_spawn_info *info, t_process *process)
if (info->stdin.tag == R_FD)
{
info->stdin = fd(dup_fd(info->stdin.fd.fd));
process->stdin = dup_fd(info->stdin.fd.fd);
process->stdin = NULL;
}
if (info->stdout.tag == R_FD)
{
info->stdout = fd(dup_fd(info->stdout.fd.fd));
process->stdout = dup_fd(info->stdout.fd.fd);
process->stdout = NULL;
}
if (info->stderr.tag == R_FD)
{
info->stderr = fd(dup_fd(info->stderr.fd.fd));
process->stderr = dup_fd(info->stderr.fd.fd);
process->stderr = NULL;
}
return (NO_ERROR);
}