From d59562899b9e2267a31d03f494425b9c6f939d68 Mon Sep 17 00:00:00 2001 From: Etienne Rey-bethbeder Date: Fri, 17 Feb 2023 16:03:28 +0100 Subject: [PATCH] Ajout de Echo, a voir si il y a des bugs --- echo.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 echo.c diff --git a/echo.c b/echo.c new file mode 100644 index 0000000..8dfdc5f --- /dev/null +++ b/echo.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* echo.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/17 13:09:08 by erey-bet #+# #+# */ +/* Updated: 2023/02/17 16:03:06 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int is_space(char c) +{ + return (c == ' ' || c == '\f' || c == '\v' || c == '\t' + || c == '\r' || c == '\n'); +} + +int check_argument(char *str, int *check, int i) +{ + int y; + + y = 0; + if (str[i] == '-') + { + while (!is_space(str[i]) || (str[i + 1] == '-' )) + { + i++; + if (is_space(str[i])) + { + y = i; + *check = 1; + } + else if (str[i] == '-' && str[i - 1] != '-') + ; + else if (str[i] != 'n') + break ; + } + i = y; + while (is_space(str[i])) + i++; + } + return (i); +} + +void echo(int fd, char *str) +{ + int check; + int i; + + check = 0; + i = 0; + while (is_space(str[i])) + i++; + i = check_argument(str, &check, i); + while (str[i]) + ft_putchar_fd(fd, str[i++]); + if (!check) + write(fd, "\n", 1); +}