Skip to content

Commit

Permalink
Fix most exit bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
arzelcm committed Jun 29, 2024
1 parent 55f3983 commit bf2f342
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
9 changes: 7 additions & 2 deletions lib/libft/ft_strtrim.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: arcanava <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/12 12:21:41 by arcanava #+# #+# */
/* Updated: 2024/01/19 19:32:20 by arcanava ### ########.fr */
/* Updated: 2024/06/29 23:53:03 by arcanava ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,7 +23,12 @@ static int included(char c, char const *set)
included = set[i++] == c;
return (included);
}

/**
* @brief
* @param s1
* @param set
* @return malloc char*
*/
char *ft_strtrim(char const *s1, char const *set)
{
size_t i;
Expand Down
26 changes: 17 additions & 9 deletions src/executor/builtins/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,43 @@
/* By: arcanava <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/29 21:58:31 by arcanava #+# #+# */
/* Updated: 2024/06/29 21:58:32 by arcanava ### ########.fr */
/* Updated: 2024/06/29 23:58:48 by arcanava ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"
#include "tokenizer_utils.h"
#include "libft.h"
#include "context.h"
#include "limits.h"
#include "environment_helper.h"
#include "builtins.h"
#include "utils.h"

static void throw_err_numeric_argument(char **argv, t_context *context)
{
ft_printff(STDERR_FILENO,
"%s: exit: %s: numeric argument required\n",
PROGRAM_NAME, argv[1]);
context->err_code = ABNORMAL_EXIT_STATUS;
custom_exit(context, 0);
}

// TODO: Limit to long long max and long long min n
int ft_exit(int argc, char **argv, t_context *context)
{
char *trimed_arg;

if (isatty(STDIN_FILENO))
ft_printff(STDERR_FILENO, "exit\n");
if (argc > 1)
{
if (ft_isnum(argv[1], LLONG_MAX))
trimed_arg = ft_strtrim(argv[1], " \t");
if (ft_isnum(trimed_arg, LLONG_MAX))
context->err_code = ft_atoi(argv[1]);
else
{
ft_printff(STDERR_FILENO,
"%s: exit: %s: numeric argument required\n",
PROGRAM_NAME, argv[1]);
context->err_code = ABNORMAL_EXIT_STATUS;
custom_exit(context, 0);
}
throw_err_numeric_argument(argv, context);
free(trimed_arg);
}
if (argc > 2)
{
Expand Down

0 comments on commit bf2f342

Please sign in to comment.