minishell/src/executor/executor_child.c
2025-03-02 22:35:00 +01:00

52 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* :::::::: */
/* executor_child.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/12 21:25:10 by willem #+# #+# */
/* Updated: 2025/03/02 22:28:55 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int is_dir(char *path)
{
struct stat path_stats;
if (stat(path, &path_stats) < 0)
{
error_msg("is_dir", "path could not be read");
return (0);
}
return (S_ISDIR(path_stats.st_mode));
}
void executor_child(t_minishell *msh, t_command *command)
{
char *path;
if (command->fd_in != 0)
dup2(command->fd_in, 0);
if (command->fd_out != 1)
dup2(command->fd_out, 1);
executor_close_fds(command->n_fds);
path = executor_absolute_path(msh, command->args[0]);
if (path == NULL || access(path, F_OK | X_OK) != 0)
{
errno = 0;
error_msg("minishell", "command not found");
return ;
}
if (is_dir(path))
{
ft_putstr_fd(RED BOLD, 2);
ft_putstr_fd("minishell: ", 2);
ft_putstr_fd(command->args[0], 2);
ft_putstr_fd(": " RESET "Is a directory\n", 2);
}
execve(path, command->args, environment_get_arr(msh));
}