-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strdup.c
36 lines (34 loc) · 1.75 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lmartin2 <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/12 17:05:52 by lmartin2 #+# #+# */
/* Updated: 2022/02/25 18:44:33 by lmartin2 ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
// This code defines a function ft_strdup that takes in a constant character
// pointer s1 and returns a newly allocated duplicate of the input string.
char *ft_strdup(const char *s1)
{
char *str;
size_t len;
// The function starts by calculating the length of the input string s1
// using the ft_strlen function.
len = ft_strlen(s1);
// Memory is then allocated for the new string str using the ft_calloc
// function. The allocated memory size is (len + 1) to include space for
// the null-terminator.
str = (char *)ft_calloc(sizeof(char), (len + 1));
// Next, the code checks if memory allocation was successful. If not, it
// returns NULL.
if (!str)
return (NULL);
// The contents of s1 are then copied to str using the ft_memcpy function.
ft_memcpy(str, s1, len);
// Finally, the function returns the newly allocated and copied string str.
return (str);
}