This repository has been archived by the owner on Nov 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_next_line_utils.c
114 lines (104 loc) · 2.15 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcombeau <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/04 14:09:27 by mcombeau #+# #+# */
/* Updated: 2021/12/15 20:33:39 by mcombeau ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
int contains_newline(const char *s)
{
int i;
i = 0;
while (s[i])
{
if (s[i] == '\n')
return (1);
i++;
}
return (0);
}
char *join_strs(const char *s1, const char *s2)
{
char *s;
int len;
int i;
len = 0;
if (!s1 && !s2)
return (NULL);
while (s1 && s1[len])
len++;
i = 0;
while (s2 && s2[i])
i++;
s = ft_malloc_zero(len + i + 1, sizeof * s);
if (!s)
return (NULL);
len = -1;
while (s1 && s1[++len])
s[len] = s1[len];
i = -1;
while (s2 && s2[++i])
s[len + i] = s2[i];
return (s);
}
char *ft_strdup(const char *s1)
{
char *s2;
int i;
if (!s1)
return (ft_strdup(""));
i = 0;
while (s1[i])
i++;
s2 = ft_malloc_zero(i + 1, sizeof * s2);
if (!s2)
return (NULL);
i = 0;
while (s1[i])
{
s2[i] = s1[i];
i++;
}
return (s2);
}
void *ft_malloc_zero(size_t count, size_t size)
{
void *r;
unsigned char *p;
size_t total;
total = count * size;
r = malloc(total);
if (!r)
return (NULL);
p = (unsigned char *)r;
while (total != 0)
{
*p = '\0';
p++;
total--;
}
return (r);
}
void ft_free_strs(char **str, char **str2, char **str3)
{
if (str && *str)
{
free(*str);
*str = NULL;
}
if (str2 && *str2)
{
free(*str2);
*str2 = NULL;
}
if (str3 && *str3)
{
free(*str3);
*str3 = NULL;
}
}