diff --git a/builtins/echo.c b/builtins/echo.c index 683b8a1..1ef2f22 100644 --- a/builtins/echo.c +++ b/builtins/echo.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/17 13:09:08 by erey-bet #+# #+# */ -/* Updated: 2023/02/21 16:02:09 by erey-bet ### ########.fr */ +/* Updated: 2023/02/21 22:09:55 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,27 +46,28 @@ int check_argument(char *str, int *check, int i) } -char *conca(char **strings) +char *conca(char **strs) { int len; - char *result; + char *str; 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) + len = 0; + while (strs[++i] != NULL) + len += strlen(strs[i]) + 2; + str = (char *) ft_calloc(len + 1, sizeof(char)); + if (str == NULL) return NULL; - i = 0; - while (strings[i] != NULL) + i = -1; + while (strs[++i] != NULL) { - ft_strncpy(result + ft_strlen(result), strings[i], ft_strlen(strings[i])); - ft_strncpy(result + ft_strlen(result), " ", 1); - i++; + ft_strlcat(str, strs[i], ft_strlen(str) + ft_strlen(strs[i]) + 2); + str[ft_strlen(str)] = ' '; } - return (result); + str[ft_strlen(str)] = '\0'; + return (str); } @@ -86,11 +87,12 @@ int echo(int fd, char **strs) ft_putchar_fd(str[i++], fd); if (!check) write(fd, "\n", 1); + free(str); return (0); } -int main(int argc, char *argv[]) +/*int main(int argc, char *argv[]) { echo(1, argv); return (0); -} +}*/