mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
exit: handle invalid and large arguments
This commit is contained in:
parent
f1c132337b
commit
715c2aced8
2 changed files with 104 additions and 3 deletions
|
|
@ -6,21 +6,73 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/01 18:17:56 by khais #+# #+# */
|
/* Created: 2025/04/01 18:17:56 by khais #+# #+# */
|
||||||
/* Updated: 2025/04/02 15:06:22 by khais ### ########.fr */
|
/* Updated: 2025/04/15 11:48:05 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "builtins.h"
|
#include "builtins.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
#include "simple_cmd_execute.h"
|
||||||
|
#include "unistd.h"
|
||||||
|
|
||||||
|
static int ft_isspace(char c)
|
||||||
|
{
|
||||||
|
return ((c == ' ' || c == '\f' || c == '\n'
|
||||||
|
|| c == '\r' || c == '\t' || c == '\v'));
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ft_atoi_strict(const char *nptr, bool *ok)
|
||||||
|
{
|
||||||
|
int sign;
|
||||||
|
int calc;
|
||||||
|
|
||||||
|
sign = 1;
|
||||||
|
calc = 0;
|
||||||
|
*ok = true;
|
||||||
|
while (*nptr && ft_isspace(*nptr))
|
||||||
|
nptr++;
|
||||||
|
if (*nptr == '+' || *nptr == '-')
|
||||||
|
{
|
||||||
|
if (nptr[0] == '-')
|
||||||
|
sign = -1;
|
||||||
|
nptr++;
|
||||||
|
}
|
||||||
|
while (*nptr && ft_isdigit(*nptr))
|
||||||
|
{
|
||||||
|
calc = calc * 10 + (*nptr - '0');
|
||||||
|
nptr++;
|
||||||
|
}
|
||||||
|
if (*nptr != '\0')
|
||||||
|
*ok = false;
|
||||||
|
return (sign * calc);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int numeric_arg_required(char *arg)
|
||||||
|
{
|
||||||
|
ft_dprintf(STDERR_FILENO,
|
||||||
|
"minishell: exit: %s: numeric argument required\n", arg);
|
||||||
|
return (2);
|
||||||
|
}
|
||||||
|
|
||||||
t_builtin_type builtin_exit(t_simple_cmd *cmd, t_minishell *app)
|
t_builtin_type builtin_exit(t_simple_cmd *cmd, t_minishell *app)
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
|
bool ok;
|
||||||
|
|
||||||
status = 0;
|
status = 0;
|
||||||
if (cmd->words->next != NULL)
|
if (cmd->words->next != NULL)
|
||||||
status = ft_atoi(cmd->words->next->word->word);
|
{
|
||||||
|
status = ft_atoi_strict(cmd->words->next->word->word, &ok);
|
||||||
|
if (!ok)
|
||||||
|
status = numeric_arg_required(cmd->words->next->word->word);
|
||||||
|
else if (cmd->words->next->next != NULL)
|
||||||
|
{
|
||||||
|
ft_dprintf(STDERR_FILENO, "minishell: exit: too many arguments\n");
|
||||||
|
app->last_return_value = 1;
|
||||||
|
return (BUILTIN_EXIT);
|
||||||
|
}
|
||||||
|
}
|
||||||
simple_cmd_destroy(cmd);
|
simple_cmd_destroy(cmd);
|
||||||
env_destroy(app->env);
|
env_destroy(app->env);
|
||||||
exit(status);
|
exit(status);
|
||||||
|
|
|
||||||
49
test.sh
49
test.sh
|
|
@ -375,6 +375,55 @@ expecting <<EOF
|
||||||
99
|
99
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
|
when_run <<EOF "exit works with extreme exit codes"
|
||||||
|
$MINISHELL
|
||||||
|
exit 1234567890
|
||||||
|
echo \$?
|
||||||
|
$MINISHELL
|
||||||
|
exit -1234567890
|
||||||
|
echo \$?
|
||||||
|
EOF
|
||||||
|
expecting <<EOF
|
||||||
|
210
|
||||||
|
46
|
||||||
|
EOF
|
||||||
|
|
||||||
|
when_run <<EOF "exit works with nan input"
|
||||||
|
$MINISHELL
|
||||||
|
exit not-a-number
|
||||||
|
echo \$?
|
||||||
|
EOF
|
||||||
|
expecting <<EOF
|
||||||
|
minishell: exit: not-a-number: numeric argument required
|
||||||
|
2
|
||||||
|
EOF
|
||||||
|
|
||||||
|
when_run <<EOF "exit works with too many arguments"
|
||||||
|
$MINISHELL
|
||||||
|
exit 1 2
|
||||||
|
echo \$?
|
||||||
|
exit
|
||||||
|
echo \$?
|
||||||
|
$MINISHELL
|
||||||
|
exit not-a-number 3
|
||||||
|
echo \$?
|
||||||
|
$MINISHELL
|
||||||
|
exit 3 not-a-number
|
||||||
|
echo \$?
|
||||||
|
exit
|
||||||
|
echo \$?
|
||||||
|
EOF
|
||||||
|
expecting <<EOF
|
||||||
|
minishell: exit: too many arguments
|
||||||
|
1
|
||||||
|
0
|
||||||
|
minishell: exit: not-a-number: numeric argument required
|
||||||
|
2
|
||||||
|
minishell: exit: too many arguments
|
||||||
|
1
|
||||||
|
0
|
||||||
|
EOF
|
||||||
|
|
||||||
when_run <<EOF "quoted parentheses are not operators"
|
when_run <<EOF "quoted parentheses are not operators"
|
||||||
echo unclosed '('
|
echo unclosed '('
|
||||||
EOF
|
EOF
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue