-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
81 lines (72 loc) · 1.72 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: agras <agras@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/02 19:08:18 by agras #+# #+# */
/* Updated: 2021/12/29 20:31:48 by agras ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
int ft_strlen(char *str)
{
int i;
i = 0;
if (!str)
return (0);
while (str[i])
i++;
return (i);
}
char *ft_strndup(char *str, int n)
{
char *dup;
int i;
i = 0;
if (!str)
return (NULL);
dup = malloc(sizeof(char) * (n + 1));
if (!dup)
return (NULL);
while (str[i] && i < n)
{
dup[i] = str[i];
i++;
}
dup[i] = '\0';
return (dup);
}
char *ft_strcpy_to_end(char *dest, char *src)
{
int i;
i = 0;
while (src[i])
{
dest[i] = src[i];
i++;
}
return (&(dest[i]));
}
char *ft_strjoin(char *s1, char *s2)
{
char *dup;
char *dup_start;
dup = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
if (!dup)
return (NULL);
dup_start = dup;
if (s1)
{
dup = ft_strcpy_to_end(dup, s1);
free(s1);
}
if (s2)
{
dup = ft_strcpy_to_end(dup, s2);
free(s2);
}
*dup = '\0';
return (dup_start);
}