42_minishell/builtins/echo.c
Etienne Rey-bethbeder a136a66522 echo.c
2023-02-21 16:03:08 +01:00

97 lines
2.1 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* echo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 13:09:08 by erey-bet #+# #+# */
/* Updated: 2023/02/21 16:02:09 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)
{
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);
}
char *conca(char **strings)
{
int len;
char *result;
char *p;
int i;
i = -1;
while (strings[++i] != NULL)
len += strlen(strings[i]);
result = (char *) ft_calloc(len * 2 + 1, sizeof(char));
if (result == NULL)
return NULL;
i = 0;
while (strings[i] != NULL)
{
ft_strncpy(result + ft_strlen(result), strings[i], ft_strlen(strings[i]));
ft_strncpy(result + ft_strlen(result), " ", 1);
i++;
}
return (result);
}
int echo(int fd, char **strs)
{
int check;
int i;
char *str;
str = conca(strs);
check = 0;
i = 0;
while (is_space(str[i]))
i++;
i = check_argument(str, &check, i);
while (str[i])
ft_putchar_fd(str[i++], fd);
if (!check)
write(fd, "\n", 1);
return (0);
}
int main(int argc, char *argv[])
{
echo(1, argv);
return (0);
}