-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils_bonus.c
104 lines (93 loc) · 2.21 KB
/
get_next_line_utils_bonus.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
99
100
101
102
103
104
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: msilva-p <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/12 18:10:34 by msilva-p #+# #+# */
/* Updated: 2022/08/12 19:32:56 by msilva-p ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
size_t ft_strlen(const char *s)
{
int i;
i = 0;
if (!s)
return (i);
while (s[i])
i++;
return (i);
}
char *ft_strjoin(char const *s1, char const *s2)
{
int i;
int j;
char *news;
i = 0;
j = 0;
news = (char *)malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
if (!news)
return (NULL);
while (s1 && s1[i])
{
news[i] = s1[i];
i++;
}
while (s2 && s2[j])
{
news[i + j] = s2[j];
j++;
}
news[i + j] = '\0';
return (news);
}
char *ft_strdup(const char *s)
{
char *copy;
size_t i;
i = 0;
copy = (char *)malloc(sizeof (char) * ft_strlen(s) + 1);
if (!copy)
return (NULL);
while (s[i])
{
copy[i] = s[i];
i++;
}
copy[i] = '\0';
return (copy);
}
size_t ft_strlcpy(char *dst, const char *src, size_t size)
{
size_t i;
i = 0;
if (!size)
return (ft_strlen(src));
while (i + 1 < size && src[i])
{
dst[i] = src[i];
i++;
}
dst[i] = '\0';
return (ft_strlen(src));
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *subs;
size_t lenmax;
if (start > ft_strlen(s))
return (ft_strdup(""));
else if (len > ft_strlen(s) - start)
lenmax = ft_strlen(s) - start;
else
lenmax = len;
if (!s)
return (NULL);
subs = (char *)malloc(sizeof(char) * (lenmax + 1));
if (!subs)
return (NULL);
ft_strlcpy(subs, s + start, lenmax + 1);
return (subs);
}