-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
107 lines (97 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dalbano <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 14:57:41 by dalbano #+# #+# */
/* Updated: 2024/10/23 18:17:52 by dalbano ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen(const char *s)
{
size_t len;
len = 0;
while (s[len])
len++;
return (len);
}
char *ft_strchr(const char *s, int c)
{
while (*s)
{
if (*s == (char)c)
return ((char *)s);
s++;
}
if (c == '\0')
return ((char *)s);
return (NULL);
}
char *ft_strdup(const char *s1)
{
size_t len;
size_t temp;
char *copy;
if (!s1)
return (NULL);
temp = 0;
len = ft_strlen(s1);
copy = (char *)malloc(len + 1);
if (!copy)
return (NULL);
while (temp < len)
{
copy[temp] = s1[temp];
temp++;
}
copy[len] = '\0';
return (copy);
}
char *ft_strjoin(char *s1, const char *s2)
{
size_t len1;
size_t len2;
size_t temp1;
size_t temp2;
char *joined;
if (!s1 || !s2)
return (NULL);
len1 = ft_strlen(s1);
len2 = ft_strlen(s2);
temp1 = -1;
temp2 = -1;
joined = (char *)malloc(len1 + len2 + 1);
if (!joined)
return (NULL);
while (++temp1 < len1)
joined[temp1] = s1[temp1];
while (++temp2 < len2)
joined[len1 + temp2] = s2[temp2];
joined[len1 + len2] = '\0';
return (joined);
}
char *ft_strndup(const char *s, size_t n)
{
size_t temp;
size_t len;
char *copy;
if (!s || !n)
return (NULL);
len = ft_strlen(s);
if (n > len)
n = len;
copy = (char *)malloc(n + 1);
if (!copy)
return (NULL);
temp = 0;
while (temp < n)
{
copy[temp] = s[temp];
temp++;
}
copy[temp] = '\0';
return (copy);
}