From 4af738e2ee30a83458de8c005c39c68001b7d931 Mon Sep 17 00:00:00 2001 From: joris schaft Date: Tue, 25 Jun 2024 13:55:51 +0200 Subject: [PATCH 1/3] [ADD] echo --- Makefile | 2 +- include/builtins.h | 6 +++++- src/echo_builtin.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/echo_builtin.c diff --git a/Makefile b/Makefile index 73339b5..87d92f7 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ SRCS = src/ # include directory INCLUDES = include/ libtf/ libft/ # .c files in src/ without the extension -CODE = main ask_command error path parse_command exec_command builtin \ +CODE = main ask_command error path parse_command exec_command builtin echo_builtin\ signals cool_readline # directories to 'make' LIBRARIES = libtf libft diff --git a/include/builtins.h b/include/builtins.h index d604fd7..912103f 100644 --- a/include/builtins.h +++ b/include/builtins.h @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */ -/* Updated: 2024/06/25 11:38:41 by jschaft ### ########.fr */ +/* Updated: 2024/06/25 13:31:08 by jschaft ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,4 +21,8 @@ bool is_builtin(const char *str); // Execute the builtin command int exec_builtin(t_call call, char *const envp[]); +// Execute echo builtin +int echo_builtin(const char **argv); + + #endif diff --git a/src/echo_builtin.c b/src/echo_builtin.c new file mode 100644 index 0000000..5a80eeb --- /dev/null +++ b/src/echo_builtin.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* echo_builtin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jschaft +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/25 13:28:29 by jschaft #+# #+# */ +/* Updated: 2024/06/25 13:55:12 by jschaft ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "include.h" + +int echo_builtin(const char **argv) +{ + int i; + int opt; + + i = 1; + opt = 0; + if (argv[1] != NULL) + { + if (!ft_strncmp(argv[1], "-n", 3)) + { + opt = 1; + i++; + } + } + while (argv[i] != NULL) + { + printf("%s", argv[i]); + if (argv[i + 1] != NULL) + printf(" "); + i++; + } + if (opt == 0) + printf("\n"); + return (0); +} From a0f6ecb7a66ccc8ddcffe6e4c7a703d8c24323d8 Mon Sep 17 00:00:00 2001 From: joris schaft Date: Tue, 25 Jun 2024 14:12:40 +0200 Subject: [PATCH 2/3] [ADD] clear and echo finished --- src/builtin.c | 4 +++- src/main.c | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/builtin.c b/src/builtin.c index 3f29f8d..8b467f2 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/03 12:02:45 by jschaft #+# #+# */ -/* Updated: 2024/06/25 11:46:40 by jschaft ### ########.fr */ +/* Updated: 2024/06/25 13:58:48 by jschaft ### ########.fr */ /* */ /* ************************************************************************** */ @@ -88,6 +88,8 @@ int exec_builtin(t_call call, char *const envp[]) return (unset_builtin());*/ if (ft_strncmp(call.program, "pwd", 4) == 0) return (pwd_builtin()); + if (ft_strncmp(call.program, "echo", 5) == 0) + return (echo_builtin((const char **)call.argv)); if (ft_strncmp(call.program, "env", 4) == 0) return (env_builtin(envp)); return (1); diff --git a/src/main.c b/src/main.c index 4040605..b4a00e1 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */ -/* Updated: 2024/06/21 15:48:48 by mcolonna ### ########.fr */ +/* Updated: 2024/06/25 14:08:55 by jschaft ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,6 +49,7 @@ static int do_command(t_env *env, const char *str) static void start(t_env *env) { env->mc_command = mem_subclass(fatal_error, env->mc_global); + do_command(env, "clear"); do_command(env, "cat ./header"); do_command(env, "echo"); mem_freeall(env->mc_command); From a61dac2ec0e47f445d4513d19aebf8fba21f3ef5 Mon Sep 17 00:00:00 2001 From: mcolonna Date: Thu, 27 Jun 2024 15:51:58 +0200 Subject: [PATCH 3/3] dev: simplify echo's source code --- src/echo_builtin.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/echo_builtin.c b/src/echo_builtin.c index 5a80eeb..f50d704 100644 --- a/src/echo_builtin.c +++ b/src/echo_builtin.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/25 13:28:29 by jschaft #+# #+# */ -/* Updated: 2024/06/25 13:55:12 by jschaft ### ########.fr */ +/* Updated: 2024/06/27 15:51:09 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,18 +14,15 @@ int echo_builtin(const char **argv) { - int i; - int opt; + int i; + bool newline; i = 1; - opt = 0; - if (argv[1] != NULL) + newline = true; + if (argv[1] && str_eq(argv[1], "-n")) { - if (!ft_strncmp(argv[1], "-n", 3)) - { - opt = 1; - i++; - } + newline = false; + i++; } while (argv[i] != NULL) { @@ -34,7 +31,7 @@ int echo_builtin(const char **argv) printf(" "); i++; } - if (opt == 0) - printf("\n"); + if (newline) + printf("\n"); return (0); }