mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
wordsplit: handle multiple words
This commit is contained in:
parent
00fd2380cf
commit
4171a3d07b
4 changed files with 68 additions and 17 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/13 17:07:01 by khais #+# #+# */
|
||||
/* Updated: 2025/02/14 13:56:45 by khais ### ########.fr */
|
||||
/* Updated: 2025/02/14 16:47:04 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -37,10 +37,15 @@ t_wordlist *wordlist_create(t_worddesc *word)
|
|||
*/
|
||||
void wordlist_destroy(t_wordlist *wordlist)
|
||||
{
|
||||
if (wordlist == NULL)
|
||||
return ;
|
||||
worddesc_destroy(wordlist->word);
|
||||
free(wordlist);
|
||||
t_wordlist *prev;
|
||||
|
||||
while (wordlist != NULL)
|
||||
{
|
||||
worddesc_destroy(wordlist->word);
|
||||
prev = wordlist;
|
||||
wordlist = wordlist->next;
|
||||
free(prev);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -53,8 +58,36 @@ t_worddesc *wordlist_get(t_wordlist *wordlist, int idx)
|
|||
if (wordlist == NULL || idx < 0)
|
||||
return (NULL);
|
||||
while (idx > 0 && wordlist != NULL)
|
||||
{
|
||||
wordlist = wordlist->next;
|
||||
idx--;
|
||||
}
|
||||
if (wordlist == NULL)
|
||||
return (NULL);
|
||||
return (wordlist->word);
|
||||
}
|
||||
|
||||
/*
|
||||
** push the given worddesc to the end of the given wordlist.
|
||||
**
|
||||
** if wordlist is null, create a new wordlist.
|
||||
**
|
||||
** returns a pointer to the first element in the wordlist.
|
||||
**
|
||||
** if malloc failure is encountered, all memory for this wordlist is freed, and
|
||||
** null is returned.
|
||||
*/
|
||||
t_wordlist *wordlist_push(t_wordlist *wordlist, t_worddesc *worddesc)
|
||||
{
|
||||
t_wordlist *start;
|
||||
|
||||
if (wordlist == NULL)
|
||||
return (wordlist_create(worddesc));
|
||||
start = wordlist;
|
||||
while (wordlist->next != NULL)
|
||||
wordlist = wordlist->next;
|
||||
wordlist->next = wordlist_create(worddesc);
|
||||
if (wordlist->next == NULL)
|
||||
return (wordlist_destroy(start), NULL);
|
||||
return (start);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue