From 4af738e2ee30a83458de8c005c39c68001b7d931 Mon Sep 17 00:00:00 2001 From: joris schaft Date: Tue, 25 Jun 2024 13:55:51 +0200 Subject: [PATCH] [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); +}