-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_getline.c
64 lines (61 loc) · 1.52 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
#include "shell.h"
/**
* find_next_newline - function that look up for newline or end of the buffer
*
* @buffer: buffer to look up in
* @length: the length of the buffer
* Return: number of read character or -1 in case of error
*/
size_t find_next_newline(char *buffer, size_t length)
{
size_t position;
position = 0;
while (position < length && buffer[position] != '\n')
position++;
return (position);
}
/**
* _getline - function takes address of line to fill with characters
*
* @line: address to line that will be filled with characters
* Return: number of read characters in case of error return -1
*/
ssize_t _getline(char **line)
{
static char buffer[BUFFER_SIZE];
static ssize_t current_position, end_of_buffer;
ssize_t old_size, next_newline;
*line = NULL;
old_size = 0;
while (1)
{
if (current_position >= end_of_buffer)
{
end_of_buffer = read(STDIN_FILENO, buffer, BUFFER_SIZE);
current_position = 0;
if (end_of_buffer < 0)
{
free(*line);
*line = NULL;
return (-1);
}
if (end_of_buffer == 0)
return (old_size);
}
next_newline = find_next_newline(buffer + current_position,
end_of_buffer - current_position);
*line = _realloc(*line, old_size, old_size + next_newline);
if (!*line)
return (-1);
_copy((*line) + old_size, buffer + current_position, next_newline);
old_size += next_newline;
current_position += next_newline;
if (current_position < end_of_buffer)
{
current_position++;
return (old_size);
}
current_position++;
}
return (-1);
}