-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
89 lines (81 loc) · 1.98 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sde-silv <sde-silv@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/28 15:08:37 by sde-silv #+# #+# */
/* Updated: 2023/08/28 15:09:52 by sde-silv ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t gnl_ft_strlen(const char *s)
{
size_t len;
len = 0;
if (!s)
return (len);
while (s[len] != '\0')
len++;
return (len);
}
char *gnl_ft_strchr(const char *s, int c)
{
if (!s)
return (NULL);
while (*s != (char)c && *s != '\0')
s ++;
if (*s == (char)c)
return ((char *)s);
return (NULL);
}
char *gnl_ft_strjoin(char *s1, char *s2)
{
size_t len;
char *ptr;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
if (!s1)
return (NULL);
s1[0] = '\0';
}
len = gnl_ft_strlen(s1) + gnl_ft_strlen(s2);
ptr = malloc((len + 1) * sizeof(char));
if (!ptr)
{
free(s1);
return (NULL);
}
else
{
gnl_ft_strlcpy(ptr, s1, gnl_ft_strlen(s1) + 1);
gnl_ft_strlcpy(ptr + gnl_ft_strlen(s1), s2, gnl_ft_strlen(s2) + 1);
free(s1);
return (ptr);
}
}
size_t gnl_ft_strlcpy(char *dest, char const *src, size_t size)
{
size_t i;
i = 0;
if (!dest || !src)
return (i);
if (size == 0)
{
while (src[i] != '\0')
i ++;
return (i);
}
while (i < size - 1 && src[i] != '\0')
{
dest[i] = src[i];
i ++;
}
if (i < size)
dest [i] = '\0';
while (src[i] != '\0')
i ++;
return (i);
}