-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils_bonus.c
82 lines (73 loc) · 1.91 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dshatilo <dshatilo@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 16:39:50 by dshatilo #+# #+# */
/* Updated: 2023/11/15 17:04:24 by dshatilo ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
char *ft_strchr(const char *s, int c)
{
size_t i;
i = 0;
while (*(s + i) != 0)
{
if (*(s + i) == (unsigned char) c)
return ((char *)s + i);
i++;
}
if ((unsigned char) c == 0)
return ((char *)s + i);
return (0);
}
char *ft_strncpy(char *dest, char *src, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
*(dest + i) = *(src + i);
i++;
}
return (dest);
}
char *ft_free(char *s)
{
free(s);
return (0);
}
char *add_to_string(char *s, char *buffer, size_t buf_len)
{
size_t s_len;
size_t new_len;
char *new_s;
s_len = 0;
while (s[s_len] != 0)
s_len++;
if ((size_t)(-1) - s_len - 1 < buf_len)
return (ft_free(s));
new_len = s_len + buf_len;
new_s = (char *)malloc(sizeof(char) * (new_len + 1));
if (!new_s)
return (ft_free(s));
new_s[new_len] = 0;
ft_strncpy(new_s, s, s_len);
ft_strncpy(new_s + s_len, buffer, buf_len);
free(s);
return (new_s);
}
char *check_string(char *s)
{
if (!s)
{
s = (char *)malloc(sizeof(char) * 1);
if (!s)
return (0);
*s = 0;
}
return (s);
}