-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_next_line_utils_bonus.c
More file actions
75 lines (66 loc) · 1.92 KB
/
get_next_line_utils_bonus.c
File metadata and controls
75 lines (66 loc) · 1.92 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ladloff <ladloff@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/04 16:10:10 by ladloff #+# #+# */
/* Updated: 2023/05/22 01:32:10 by ladloff ### ########.fr */
/* */
/* ************************************************************************** */
#include <limits.h>
#include "get_next_line_bonus.h"
static void ft_bzero(void *s, size_t n)
{
unsigned char *p_s;
p_s = s;
while (n-- > 0)
*p_s++ = 0;
}
void *ft_calloc(size_t count, size_t size)
{
size_t total_size;
void *p;
if (!count || !size)
return (NULL);
if (count > ULONG_MAX / 2 || size > ULONG_MAX / 2)
return (NULL);
total_size = count * size;
p = malloc(total_size);
if (!p)
return (NULL);
ft_bzero(p, total_size);
return (p);
}
t_gnl *free_gnl_node(t_gnl *current)
{
t_gnl *next;
if (!current)
return (NULL);
next = current->next;
free(current->buffer);
free(current);
return (next);
}
t_gnl *create_gnl_node(int fd)
{
t_gnl *new_node;
new_node = ft_calloc(1, sizeof(t_gnl));
if (!new_node)
return (NULL);
new_node->buffer = ft_calloc(BUFFER_SIZE, sizeof(char));
if (!new_node->buffer)
{
free(new_node);
return (NULL);
}
new_node->read_bytes = read(fd, new_node->buffer, BUFFER_SIZE);
if (new_node->read_bytes < 0)
{
free(new_node->buffer);
free(new_node);
return (NULL);
}
return (new_node);
}