diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-10-14 12:54:57 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-10-15 07:58:38 +0200 |
| commit | f23592ea0c122a0da2723d4253c47ca227529f79 (patch) | |
| tree | 6aeb7318fd7a13b1aef209e408fc0f867c7647cf /ft_split.c | |
| parent | bbdc932411501e239757985023942f93d1b21f46 (diff) | |
| download | libft-f23592ea0c122a0da2723d4253c47ca227529f79.tar.gz libft-f23592ea0c122a0da2723d4253c47ca227529f79.tar.bz2 libft-f23592ea0c122a0da2723d4253c47ca227529f79.zip | |
Fixed ft_split to destroy previous alloc if ENOMEM
- Normed libf.h
- Removed ugly conditions in ft_strnstr
Diffstat (limited to 'ft_split.c')
| -rw-r--r-- | ft_split.c | 18 |
1 files changed, 15 insertions, 3 deletions
@@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:22:09 by cacharle #+# #+# */ -/* Updated: 2019/10/07 14:30:10 by cacharle ### ########.fr */ +/* Updated: 2019/10/14 12:51:34 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,6 +48,17 @@ static char *ft_strndup(const char *s1, size_t n) return (clone); } +static void *destroy_strs(char **strs) +{ + int i; + + i = 0; + while (strs[i] != NULL) + free(strs[i++]); + free(strs); + return (NULL); +} + char **ft_split(char const *s, char c) { char **strs; @@ -69,9 +80,10 @@ char **ft_split(char const *s, char c) i = 0; while (s[i] && s[i] != c) i++; - strs[j++] = ft_strndup(s, i); + if ((strs[j++] = ft_strndup(s, i)) == NULL) + return (destroy_strs(strs)); s += i; } - strs[j] = 0; + strs[j] = NULL; return (strs); } |
