mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
exec: correct error and return value when cmd is a directory
minishell: /: Is a directory $? = 126
This commit is contained in:
parent
d08c9a6727
commit
386d2bcb3a
2 changed files with 85 additions and 6 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/02 18:19:23 by khais #+# #+# */
|
/* 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 "../../env/env_convert.h"
|
||||||
#include "../../parser/simple_cmd/simple_cmd.h"
|
#include "../../parser/simple_cmd/simple_cmd.h"
|
||||||
#include "../../subst/path_split.h"
|
#include "../../subst/path_split.h"
|
||||||
|
#include <dirent.h>
|
||||||
|
#include "../../ft_errno.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
static char **argv_from_wordlist(t_wordlist *wordlist)
|
static char **argv_from_wordlist(t_wordlist *wordlist)
|
||||||
{
|
{
|
||||||
|
|
@ -31,20 +34,51 @@ static char **argv_from_wordlist(t_wordlist *wordlist)
|
||||||
return (out);
|
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);
|
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]);
|
ft_dprintf(STDERR_FILENO, "minishell: %s: ", argv[0]);
|
||||||
perror(NULL);
|
ft_perror(NULL);
|
||||||
|
}
|
||||||
free(exe);
|
free(exe);
|
||||||
path_split_destroy(argv);
|
path_split_destroy(argv);
|
||||||
path_split_destroy(envp);
|
path_split_destroy(envp);
|
||||||
|
return (retvalue);
|
||||||
}
|
}
|
||||||
|
|
||||||
void execute_subprocess(char *exe, t_simple_cmd *cmd, t_minishell *app)
|
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);
|
simple_cmd_destroy(cmd);
|
||||||
env_destroy(app->env);
|
env_destroy(app->env);
|
||||||
exit(127);
|
exit(retvalue);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
45
test.sh
45
test.sh
|
|
@ -131,6 +131,51 @@ EOF
|
||||||
expecting <<EOF
|
expecting <<EOF
|
||||||
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"
|
when_run <<EOF "simple commands are run"
|
||||||
echo no files:
|
echo no files:
|
||||||
ls -a
|
ls -a
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue