-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
98 lines (88 loc) · 1.89 KB
/
get_next_line_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: azaher <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/05 10:15:53 by azaher #+# #+# */
/* Updated: 2022/11/11 12:10:10 by azaher ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen(const char *s)
{
size_t l;
l = 0;
while (*s != '\0')
{
l++;
s++;
}
return (l);
}
void *ft_calloc(size_t count, size_t size)
{
void *p;
size_t i;
if (size != 0 && count > SIZE_MAX / size)
return (0);
p = malloc(count * size);
i = 0;
if (!p)
return (0);
if (p)
{
while (i < count)
{
*((char *)p + i) = 0;
i++;
}
}
return (p);
}
char *ft_strchr(const char *str, int c)
{
size_t i;
i = 0;
while (i <= ft_strlen(str))
{
if (str[i] == (char)c)
return ((char *)&str[i]);
i++;
}
return (0);
}
char *ft_strjoin(char *s1, char *s2)
{
char *dest;
size_t i;
size_t j;
size_t s1len;
if (s1)
s1len = ft_strlen(s1);
else
s1len = 0;
i = 0;
j = 0;
dest = ft_calloc((s1len + ft_strlen(s2) + 1), sizeof(char));
if (!dest)
return (0);
while (i < s1len)
{
dest[i] = s1[i];
i++;
}
while (j < ft_strlen(s2))
dest[i++] = *(s2 + j++);
free_buff(&s1);
return (dest);
}
void free_buff(char **p)
{
if (p && *p)
{
free(*p);
*p = NULL;
}
}