From 012c23e4f2c5181d426ebaae0b3041462eea09f9 Mon Sep 17 00:00:00 2001 From: Lilia-42 Date: Thu, 22 Aug 2024 13:40:10 +0200 Subject: [PATCH] Add echo builtin function --- Makefile | 1 + inc/execution.h | 3 ++- src/builtins/echo.c | 57 +++++++++++++++++++++++++++++++++++++++++++++ src/execution.c | 8 +++++-- 4 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 src/builtins/echo.c diff --git a/Makefile b/Makefile index b5ed745..05e8ece 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,7 @@ SOURCES := main.c \ helpers.c \ \ builtins/cd.c \ + builtins/echo.c \ SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES)) diff --git a/inc/execution.h b/inc/execution.h index b413c7b..84a5558 100644 --- a/inc/execution.h +++ b/inc/execution.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 17:40:19 by ljiriste #+# #+# */ -/* Updated: 2024/08/02 17:42:18 by ljiriste ### ########.fr */ +/* Updated: 2024/08/22 13:35:41 by lnikolov ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,5 +18,6 @@ const char *get_env_var_value(const t_execution_env *env, int set_env_var_value(t_execution_env *env, const char *var_name, const char *new_value); int cd(int argc, char **argv, t_execution_env *env); +int echo(int argc, char **argv); #endif // EXECUTION_H diff --git a/src/builtins/echo.c b/src/builtins/echo.c new file mode 100644 index 0000000..2b45983 --- /dev/null +++ b/src/builtins/echo.c @@ -0,0 +1,57 @@ +#include +#include "minishell.h" +#include "execution.h" +#include + +// size_t ft_strlen(const char *str) //libft () +// { +// unsigned int i; +// i = 0; +// while (str[i] != '\0') +// { +// i++; +// } +// return (i); +// } + +// void ft_putstr_fd(char *s, int fd) //libft fuction +// { +// if (!s) +// return; +// write(fd, s, ft_strlen(s)); +// } + +int ft_check_n(char *str) +{ + if (str[0] == '-' && str[1] == 'n') + return (1); + + return (0); +} + +int echo(int argc, char **argv) +{ + if (argc == 1) + return (1); + int i = 1; + int flag = 0; + int t; + + t = ft_check_n(argv[i]); + if (t == 1) + { + flag = 1; + i++; + } + while (i < argc) + { + ft_putstr_fd(argv[i], 1); + if (i + 1 < argc) + ft_putstr_fd(" ", 1); + i++; + } + if (flag == 0) + ft_putstr_fd("\n", 1); + + return 1; +} diff --git a/src/execution.c b/src/execution.c index 819fcf1..4dc97fe 100644 --- a/src/execution.c +++ b/src/execution.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* execution.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ljiriste +#+ +:+ +#+ */ +/* By: lnikolov ret_val = cd(count_fields(fields), fields, env); + if (!ft_strcmp(fields[0], "echo")) + env->ret_val = echo(count_fields(fields), fields); else return (-1); return (0); -- 2.30.2