-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_getline.c
145 lines (138 loc) · 2.85 KB
/
_getline.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "_getline.h"
/**
* __getline - gets a line of chars from a file descriptor
* @fd: the file descriptor to read
*
* Return: pointer to the line
*/
char *__getline(const int fd)
{
static FdBuf head;
FdBuf *fb = NULL, *temp;
char *line = NULL;
if (fd == -1)
{
if (head.buf)
head.buf = (free(head.buf), NULL);
for (fb = head.next; fb;)
{
if (fb->buf)
{
free(fb->buf);
fb->buf = NULL;
}
temp = fb;
fb = fb->next;
free(temp);
}
_memset((void *)&head, 0, sizeof(head));
return (NULL);
}
fb = get_fdbuf(&head, fd);
if (fb)
line = __read_buf(fb);
if (line && line[0] == '\n' && !line[1])
line[0] = 0;
return (line);
}
/**
* __read_buf - reads into the buffer
* @fb: the fd buf struct
*
* Return: 0 on success else -1 on error.
*/
char *__read_buf(FdBuf *fb)
{
char buf[READ_SIZE + 1], *p, *line;
ssize_t r = 0;
p = __strchr(fb->buf + fb->i, '\n', fb->len - fb->i);
if (!fb->len || fb->i >= fb->len || !p)
{
while (1)
{
r = read(fb->fd, buf, READ_SIZE);
if (r < 0 || (r == 0 && !fb->len))
return (fb->buf ? (free(fb->buf), NULL) : NULL);
if (r == 0)
{
p = fb->buf + fb->len;
break;
}
fb->buf = _realloc(fb->buf, fb->len, fb->len + r + 1);
if (!fb->buf)
return (NULL);
_memcpy((void *)(fb->buf + fb->len), buf, r), fb->len += r;
p = __strchr(fb->buf + (fb->len - r), '\n', r);
if (p)
{
fb->buf[fb->len] = 0;
break;
}
}
}
*p = '\0';
line = malloc(1 + (p - (fb->buf + fb->i)));
if (!line)
return (NULL);
_memcpy((void *)line, fb->buf + fb->i, 1 + (p - (fb->buf + fb->i)));
fb->i = (p - fb->buf) + 1;
if (fb->i >= fb->len)
{
fb->i = fb->len = 0;
fb->buf = (free(fb->buf), NULL);
}
return (line);
}
/**
* get_fdbuf - adds a car to linked list
* @head: pointer to head node
* @fd: file descriptor of buffer to get
* Return: pointer to the fd buf node
*/
FdBuf *get_fdbuf(FdBuf *head, const int fd)
{
FdBuf *node;
if (!head->buf && !head->fd && !head->next)
{
head->fd = fd;
return (head);
}
for (; head->next && head->next->fd <= fd; head = head->next)
;
if (head->fd == fd)
return (head);
node = malloc(sizeof(*node));
if (!node)
return (NULL);
if (fd < head->fd) /* need to copy head over and replace */
{
_memcpy((void *)node, (void *)head, sizeof(*head));
_memset((void *)head, 0, sizeof(*head));
head->fd = fd;
head->next = node;
return (head);
}
_memset((void *)node, 0, sizeof(*node));
node->fd = fd;
node->next = head->next;
head->next = node;
return (node);
}
/**
**__strchr - locates a character in a string
*@s: the string to be parsed
*@c: the character to look for
*@size: number of bytes to search
*Return: (s) a pointer to the memory area s
*/
char *__strchr(char *s, char c, ssize_t size)
{
if (!s)
return (NULL);
do {
if (*s == c)
return (s);
s++;
} while (--size > 0);
return (NULL);
}