-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils_bonus.c
97 lines (87 loc) · 2.22 KB
/
get_next_line_utils_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lcosta-g <lcosta-g@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/11 11:58:10 by lcosta-g #+# #+# */
/* Updated: 2024/11/15 16:03:56 by lcosta-g ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.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 && *s != (unsigned char)c)
s++;
if (*s == (unsigned char)c)
return ((char *)s);
return (NULL);
}
void *ft_calloc(size_t nmemb, size_t size)
{
size_t m_size;
size_t i;
void *temp;
if (!nmemb || !size)
return (malloc(0));
m_size = nmemb * size;
if (m_size / size != nmemb)
return (NULL);
temp = malloc(m_size);
if (!temp)
return (NULL);
i = 0;
while (i < m_size)
((unsigned char *)temp)[i++] = 0;
return (temp);
}
char *ft_strjoin(char const *s1, char const *s2)
{
char *temp;
size_t i;
size_t j;
temp = (char *)malloc(ft_strlen(s1) + ft_strlen(s2) + 1);
if (!temp)
return (NULL);
i = 0;
while (s1[i])
{
temp[i] = s1[i];
i++;
}
j = 0;
while (s2[j])
temp[i++] = s2[j++];
temp[i] = '\0';
return (temp);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
unsigned int len_s;
char *substr;
unsigned int i;
len_s = ft_strlen(s);
if (!s)
return (NULL);
if (start >= len_s)
return ((char *)ft_calloc(1, sizeof(char)));
if ((len_s - start) < len)
len = len_s - start;
substr = (char *)malloc(len + 1);
if (!substr)
return (NULL);
i = 0;
while (i < len)
substr[i++] = s[start++];
substr[i] = '\0';
return (substr);
}