-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path_get_line.c
48 lines (43 loc) · 833 Bytes
/
_get_line.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
#include "shell.h"
/**
* get_line - Read input from the standard input.
*
* Return: The string enter by the user.
*/
void *get_line(void)
{
static char buffer[BUFFER_SIZE];
static int buf_pos, buf_size;
char *input_str = NULL;
char current_char;
int input_len = 0;
while (1)
{
if (buf_pos >= buf_size)
{
buf_size = read(STDIN_FILENO, buffer, BUFFER_SIZE);
buf_pos = 0;
if (buf_size == 0)
return (input_str);
else if (buf_size < 0)
{
perror("read");
return (NULL);
}
}
current_char = buffer[buf_pos];
buf_pos++;
if (current_char == '\n')
{
input_str = realloc(input_str, input_len + 1);
input_str[input_len] = '\0';
return (input_str);
}
else
{
input_str = realloc(input_str, input_len + 1);
input_str[input_len] = current_char;
input_len++;
}
}
}