-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils_bonus.c
80 lines (70 loc) · 1.85 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mnouchet <mnouchet> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/18 17:23:28 by mnouchet #+# #+# */
/* Updated: 2022/12/07 02:36:06 by mnouchet ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static size_t ft_strlen(const char *str)
{
const char *s;
if (!str)
return (0);
s = str;
while (*s)
s++;
return (s - str);
}
void strreplace(char **a, char *b)
{
free(*a);
*a = b;
}
char *strnjoin(char *s1, char *s2, size_t n)
{
size_t s1_len;
char *output;
size_t i;
s1_len = ft_strlen(s1);
output = malloc(s1_len + n + 1);
if (!output)
return (NULL);
i = 0;
if (s1)
while (*s1)
output[i++] = *(s1++);
while (*s2 && i - s1_len < n)
output[i++] = *(s2++);
output[i] = '\0';
return (output);
}
static void *ft_memset(void *s, int c, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
*((unsigned char *)s + i) = (unsigned char)c;
i++;
}
return (s);
}
void *ft_calloc(size_t nmemb, size_t size)
{
size_t bytes;
void *ptr;
if (nmemb == 0 || size == 0)
return (NULL);
bytes = nmemb * size;
if (bytes / size != nmemb)
return (NULL);
ptr = malloc(nmemb * size);
if (ptr)
ft_memset(ptr, 0, nmemb * size);
return (ptr);
}