42_minishell/builtins/echo.c

65 lines
1.6 KiB
C
Raw Normal View History

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* echo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 13:09:08 by erey-bet #+# #+# */
2023-03-13 11:30:45 -04:00
/* Updated: 2023/03/13 16:29:47 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');
}
2023-02-22 07:24:35 -05:00
int check_argument(char *str, int *check)
{
2023-02-22 07:24:35 -05:00
int i;
2023-02-21 16:10:40 -05:00
i = -1;
2023-02-22 07:24:35 -05:00
while (str[++i])
if (str[i] != '-' && str[i] != 'n')
return (1);
if (ft_strnstr(str, "n", ft_strlen(str)))
*check = 1;
2023-03-13 11:30:45 -04:00
else
return (1);
2023-02-22 07:24:35 -05:00
return (0);
2023-02-21 09:42:05 -05:00
}
int echo(int fd, char **strs)
{
2023-02-21 09:42:05 -05:00
int check;
int i;
check = 0;
2023-02-22 07:24:35 -05:00
i = -1;
while (strs[++i])
{
while (is_space(*strs[i]))
strs[i]++;
2023-03-13 11:30:45 -04:00
if (check == 2 || check_argument(strs[i], &check))
2023-02-22 07:24:35 -05:00
{
2023-03-13 11:30:45 -04:00
check = 2;
2023-02-22 07:24:35 -05:00
ft_putstr_fd(strs[i], fd);
if (strs[i + 1] != NULL)
write(fd, " ", 1);
}
}
if (!check)
write(fd, "\n", 1);
2023-02-17 10:54:58 -05:00
return (0);
}
2023-02-21 10:03:08 -05:00
2023-02-21 16:10:40 -05:00
/*int main(int argc, char *argv[])
2023-02-21 10:03:08 -05:00
{
2023-02-22 07:24:35 -05:00
echo(1, argv + 1);
2023-02-21 10:03:08 -05:00
return (0);
2023-02-21 16:10:40 -05:00
}*/