-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils_bonus.c
99 lines (88 loc) · 2.21 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
98
99
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mavinici <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/05 18:11:11 by mavinici #+# #+# */
/* Updated: 2021/06/05 18:11:11 by mavinici ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
size_t ft_strlen(const char *s)
{
size_t len;
len = 0;
while ((unsigned char)s[len])
len++;
return (len);
}
char *ft_strjoin(char const *s1, char const *s2)
{
size_t total;
size_t i;
char *newstr;
if (!s1 || !s2)
return (NULL);
i = 0;
total = ft_strlen(s1) + ft_strlen(s2);
newstr = (char *)malloc(sizeof(char) * (total + 1));
if (!newstr)
return (NULL);
while (*s1)
newstr[i++] = *s1++;
while (*s2)
newstr[i++] = *s2++;
newstr[i] = '\0';
return (newstr);
}
char *ft_strdup(const char *s)
{
char *new_str;
size_t len;
size_t i;
len = ft_strlen(s);
new_str = (char *)malloc(len + 1);
if (new_str == NULL)
return (NULL);
i = 0;
while (i < len)
{
new_str[i] = s[i];
i++;
}
new_str[len] = '\0';
return (new_str);
}
char *ft_strchr(const char *s, int c)
{
unsigned char *s_s;
unsigned char s_c;
s_s = (unsigned char *)s;
s_c = (unsigned char)c;
if (*s_s == s_c)
return ((char *)s);
while (*s_s++)
{
if (*s_s == s_c)
return ((char *)s_s);
}
return (NULL);
}
char *ft_line(char *s, size_t len)
{
char *line;
size_t i;
line = malloc(len + 1);
if (!line)
return (NULL);
i = 0;
while (i < len)
{
line[i] = s[i];
i++;
}
line[i] = '\0';
return (line);
}