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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/*
ft_getline Copyright (C) 2026 yctct
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ft_getline.h"
char *ft_strchr(const char *s, int c)
{
size_t i;
size_t size;
size = 0;
while (s[size])
size++;
i = 0;
while (i <= size)
{
if (s[i] == (char)c)
return ((char *)&s[i]);
i++;
}
return (0);
}
char *ft_strjoin(char *s1, char *s2)
{
char *ptr;
int len_s1;
int len_s2;
len_s1 = 0;
while (s1[len_s1])
len_s1++;
len_s2 = 0;
while (s2[len_s2])
len_s2++;
ptr = malloc(sizeof(char) * (len_s1 + len_s2 + 1));
if (!ptr)
{
free(s1);
return (NULL);
}
ft_strlcpy(ptr, s1, (len_s1 + 1));
ft_strlcat(ptr, s2, (len_s1 + len_s2 + 1));
free(s1);
return (ptr);
}
size_t ft_strlcat(char *dest, const char *src, size_t size)
{
size_t dlen;
size_t slen;
dlen = 0;
while (dest[dlen])
dlen++;
slen = 0;
while (src[slen])
slen++;
if (dlen >= size)
return (size + slen);
if (slen < size - dlen)
ft_memcpy(dest + dlen, src, slen + 1);
else
{
ft_memcpy(dest + dlen, src, size - dlen - 1);
dest[size - 1] = '\0';
}
return (dlen + slen);
}
void *ft_memcpy(void *dest, const void *src, size_t n)
{
unsigned char *a;
const char *b;
if (n == 0)
return (dest);
if (!dest || !src)
return (dest);
a = dest;
b = src;
while (n > 0)
{
*a++ = *b++;
n--;
}
return (dest);
}
size_t ft_strlcpy(char *dest, const char *src, size_t size)
{
unsigned int i;
i = 0;
if (size > 0)
{
while (src[i] && i < (size - 1))
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
}
i = 0;
while (src[i])
i++;
return (i);
}
char *ft_strdup(const char *s)
{
char *ptr;
int len;
len = 0;
while (s[len])
len++;
ptr = (char *)malloc(len + 1);
if (!ptr)
return (NULL);
ft_strlcpy(ptr, s, len + 1);
return (ptr);
}
|