aboutsummaryrefslogtreecommitdiff
path: root/ft_split.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-10-14 12:54:57 +0200
committerCharles <sircharlesaze@gmail.com>2019-10-15 07:58:38 +0200
commitf23592ea0c122a0da2723d4253c47ca227529f79 (patch)
tree6aeb7318fd7a13b1aef209e408fc0f867c7647cf /ft_split.c
parentbbdc932411501e239757985023942f93d1b21f46 (diff)
downloadlibft-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.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/ft_split.c b/ft_split.c
index e3b312c..74703fb 100644
--- a/ft_split.c
+++ b/ft_split.c
@@ -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);
}