-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
92 lines (81 loc) · 2.18 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hhamza <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/19 13:27:06 by hhamza #+# #+# */
/* Updated: 2021/12/19 17:21:50 by hhamza ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen(const char *str)
{
size_t i;
i = 0;
while (str[i] != '\0')
++i;
return (i);
}
char *ft_strdup(const char *str)
{
char *result;
size_t strlen;
strlen = ft_strlen(str) + 1;
result = (char *) malloc(sizeof(char) * strlen);
if (result == NULL)
return (NULL);
ft_strlcpy(result, str, strlen);
return (result);
}
int ft_indexof(const char *str, char c)
{
unsigned int i;
if (str == NULL)
return (-1);
i = 0;
while (str[i] != '\0')
{
if (str[i] == c)
return (i);
++i;
}
return (-1);
}
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize)
{
unsigned int i;
if (dstsize > 0)
{
i = 0;
while (i < dstsize - 1 && src[i] != '\0')
{
dst[i] = src[i];
++i;
}
dst[i] = '\0';
}
return (ft_strlen(src));
}
char *ft_strjoin(const char *s1, const char *s2)
{
char *result;
size_t s1_len;
size_t s2_len;
if (s1 == NULL && s2 == NULL)
return (NULL);
if (s1 == NULL && s2 != NULL)
return (ft_strdup(s2));
else if (s1 != NULL && s2 == NULL)
return (ft_strdup(s1));
s1_len = ft_strlen(s1);
s2_len = ft_strlen(s2);
result = (char *) malloc(sizeof(char) * (s1_len + s2_len + 1));
if (result == NULL)
return (NULL);
result[0] = '\0';
ft_strlcpy(result, s1, s1_len + 1);
ft_strlcpy(result + s1_len, s2, s2_len + 1);
return (result);
}