-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_getline.c
51 lines (48 loc) · 1.27 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
#include "shell.h"
/**
* _getline - reads input from a file descriptor
* @lineptr: a pointer to the string buffer
* @n: number of bytes written
* @fd: file descriptor
*
* Return: the number of bytes on success, else -1 on error
*/
ssize_t _getline(char **lineptr, size_t *n, int fd)
{
ssize_t n_read;
size_t total_read, buffer_size = BUFF_SIZE;
if (*lineptr == NULL) /* check if we should allocate memory */
{
*lineptr = malloc(sizeof(char) * (buffer_size + 1));
if (*lineptr == NULL)
return (-1); /* memory allocation failed */
}
n_read = total_read = 0;
while ((n_read = read(fd, *lineptr + total_read, BUFF_SIZE)) > 0)
{
total_read += n_read;
/* check if the buffer needs to be resized */
if (total_read >= buffer_size)
{
buffer_size *= 2;
*lineptr = _realloc(*lineptr, total_read, buffer_size);
if (*lineptr == NULL)
return (-1); /* resizing buffer memory failed */
*n = total_read;
}
if (total_read && (*lineptr)[total_read - 1] == '\n')
{
(*lineptr)[total_read] = '\0';
*n = total_read;
return (total_read);
}
}
if (n_read == -1)
{
safe_free(*lineptr);
return (-1); /* reading from file descriptor failed */
}
if (total_read == 0)
safe_free(*lineptr); /* nothing was read, free the memory */
return (total_read);
}