42_minishell/builtins/echo.c
Etienne Rey-bethbeder a4befa5fa6 Finito Echo
2023-02-22 13:24:35 +01:00

62 lines
1.6 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* echo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 13:09:08 by erey-bet #+# #+# */
/* Updated: 2023/02/22 13:23:16 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;
i = -1;
while (str[++i])
if (str[i] != '-' && str[i] != 'n')
return (1);
if (ft_strnstr(str, "n", ft_strlen(str)))
*check = 1;
return (0);
}
int echo(int fd, char **strs)
{
int check;
int i;
check = 0;
i = -1;
while (strs[++i])
{
while (is_space(*strs[i]))
strs[i]++;
if (check == 1 || check_argument(strs[i], &check))
{
ft_putstr_fd(strs[i], fd);
if (strs[i + 1] != NULL)
write(fd, " ", 1);
}
}
if (!check)
write(fd, "\n", 1);
return (0);
}
/*int main(int argc, char *argv[])
{
echo(1, argv + 1);
return (0);
}*/