From 7edf6ec8f4ff629bebbf1301957786016685a66c Mon Sep 17 00:00:00 2001 From: dayano74 Date: Sun, 4 May 2025 17:06:20 +0900 Subject: [PATCH 1/5] fix print_cmd_not_found func --- src/invoke_cmd/execute_cmd_helper.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/invoke_cmd/execute_cmd_helper.c b/src/invoke_cmd/execute_cmd_helper.c index c3ba59b..72f7f1e 100644 --- a/src/invoke_cmd/execute_cmd_helper.c +++ b/src/invoke_cmd/execute_cmd_helper.c @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/21 21:37:20 by dayano #+# #+# */ -/* Updated: 2025/04/25 20:50:51 by dayano ### ########.fr */ +/* Updated: 2025/05/04 17:04:58 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,10 +14,10 @@ void print_cmd_not_found(t_cmd *cmd) { - ft_putstr_fd("minish: ", STDERR_FILENO); ft_putstr_fd(cmd->argv[0], STDERR_FILENO); - ft_putstr_fd(": command not found", STDERR_FILENO); + ft_putstr_fd(": command not found\n", STDERR_FILENO); cmd->status = CMD_NOT_FOUND_EXIT_STATUS; + exit(CMD_NOT_FOUND_EXIT_STATUS); } void free_str_array(char **str) From 8efcd966363a6be30dac58173da3e73aca874b1b Mon Sep 17 00:00:00 2001 From: dayano74 Date: Sun, 4 May 2025 17:19:07 +0900 Subject: [PATCH 2/5] update built-in exit --- src/builtin/exit.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/builtin/exit.c b/src/builtin/exit.c index ff0dbaa..cc1f29e 100644 --- a/src/builtin/exit.c +++ b/src/builtin/exit.c @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/13 21:20:48 by dayano #+# #+# */ -/* Updated: 2025/05/01 15:50:02 by dayano ### ########.fr */ +/* Updated: 2025/05/04 17:13:57 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,9 +15,18 @@ int builtin_exit(int argc, char *argv[], t_minish *minish) { (void)minish; - if (argc != 1) + if (argc > 2) return (error_mes(argv[0], "too many arguments"), EXIT_FAILURE); - if (printf("exit\n") < 0) - return (perror("printf"), EXIT_FAILURE); - exit(EXIT_SUCCESS); + if(argc == 1) + { + if (printf("exit\n") < 0) + return (perror("printf"), EXIT_FAILURE); + exit(EXIT_SUCCESS); + } + else + { + if (printf("exit\n") < 0) + return (perror("printf"), EXIT_FAILURE); + exit(argv[1]); + } } From fffe15be4dd2a116007782a860e2d0bc26ad1a0a Mon Sep 17 00:00:00 2001 From: dayano74 Date: Sun, 4 May 2025 17:58:45 +0900 Subject: [PATCH 3/5] fix builit-in exit --- inc/execute_cmd.h | 3 +- lib/libft/Makefile | 7 ++- lib/libft/ft_strtol.c | 21 +++++++ lib/libft/ft_strtoll.c | 122 +++++++++++++++++++++++++++++++++++++++++ lib/libft/libft.h | 6 +- src/builtin/exit.c | 22 +++++--- 6 files changed, 166 insertions(+), 15 deletions(-) create mode 100644 lib/libft/ft_strtol.c create mode 100644 lib/libft/ft_strtoll.c diff --git a/inc/execute_cmd.h b/inc/execute_cmd.h index da773ac..2b4f722 100644 --- a/inc/execute_cmd.h +++ b/inc/execute_cmd.h @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/21 21:39:18 by dayano #+# #+# */ -/* Updated: 2025/04/28 13:56:33 by dayano ### ########.fr */ +/* Updated: 2025/05/04 17:48:44 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ # define PATH_PREFIX_LEN 5 # define EQUAL_LEN 1 +# define INCORRECT_USAGE 2 # define CMD_FAILED_EXIT_STATUS 126 # define CMD_NOT_FOUND_EXIT_STATUS 127 diff --git a/lib/libft/Makefile b/lib/libft/Makefile index 1d18d6d..beae0f5 100644 --- a/lib/libft/Makefile +++ b/lib/libft/Makefile @@ -3,10 +3,10 @@ # ::: :::::::: # # Makefile :+: :+: :+: # # +:+ +:+ +:+ # -# By: ttsubo +#+ +:+ +#+ # +# By: dayano +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/11/06 10:03:51 by ttsubo #+# #+# # -# Updated: 2025/04/17 16:32:30 by ttsubo ### ########.fr # +# Updated: 2025/05/04 17:33:14 by dayano ### ########.fr # # # # **************************************************************************** # @@ -33,7 +33,8 @@ SRCS = ft_atoi.c ft_isdigit.c ft_memmove.c ft_split.c ft_strlcpy.c ft_max.c ft_min.c ft_abs.c ft_strnlen.c ft_strlen_until.c \ ft_lstnew.c ft_lstadd_front.c ft_lstsize.c ft_lstlast.c ft_lstadd_back.c \ ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c ft_strcmp.c \ - ft_max.c ft_min.c ft_abs.c ft_strnlen.c ft_strlen_until.c ft_isspace.c + ft_max.c ft_min.c ft_abs.c ft_strnlen.c ft_strlen_until.c ft_isspace.c \ + ft_strtoll.c ft_strtol.c ifeq ($(MAKECMDGOALS), debug) CC := gcc -Wall -Wextra -Werror -g diff --git a/lib/libft/ft_strtol.c b/lib/libft/ft_strtol.c new file mode 100644 index 0000000..cf858b7 --- /dev/null +++ b/lib/libft/ft_strtol.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtol.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dayano +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/07 20:43:11 by dayano #+# #+# */ +/* Updated: 2025/05/04 17:29:50 by dayano ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include +#include +#include + +long ft_strtol(const char *nptr, char **endptr, int base) +{ + return ((long)ft_strtoll(nptr, endptr, base)); +} diff --git a/lib/libft/ft_strtoll.c b/lib/libft/ft_strtoll.c new file mode 100644 index 0000000..cbe73f4 --- /dev/null +++ b/lib/libft/ft_strtoll.c @@ -0,0 +1,122 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtoll.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dayano +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/09 21:02:46 by dayano #+# #+# */ +/* Updated: 2025/05/04 17:30:03 by dayano ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include +#include +#include + +static int parse_base_and_sign(const char **str, int base, int *sign) +{ + *sign = 1; + if (**str == '-' || **str == '+') + { + if (**str == '-') + *sign = -1; + (*str)++; + } + if (base == 0) + { + if (**str == '0') + { + (*str)++; + if (**str == 'x' || **str == 'X') + { + (*str)++; + return (16); + } + return (8); + } + return (10); + } + if (base == 16 && **str == '0' && (*(*str + 1) == 'x' || *(*str + + 1) == 'X')) + *str += 2; + return (base); +} + +static int convert_digit(char c, int base) +{ + const char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const char *p; + + p = ft_strchr(digits, ft_toupper(c)); + if (p && p - digits < base) + return (p - digits); + return (-1); +} + +static void handle_overflow(const char **str, int base, + unsigned long long *result, int sign) +{ + errno = ERANGE; + *result = (unsigned long long)LLONG_MAX + ((1 - sign) >> 1); + while (convert_digit(**str, base) >= 0) + (*str)++; +} + +static long long process_digits(const char **str, int base, int sign, + char **endptr) +{ + unsigned long long result; + unsigned long long cutoff; + int cutlim; + int digit; + + result = 0; + cutoff = (unsigned long long)LLONG_MAX + ((1 - sign) >> 1); + cutlim = cutoff % base; + cutoff /= base; + while (1) + { + digit = convert_digit(**str, base); + if (digit < 0) + break ; + if (result > cutoff || (result == cutoff && digit > cutlim)) + { + handle_overflow(str, base, &result, sign); + break ; + } + result = result * base + digit; + (*str)++; + } + if (endptr) + *endptr = (char *)*str; + return ((long long)sign * (long long)result); +} + +long long ft_strtoll(const char *nptr, char **endptr, int base) +{ + const char *str; + int sign; + long long result; + + if (base < 0 || base == 1 || base > 36) + { + errno = EINVAL; + if (endptr) + *endptr = (char *)nptr; + return (0); + } + while (ft_isspace(*nptr)) + nptr++; + str = nptr; + base = parse_base_and_sign(&str, base, &sign); + result = process_digits(&str, base, sign, endptr); + if (str == nptr) + { + if (endptr) + *endptr = (char *)nptr; + return (0); + } + return (result); +} diff --git a/lib/libft/libft.h b/lib/libft/libft.h index 9a28f09..ad5bdc5 100644 --- a/lib/libft/libft.h +++ b/lib/libft/libft.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* libft.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ttsubo +#+ +:+ +#+ */ +/* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/28 11:41:08 by ttsubo #+# #+# */ -/* Updated: 2025/04/17 16:30:51 by ttsubo ### ########.fr */ +/* Updated: 2025/05/04 17:34:18 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,6 +40,8 @@ char *ft_strnstr(const char *big, const char *little, size_t len); char *ft_strrchr(const char *s, int c); char *ft_strtrim(char const *s1, char const *set); +long ft_strtol(const char *nptr, char **endptr, int base); +long long ft_strtoll(const char *nptr, char **endptr, int base); char *ft_substr(char const *s, unsigned int start, size_t len); int ft_atoi(const char *str); int ft_isalnum(int c); diff --git a/src/builtin/exit.c b/src/builtin/exit.c index cc1f29e..b28e6f2 100644 --- a/src/builtin/exit.c +++ b/src/builtin/exit.c @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/13 21:20:48 by dayano #+# #+# */ -/* Updated: 2025/05/04 17:13:57 by dayano ### ########.fr */ +/* Updated: 2025/05/04 17:54:32 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,19 +14,23 @@ int builtin_exit(int argc, char *argv[], t_minish *minish) { + long status; + char *endptr; + (void)minish; if (argc > 2) return (error_mes(argv[0], "too many arguments"), EXIT_FAILURE); + if (printf("exit\n") < 0) + return (perror("printf"), EXIT_FAILURE); if(argc == 1) - { - if (printf("exit\n") < 0) - return (perror("printf"), EXIT_FAILURE); exit(EXIT_SUCCESS); - } - else + errno = 0; + status = ft_strtol(argv[1], &endptr, 10); + if(errno != 0 || *endptr != '\0') { - if (printf("exit\n") < 0) - return (perror("printf"), EXIT_FAILURE); - exit(argv[1]); + ft_putstr_fd("minishell: exit: ", STDERR_FILENO); + error_mes(argv[1], "numeric argument required"); + exit(INCORRECT_USAGE); } + exit((unsigned int)status); } From 36cb46dd9d6c798d935db4e4f22e25cce4cc6037 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Sun, 4 May 2025 18:15:36 +0900 Subject: [PATCH 4/5] fix execute builitin --- inc/invoke_command.h | 3 ++- src/invoke_cmd/exec_pipeline.c | 4 ++-- src/invoke_cmd/invoke_command.c | 8 ++++---- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/inc/invoke_command.h b/inc/invoke_command.h index ba7cecb..3f6ea45 100644 --- a/inc/invoke_command.h +++ b/inc/invoke_command.h @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/20 21:21:13 by dayano #+# #+# */ -/* Updated: 2025/05/01 16:38:55 by dayano ### ########.fr */ +/* Updated: 2025/05/04 18:14:32 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ int invoke_commands(t_cmd *cmd_head, t_minish *minish); int exec_unit_builtin(t_cmd *cmd, t_minish *minish); +int execute_builtin(t_cmd *cmd, t_minish *minish); // wait_pipeline.c int wait_pipeline(t_cmd *cmd_head); diff --git a/src/invoke_cmd/exec_pipeline.c b/src/invoke_cmd/exec_pipeline.c index 036488b..a986f15 100644 --- a/src/invoke_cmd/exec_pipeline.c +++ b/src/invoke_cmd/exec_pipeline.c @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/21 13:14:15 by dayano #+# #+# */ -/* Updated: 2025/05/01 16:51:13 by dayano ### ########.fr */ +/* Updated: 2025/05/04 18:14:21 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,7 +57,7 @@ static pid_t _fork_command(t_cmd *cmd, t_cmd *cmd_head, t_pipe_io *pipefds, if ((cmd->next != NULL) && is_redirect(cmd->next)) redirect(cmd->next); if (is_builtin(cmd)) - exit(exec_unit_builtin(cmd, minish)); + exit(execute_builtin(cmd, minish)); execute_cmd(cmd, minish); print_error(cmd->argv[0]); exit(EXIT_FAILURE); diff --git a/src/invoke_cmd/invoke_command.c b/src/invoke_cmd/invoke_command.c index 8aba975..5746120 100644 --- a/src/invoke_cmd/invoke_command.c +++ b/src/invoke_cmd/invoke_command.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* invoke_command.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ttsubo +#+ +:+ +#+ */ +/* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/20 21:19:33 by dayano #+# #+# */ -/* Updated: 2025/05/01 17:07:37 by ttsubo ### ########.fr */ +/* Updated: 2025/05/04 18:11:39 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ static bool is_unit_builtin(t_cmd *cmd_head) return (false); } -static int _execute_builtin(t_cmd *cmd, t_minish *minish) +int execute_builtin(t_cmd *cmd, t_minish *minish) { static const t_builtin builtin_funcs[] = { builtin_echo, builtin_pwd, builtin_exit, builtin_cd, @@ -83,7 +83,7 @@ int exec_unit_builtin(t_cmd *cmd, t_minish *minish) } if (cmd->next && is_redirect(cmd->next)) redirect(cmd->next); - return (_execute_builtin(builtin_cmd, minish)); + return (execute_builtin(builtin_cmd, minish)); } /** From 277587c41565e5b1e249601219e79c185affc513 Mon Sep 17 00:00:00 2001 From: dayano74 Date: Sun, 4 May 2025 18:35:28 +0900 Subject: [PATCH 5/5] fix norm exit.c --- src/builtin/exit.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/builtin/exit.c b/src/builtin/exit.c index b28e6f2..409c370 100644 --- a/src/builtin/exit.c +++ b/src/builtin/exit.c @@ -6,7 +6,7 @@ /* By: dayano +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/13 21:20:48 by dayano #+# #+# */ -/* Updated: 2025/05/04 17:54:32 by dayano ### ########.fr */ +/* Updated: 2025/05/04 18:34:59 by dayano ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,19 +14,19 @@ int builtin_exit(int argc, char *argv[], t_minish *minish) { - long status; - char *endptr; + long status; + char *endptr; (void)minish; if (argc > 2) return (error_mes(argv[0], "too many arguments"), EXIT_FAILURE); if (printf("exit\n") < 0) return (perror("printf"), EXIT_FAILURE); - if(argc == 1) + if (argc == 1) exit(EXIT_SUCCESS); errno = 0; status = ft_strtol(argv[1], &endptr, 10); - if(errno != 0 || *endptr != '\0') + if (errno != 0 || *endptr != '\0') { ft_putstr_fd("minishell: exit: ", STDERR_FILENO); error_mes(argv[1], "numeric argument required");