forked from AingeruAlvarezSanchez/Libft
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_doublestrdup.c
48 lines (45 loc) · 1.59 KB
/
ft_doublestrdup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_doublestrdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aalvarez <aalvarez@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/21 18:21:54 by aalvarez #+# #+# */
/* Updated: 2022/08/21 18:27:50 by aalvarez ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
#include <errno.h>
/**
* @brief attempts to duplicate the double pointer pointed by str to a new
* allocated double pointer.
*
* @param str the double pointer to be duplicated.
* @return char** the allocated new double pointer or NULL if the allocation
* failed or str was nonexisting.
*/
char **ft_doublestrdup(const char **str)
{
char **result;
int i;
if (!str)
return (NULL);
result = (char **)malloc(sizeof(char *) * (ft_doublestrlen(str) + 1));
if (!result)
return (NULL);
i = -1;
while (str[++i])
{
result[i] = ft_strdup(str[i]);
if (result[i] == NULL)
{
ft_doublefree(result);
errno = ENOMEM;
return (NULL);
}
}
result[i] = 0;
return (result);
}