From e75c563eb768e9cbafb7429d12a0191fb8ae0c12 Mon Sep 17 00:00:00 2001 From: Kyungjun Lee Date: Tue, 9 Apr 2024 18:03:36 +0900 Subject: [PATCH] fix echo (accept -n multiple times; fix github action --- codes/builtins/builtin_echo.c | 50 ++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/codes/builtins/builtin_echo.c b/codes/builtins/builtin_echo.c index 54858cb..eacc533 100644 --- a/codes/builtins/builtin_echo.c +++ b/codes/builtins/builtin_echo.c @@ -6,13 +6,14 @@ /* By: kyungjle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/05 12:45:54 by kyungjle #+# #+# */ -/* Updated: 2024/04/09 14:55:55 by kyungjle ### ########.fr */ +/* Updated: 2024/04/09 18:01:44 by kyungjle ### ########.fr */ /* */ /* ************************************************************************** */ #include "builtin.h" -int builtin_echo(char *args[], t_ld_map_env *env); +int builtin_echo(char *args[], t_ld_map_env *env); +static t_bool parse_args(char *args[], int *index); /* int builtin_echo(char *args[], t_ld_map_env *env) @@ -22,24 +23,43 @@ int builtin_echo(char *args[], t_ld_map_env *env) */ int builtin_echo(char *args[], t_ld_map_env *env) { - int i; + int index; + t_bool print_nl; (void) env; - i = 1; - if (args[1] != NULL - && ft_strlen(args[1]) == 2 && ft_strncmp(args[1], "-n", 2) == 0) - i += 1; - while (args[i] != NULL) + print_nl = parse_args(args, &index); + while (args[index] != NULL) { - printf("%s", args[i]); - if (args[i + 1] != NULL) + printf("%s", args[index]); + if (args[index + 1] != NULL) printf(" "); - i++; + index++; } - if (args[1] != NULL - && !(ft_strlen(args[1]) == 2 && ft_strncmp(args[1], "-n", 2) == 0)) - printf("\n"); - if (args[1] == NULL) + if (print_nl || args[1] == NULL) printf("\n"); return (EXIT_SUCCESS); } + +static t_bool parse_args(char *args[], int *index) +{ + t_bool ret; + int i; + + ret = TRUE; + *index = 1; + while (args[*index] != NULL) + { + if (args[*index][0] != '-') + break ; + i = 1; + while (args[*index][i] != '\0') + { + if (args[*index][i] != 'n') + return (ret); + i += 1; + } + ret = FALSE; + *index += 1; + } + return (ret); +}