exec: correct error and return value when cmd is a directory

minishell: /: Is a directory
$? = 126
This commit is contained in:
Khaïs COLIN 2025-04-07 18:41:44 +02:00
parent d08c9a6727
commit 386d2bcb3a
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
2 changed files with 85 additions and 6 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/02 18:19:23 by khais #+# #+# */
/* Updated: 2025/04/02 18:20:54 by khais ### ########.fr */
/* Updated: 2025/04/08 13:23:59 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,10 @@
#include "../../env/env_convert.h"
#include "../../parser/simple_cmd/simple_cmd.h"
#include "../../subst/path_split.h"
#include <dirent.h>
#include "../../ft_errno.h"
#include <stdio.h>
#include <errno.h>
static char **argv_from_wordlist(t_wordlist *wordlist)
{
@ -31,20 +34,51 @@ static char **argv_from_wordlist(t_wordlist *wordlist)
return (out);
}
static void ft_execve(char *exe, char **argv, char **envp)
static bool ft_is_directory(char *path)
{
DIR *dir;
bool retvalue;
errno = 0;
retvalue = true;
dir = opendir(path);
if (errno == ENOTDIR)
retvalue = false;
if (dir != NULL)
closedir(dir);
return (retvalue);
}
static int ft_execve(char *exe, char **argv, char **envp)
{
int retvalue;
retvalue = 127;
execve(exe, argv, envp);
if (ft_is_directory(exe))
{
ft_dprintf(STDERR_FILENO, "minishell: %s: %s\n", argv[0],
strerror(EISDIR));
retvalue = 126;
}
else
{
ft_dprintf(STDERR_FILENO, "minishell: %s: ", argv[0]);
perror(NULL);
ft_perror(NULL);
}
free(exe);
path_split_destroy(argv);
path_split_destroy(envp);
return (retvalue);
}
void execute_subprocess(char *exe, t_simple_cmd *cmd, t_minishell *app)
{
ft_execve(exe, argv_from_wordlist(cmd->words), envp_from_env(app->env));
int retvalue;
retvalue = ft_execve(exe, argv_from_wordlist(cmd->words),
envp_from_env(app->env));
simple_cmd_destroy(cmd);
env_destroy(app->env);
exit(127);
exit(retvalue);
}

45
test.sh
View file

@ -131,6 +131,51 @@ EOF
expecting <<EOF
EOF
when_run <<EOF "/ is a directory"
/
echo \$?
EOF
expecting <<EOF
minishell: /: Is a directory
126
EOF
when_run <<EOF "// is a directory"
//
echo \$?
EOF
expecting <<EOF
minishell: //: Is a directory
126
EOF
when_run <<EOF "/. is a directory"
/.
echo \$?
EOF
expecting <<EOF
minishell: /.: Is a directory
126
EOF
when_run <<EOF "/./../../../../.. is a directory"
/./../../../../..
echo \$?
EOF
expecting <<EOF
minishell: /./../../../../..: Is a directory
126
EOF
when_run <<EOF "//////// is a directory"
////////
echo \$?
EOF
expecting <<EOF
minishell: ////////: Is a directory
126
EOF
when_run <<EOF "simple commands are run"
echo no files:
ls -a