-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_alloc.c
More file actions
73 lines (64 loc) · 1.67 KB
/
ft_alloc.c
File metadata and controls
73 lines (64 loc) · 1.67 KB
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
66
67
68
69
70
71
72
73
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_alloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kacherch <kacherch@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/03 16:46:58 by kacherch #+# #+# */
/* Updated: 2025/11/05 09:18:24 by kacherch ### ########.fr */
/* */
/* ************************************************************************** */
#include <limits.h>
#include <stdlib.h>
static size_t ft_strlen(const char *s)
{
int i;
i = 0;
while (s[i])
i++;
return (i);
}
static void *ft_memset(void *s, int c, size_t n)
{
unsigned char *ptr;
ptr = (unsigned char *)s;
while (n)
{
*ptr = (unsigned char)c;
ptr++;
n--;
}
return (s);
}
char *ft_strdup(const char *s)
{
size_t i;
size_t len;
char *str;
len = ft_strlen(s);
str = malloc(sizeof(char) * (len + 1));
if (!str)
return (NULL);
i = 0;
while (i < len)
{
str[i] = s[i];
i++;
}
str[i] = '\0';
return (str);
}
void *ft_calloc(size_t nmemb, size_t size)
{
void *ptr;
size_t nb_bytes;
if (nmemb != 0 && size > INT_MAX / nmemb)
return (NULL);
nb_bytes = nmemb * size;
ptr = malloc(nb_bytes);
if (!ptr)
return (0);
ft_memset(ptr, 0, nb_bytes);
return (ptr);
}