This repository was archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_mem_utils.c
65 lines (58 loc) · 1.52 KB
/
ft_mem_utils.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
56
57
58
59
60
61
62
63
64
65
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_mem_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ctherin <ctherin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/20 17:23:57 by ctherin #+# #+# */
/* Updated: 2022/06/20 18:46:29 by ctherin ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void *ft_calloc(size_t nmemb, size_t size)
{
void *ptr;
size_t i;
i = 0;
if (nmemb > LONG_MAX || size > LONG_MAX)
return (NULL);
ptr = malloc(nmemb * size);
if (!ptr)
return (NULL);
while (i < nmemb * size)
{
((char *)ptr)[i] = 0;
i++;
}
return (ptr);
}
size_t ft_strlen(const char *s)
{
size_t i;
if (!s)
return (0);
i = 0;
while (s[i])
i++;
return (i);
}
char *ft_strdup(const char *s)
{
size_t sz;
size_t i;
char *dest;
sz = 0;
i = 0;
while (s[sz])
sz++;
dest = ft_calloc(sz + 1, sizeof(char));
if (!dest)
return (NULL);
while (i < sz)
{
dest[i] = s[i];
i++;
}
return (dest);
}