diff options
Diffstat (limited to 'src/p_args.c')
-rw-r--r-- | src/p_args.c | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/src/p_args.c b/src/p_args.c index 6a15cb8..572b965 100644 --- a/src/p_args.c +++ b/src/p_args.c @@ -15,6 +15,7 @@ #include <stdlib.h> #include "d_define.h" +#include "p_args.h" #include "p_args_next.h" /* ================= */ @@ -60,12 +61,13 @@ static char } static uint16_t - p_count_args(char *ptr, uint16_t argc) + p_count_args(const char *head, char *ptr, uint16_t argc, size_t start[]) { if (*ptr == C_NULL) return (argc); while (*ptr != C_NULL && ft_iswhitespace(*ptr) == TRUE) ptr++; + start[argc] = ptr - head; if (*ptr != C_SQUOTE && *ptr != C_DQUOTE && *ptr != C_NULL) { while (*ptr != C_NULL && ft_iswhitespace(*ptr) == FALSE) @@ -74,7 +76,7 @@ static uint16_t if ((*ptr == C_SQUOTE || *ptr == C_DQUOTE) && *(ptr - 1) != '\\') { ptr++; - return (p_count_args(ptr, argc)); + return (p_count_args(head, ptr, argc, start)); } } } @@ -82,42 +84,44 @@ static uint16_t { ptr = p_skip_delim(ptr, *ptr); if (ft_iswhitespace(*ptr) == FALSE) - return (p_count_args(ptr, argc)); + return (p_count_args(head, ptr, argc, start)); } else if (*ptr == C_NULL) return (argc); - return (p_count_args(ptr, argc + 1)); + return (p_count_args(head, ptr, argc + 1, start)); /* TODO: quotes parse error */ } static void - p_del_alloced_words(char *words[], uint16_t to_del, uint16_t i) + p_del_alloced_words(char *words[], uint16_t to_del) { - while (i > to_del) + uint16_t i; + + i = 0; + while (i < to_del) { ft_memdel((void*)&words[i]); - i--; + i++; } } static char **p_split_words_no_rdr(const char word[]) { - char **words; - char *ptr; - uint16_t argc; - uint16_t to_del; + char **words; + char *ptr; + size_t start[512]; + uint16_t argc; + uint16_t to_del; ptr = (char*)word; - argc = p_count_args(ptr, 0); - ft_printf("%hu\n", argc); - exit(0); + argc = p_count_args(word, ptr, 0, start); if ((words = (char**)malloc((argc + 1) * sizeof(char*))) == NULL) return (NULL); words[argc] = NULL; - if ((to_del = p_dup_words(words, word, argc)) != 0) + if ((to_del = p_dup_words(words, word, argc, start)) != argc) { - p_del_alloced_words(words, to_del, argc); + p_del_alloced_words(words, to_del); return (NULL); } return (words); @@ -135,6 +139,7 @@ char if ((words = p_split_words_no_rdr(word)) == NULL) return (NULL); p_print(words); + exit(0); return (words); } p_print(words); |