-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strdup.c
55 lines (51 loc) · 1.68 KB
/
ft_strdup.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
49
50
51
52
53
54
55
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbutt <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/23 12:52:49 by mbutt #+# #+# */
/* Updated: 2019/02/23 21:26:57 by mbutt ### ########.fr */
/* */
/* ************************************************************************** */
/*
** The strdup() function allocates sufficient memory for a copy of the string
** s1, does the copy, and returns a pointer to it. The pointer may subsequently
** be used as an argument to the function free(3).
** If insufficient memory is available, NULL is returned and errno is set to
** ENOMEM.
*/
#include "libft.h"
char *ft_strdup(const char *s1)
{
char *string2;
int i;
i = 0;
string2 = (char*)malloc(1 + ft_strlen(s1));
if (!string2)
{
return (NULL);
}
while (s1[i])
{
string2[i] = s1[i];
i++;
}
string2[i] = '\0';
return (string2);
}
/*
** int main (void)
** {
** const char *string1 = "Test";
** char *string3;
** char *string4;
** string3 = strdup(string1);
** string4 = ft_strdup(string1);
** printf(" strdup: %s\n", string3);
** printf("ft_strdup: %s\n", string4);
**
** return(0);
**}
*/