diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-09 17:53:40 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-09 17:53:40 +0200 |
commit | c8365681f146bc8ccf2c95f6ceb3cbeab3470f08 (patch) | |
tree | b2d420df4e9d96167fd13c2b2731db2e1706d0fc | |
parent | qwe (diff) | |
download | 42-minishell-c8365681f146bc8ccf2c95f6ceb3cbeab3470f08.tar.gz 42-minishell-c8365681f146bc8ccf2c95f6ceb3cbeab3470f08.tar.bz2 42-minishell-c8365681f146bc8ccf2c95f6ceb3cbeab3470f08.tar.xz 42-minishell-c8365681f146bc8ccf2c95f6ceb3cbeab3470f08.tar.zst 42-minishell-c8365681f146bc8ccf2c95f6ceb3cbeab3470f08.zip |
Pretty bav
-rw-r--r-- | src/p_split.c | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/src/p_split.c b/src/p_split.c index 482bc58..d39a4cb 100644 --- a/src/p_split.c +++ b/src/p_split.c @@ -154,7 +154,7 @@ typedef struct s_split { size_t pos[ARG_MAX / 2]; int8_t nextif[ARG_MAX / 2]; - int16_t count; + uint32_t count; } t_split; static void @@ -208,15 +208,51 @@ static t_split p_meet_splitter(ptr, line, &sp, mode); ptr++; } + sp.pos[sp.count] = ptr - line; + sp.nextif[sp.count] = 0; + sp.count += 1; return (sp); } +static void + *p_del_split(char *words[], size_t todel) +{ + size_t i; + + i = 0; + while (i < todel) + { + ft_memdel((void*)&words[i]); + } + ft_memdel((void*)&words); + return (NULL); +} + static char -**p_get_words(const char line[], const t_split sp) + **p_get_words(const char line[], const t_split sp) { char **words; + size_t i; + size_t oldpos; + int8_t oldif; - words = NULL; + if ((words = (char**)malloc((sp.count + 1) * sizeof(char*))) == NULL) + return (NULL); + oldpos = 0; + oldif = -1; + i = 0; + while (i < sp.count) + { + if ((words[i] = + (char*)malloc(((sp.pos[i] - oldpos) + 1) * sizeof(char))) == NULL) + return ((char**)p_del_split(words, i)); + ft_strlcpy(words[i], line + oldpos + ((oldif > 0) ? (2) : (oldif) + 1), sp.pos[i] - oldpos); + ft_printf("[%s]\n", words[i]); + oldpos = sp.pos[i]; + oldif = sp.nextif[i]; + i++; + } + words[i] = NULL; return (words); } |