42_minishell/builtins/echo.c
Etienne Rey-bethbeder 10db23e4ef CAMILLE BOUFON
2023-04-11 17:26:49 +02:00

64 lines
1.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* echo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 13:09:08 by erey-bet #+# #+# */
/* Updated: 2023/04/11 14:57:00 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./builtins_private.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_backslash_n)
{
int i;
i = -1;
while (str[++i])
if (str[i] != '-' && str[i] != 'n')
return (1);
if (ft_strnstr(str, "n", ft_strlen(str)) && str[0] == '-')
*check_backslash_n = 1;
else
return (1);
return (0);
}
int echo(int fd, char **strs)
{
int check_backslash_n;
int check_start_write;
int i;
int y;
check_backslash_n = 0;
check_start_write = 0;
i = -1;
while (strs[++i])
{
y = -1;
while (is_space(strs[i][++y]))
;
if (check_start_write == 1
|| check_argument(strs[i], &check_backslash_n))
{
check_start_write = 1;
ft_putstr_fd(strs[i], fd);
if (strs[i + 1] != NULL)
write(fd, " ", 1);
}
}
if (!check_backslash_n)
write(fd, "\n", 1);
return (0);
}