-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
110 lines (100 loc) · 2.22 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
99
100
101
102
103
104
105
106
107
108
109
110
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nidionis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/04 16:20:59 by nidionis #+# #+# */
/* Updated: 2024/11/03 20:15:16 by nidionis ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen(const char *str)
{
int i;
if (!str)
return (0);
i = 0;
while (str[i] != '\0')
i++;
return (i);
}
char *ft_strjoin(char *s1, char *s2)
{
size_t len_s1;
unsigned int i;
char *ret;
if (!s1)
return (NULL);
i = 0;
len_s1 = ft_strlen(s1);
ret = malloc(sizeof(char) * (ft_strlen(s2) + len_s1 + 1));
if (!ret)
return (NULL);
i = 0;
if (s1)
while (i < len_s1)
ret[i++] = *s1++;
if (s2)
{
while (s2[i - len_s1])
{
ret[i] = s2[i - len_s1];
i++;
}
}
ret[i] = '\0';
return (ret);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *substr;
char *return_val;
if (!s)
return (NULL);
substr = (char *) malloc(sizeof(char) * (len + 1));
if (!substr)
return (NULL);
while (start--)
{
if (!*s++)
{
substr[0] = '\0';
return (substr);
}
}
return_val = substr;
while (len-- && *s)
*substr++ = *s++;
*substr = '\0';
return (return_val);
}
char *ft_strchr(const char *str, int c)
{
if (str)
{
while (*str != (char)c)
{
if (!*str)
return (NULL);
str++;
}
}
else
return (NULL);
return ((char *)str);
}
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize)
{
size_t src_len;
src_len = ft_strlen(src);
while (*src && dstsize > 1)
{
*(dst++) = *(src++);
dstsize--;
}
if (dstsize)
*dst = '\0';
return (src_len);
}