2023-02-17 10:03:28 -05:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* echo.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2023/02/17 13:09:08 by erey-bet #+# #+# */
|
2023-04-11 11:26:49 -04:00
|
|
|
/* Updated: 2023/04/11 14:57:00 by erey-bet ### ########.fr */
|
2023-02-17 10:03:28 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2023-03-10 06:32:39 -05:00
|
|
|
#include "./builtins_private.h"
|
2023-02-17 10:03:28 -05:00
|
|
|
|
|
|
|
int is_space(char c)
|
|
|
|
{
|
|
|
|
return (c == ' ' || c == '\f' || c == '\v' || c == '\t'
|
|
|
|
|| c == '\r' || c == '\n');
|
|
|
|
}
|
|
|
|
|
2023-03-13 11:51:56 -04:00
|
|
|
int check_argument(char *str, int *check_backslash_n)
|
2023-02-17 10:03:28 -05:00
|
|
|
{
|
2023-02-22 07:24:35 -05:00
|
|
|
int i;
|
2023-02-17 10:03:28 -05:00
|
|
|
|
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);
|
2023-03-30 07:56:16 -04:00
|
|
|
if (ft_strnstr(str, "n", ft_strlen(str)) && str[0] == '-')
|
2023-03-13 11:51:56 -04:00
|
|
|
*check_backslash_n = 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-17 10:03:28 -05:00
|
|
|
{
|
2023-03-13 11:51:56 -04:00
|
|
|
int check_backslash_n;
|
|
|
|
int check_start_write;
|
2023-02-21 09:42:05 -05:00
|
|
|
int i;
|
2023-04-11 11:26:49 -04:00
|
|
|
int y;
|
2023-02-17 10:03:28 -05:00
|
|
|
|
2023-03-13 11:51:56 -04:00
|
|
|
check_backslash_n = 0;
|
|
|
|
check_start_write = 0;
|
2023-02-22 07:24:35 -05:00
|
|
|
i = -1;
|
|
|
|
while (strs[++i])
|
|
|
|
{
|
2023-04-11 11:26:49 -04:00
|
|
|
y = -1;
|
|
|
|
while (is_space(strs[i][++y]))
|
|
|
|
;
|
2023-03-28 09:55:08 -04:00
|
|
|
if (check_start_write == 1
|
|
|
|
|| check_argument(strs[i], &check_backslash_n))
|
2023-02-22 07:24:35 -05:00
|
|
|
{
|
2023-03-13 11:51:56 -04:00
|
|
|
check_start_write = 1;
|
2023-02-22 07:24:35 -05:00
|
|
|
ft_putstr_fd(strs[i], fd);
|
|
|
|
if (strs[i + 1] != NULL)
|
|
|
|
write(fd, " ", 1);
|
|
|
|
}
|
|
|
|
}
|
2023-03-13 11:51:56 -04:00
|
|
|
if (!check_backslash_n)
|
2023-02-17 10:03:28 -05:00
|
|
|
write(fd, "\n", 1);
|
2023-02-17 10:54:58 -05:00
|
|
|
return (0);
|
2023-02-17 10:03:28 -05:00
|
|
|
}
|