-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils_bonus.c
101 lines (90 loc) · 2.11 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
100
101
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pablomar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/02 14:10:11 by pablomar #+# #+# */
/* Updated: 2019/12/05 19:38:35 by pablomar ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
size_t ft_strlen(char const *str)
{
size_t i;
i = 0;
while (str[i] != '\0')
{
i++;
}
return (i);
}
char *ft_strdup(const char *s)
{
int i;
int size;
char *str;
i = 0;
size = ft_strlen(s);
str = (char*)malloc(sizeof(*str) * size + 1);
if (str == NULL)
return (NULL);
while (i < size)
{
str[i] = s[i];
i++;
}
str[size] = '\0';
return (str);
}
char *ft_strjoin(char const *s1, char const *s2)
{
char *str;
int i;
i = 0;
if (s1 && s2)
{
str = (char*)malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (str == NULL)
return (NULL);
while (*s1)
str[i++] = *s1++;
while (*s2)
str[i++] = *s2++;
str[i] = '\0';
return (str);
}
return (0);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
unsigned int i;
i = 0;
if (s)
{
if (!(str = malloc(((int)len + 1) * sizeof(char))))
return (NULL);
ft_bzero(str, len);
if (start <= ft_strlen(s))
{
while (i < len && s[i])
{
str[i] = s[i + start];
i++;
}
}
if (i < len + 1)
str[i] = '\0';
return (str);
}
return (0);
}
void ft_bzero(void *s, size_t n)
{
unsigned char *ptr;
ptr = (unsigned char*)s;
while (n-- > 0)
*(ptr++) = 0;
}