-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
121 lines (110 loc) · 2.63 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
111
112
113
114
115
116
117
118
119
120
121
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: minkim3 <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/03 21:02:39 by minjukim #+# #+# */
/* Updated: 2023/02/05 15:56:39 by minkim3 ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
ssize_t find_char(const char *s, int c)
{
char *dummy;
ssize_t index;
dummy = (char *)s;
index = 0;
while (*(dummy + index))
{
if (((char)*(dummy + index)) == (char)c)
return (index);
index++;
}
if ((char)c == '\0')
return (index);
return (-1);
}
size_t ft_strlen(const char *s)
{
size_t count;
count = 0;
while (s[count])
count++;
return (count);
}
char *append_buffer(char const *s1, char const *s2, size_t len)
{
char *arr;
size_t len_s1;
if (s1 == NULL && s2 != NULL)
{
arr = malloc(sizeof(char) * (len + 1));
if (!arr)
return (NULL);
ft_strlcpy(arr, s2, len + 1);
return (arr);
}
else if (s2 == NULL)
return (NULL);
len_s1 = ft_strlen(s1);
arr = (char *)malloc(sizeof(char) * (len_s1 + len + 1));
if (!arr)
return (NULL);
ft_strlcpy(arr, s1, len_s1 + 1);
ft_strlcat(arr, s2, len_s1 + len + 1);
free((void *)s1);
return (arr);
}
size_t ft_strlcat(char *dst, const char *src, size_t dstsize)
{
unsigned int dst_len;
unsigned int src_len;
unsigned int cat_size;
dst_len = ft_strlen(dst);
src_len = ft_strlen((char *)src);
cat_size = dstsize - dst_len - 1;
if (dstsize <= dst_len)
return (src_len + dstsize);
while (*dst)
dst++;
if (dstsize > dst_len)
{
while ((cat_size) && *src != '\0')
{
*dst = *src;
dst++;
src++;
cat_size--;
}
*dst = '\0';
}
return (dst_len + src_len);
}
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize)
{
size_t src_len;
size_t index;
src_len = ft_strlen((char *)src);
index = 0;
if (dstsize <= src_len)
{
while (index + 1 < dstsize)
{
dst[index] = src[index];
index++;
}
}
else
{
while (src[index])
{
dst[index] = src[index];
index++;
}
}
if (dstsize != 0)
dst[index] = 0;
return (src_len);
}