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-02-21 09:42:05 -05:00
|
|
|
/* Updated: 2023/02/21 15:38:53 by erey-bet ### ########.fr */
|
2023-02-17 10:03:28 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2023-02-17 11:46:12 -05:00
|
|
|
#include "../minishell.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');
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-02-21 09:42:05 -05:00
|
|
|
|
|
|
|
char *conca(char **strings)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char *result;
|
|
|
|
char *p;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = -1;
|
|
|
|
while (strings[++i] != NULL)
|
|
|
|
len += strlen(strings[i]);
|
|
|
|
result = (char *) malloc(len * 2 + 1);
|
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
|
|
|
p = result;
|
|
|
|
i = 0;
|
|
|
|
while (strings[i] != NULL)
|
|
|
|
{
|
|
|
|
ft_strncpy(p, strings[i], ft_strlen(strings[i]));
|
|
|
|
ft_strncpy(p, " ", 1);
|
|
|
|
p += strlen(strings[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int echo(int fd, char **strs)
|
2023-02-17 10:03:28 -05:00
|
|
|
{
|
2023-02-21 09:42:05 -05:00
|
|
|
int check;
|
|
|
|
int i;
|
|
|
|
char *str;
|
2023-02-17 10:03:28 -05:00
|
|
|
|
2023-02-21 09:42:05 -05:00
|
|
|
str = conca(strs);
|
2023-02-17 10:03:28 -05:00
|
|
|
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);
|
2023-02-17 10:54:58 -05:00
|
|
|
return (0);
|
2023-02-17 10:03:28 -05:00
|
|
|
}
|