42_minishell/builtins/echo.c

108 lines
2.5 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-02-21 16:13:08 -05:00
<<<<<<< HEAD
2023-02-21 16:26:45 -05:00
/* Updated: 2023/02/21 22:24:51 by erey-bet ### ########.fr */
2023-02-21 17:28:37 -05:00
=======
<<<<<<< HEAD
2023-02-21 16:12:23 -05:00
/* Updated: 2023/02/21 21:45:38 by cchauvet ### ########.fr */
2023-02-21 16:13:08 -05:00
=======
2023-02-21 16:10:40 -05:00
/* Updated: 2023/02/21 22:09:55 by erey-bet ### ########.fr */
2023-02-21 16:13:08 -05:00
>>>>>>> 1fdc51a668e616de94c33d5b3b1e9cb51182916d
2023-02-21 17:28:37 -05:00
>>>>>>> eb4a8ed663b2f93cd955f7608aa686bd8e767a65
/* */
/* ************************************************************************** */
2023-02-17 11:46:12 -05:00
#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);
}
2023-02-21 09:42:05 -05:00
2023-02-21 16:10:40 -05:00
char *conca(char **strs)
2023-02-21 09:42:05 -05:00
{
int len;
2023-02-21 16:10:40 -05:00
char *str;
2023-02-21 09:42:05 -05:00
char *p;
int i;
i = -1;
2023-02-21 16:10:40 -05:00
len = 0;
while (strs[++i] != NULL)
len += strlen(strs[i]) + 2;
str = (char *) ft_calloc(len + 1, sizeof(char));
if (str == NULL)
2023-02-21 09:42:05 -05:00
return NULL;
2023-02-21 16:10:40 -05:00
i = -1;
while (strs[++i] != NULL)
2023-02-21 09:42:05 -05:00
{
2023-02-21 16:10:40 -05:00
ft_strlcat(str, strs[i], ft_strlen(str) + ft_strlen(strs[i]) + 2);
2023-02-21 16:26:45 -05:00
if (strs[i + 1] != NULL)
str[ft_strlen(str)] = ' ';
2023-02-21 09:42:05 -05:00
}
2023-02-21 16:10:40 -05:00
str[ft_strlen(str)] = '\0';
return (str);
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;
char *str;
2023-02-21 09:42:05 -05:00
str = conca(strs);
check = 0;
i = 0;
while (is_space(str[i]))
i++;
i = check_argument(str, &check, i);
while (str[i])
2023-02-21 10:03:08 -05:00
ft_putchar_fd(str[i++], fd);
if (!check)
write(fd, "\n", 1);
2023-02-21 16:10:40 -05:00
free(str);
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
{
echo(1, argv);
return (0);
2023-02-21 16:10:40 -05:00
}*/