Finito Echo
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/17 13:09:08 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/02/21 23:31:04 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/22 13:23:16 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -18,80 +18,44 @@ int is_space(char c)
|
||||
|| c == '\r' || c == '\n');
|
||||
}
|
||||
|
||||
int check_argument(char *str, int *check, int i)
|
||||
int check_argument(char *str, int *check)
|
||||
{
|
||||
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 **strs)
|
||||
{
|
||||
int len;
|
||||
char *str;
|
||||
char *p;
|
||||
int i;
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
len = 0;
|
||||
while (strs[++i] != NULL)
|
||||
len += ft_strlen(strs[i]) + 2;
|
||||
str = (char *) ft_calloc(len + 1, sizeof(char));
|
||||
if (str == NULL)
|
||||
return NULL;
|
||||
i = -1;
|
||||
while (strs[++i] != NULL)
|
||||
{
|
||||
ft_strlcat(str, strs[i], ft_strlen(str) + ft_strlen(strs[i]) + 2);
|
||||
if (strs[i + 1] != NULL)
|
||||
str[ft_strlen(str)] = ' ';
|
||||
}
|
||||
str[ft_strlen(str)] = '\0';
|
||||
return (str);
|
||||
while (str[++i])
|
||||
if (str[i] != '-' && str[i] != 'n')
|
||||
return (1);
|
||||
if (ft_strnstr(str, "n", ft_strlen(str)))
|
||||
*check = 1;
|
||||
return (0);
|
||||
}
|
||||
|
||||
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);
|
||||
i = -1;
|
||||
while (strs[++i])
|
||||
{
|
||||
while (is_space(*strs[i]))
|
||||
strs[i]++;
|
||||
if (check == 1 || check_argument(strs[i], &check))
|
||||
{
|
||||
ft_putstr_fd(strs[i], fd);
|
||||
if (strs[i + 1] != NULL)
|
||||
write(fd, " ", 1);
|
||||
}
|
||||
}
|
||||
if (!check)
|
||||
write(fd, "\n", 1);
|
||||
free(str);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*int main(int argc, char *argv[])
|
||||
{
|
||||
echo(1, argv);
|
||||
echo(1, argv + 1);
|
||||
return (0);
|
||||
}*/
|
||||
|
Reference in New Issue
Block a user