-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_getline.c
More file actions
45 lines (40 loc) · 979 Bytes
/
Copy path_getline.c
File metadata and controls
45 lines (40 loc) · 979 Bytes
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
#include "shell.h"
/**
* get_line - stores into malloced buffer the user's command into shell
* @str: buffer
* Return: number of characters read
*/
size_t get_line(char **str)
{
ssize_t i = 0, size = 0, t = 0, t2 = 0, n = 0;
char buff[1024];
/* read while there's stdin greater than buffsize; -1 to add a '\0' */
while (t2 == 0 && (i = read(STDIN_FILENO, buff, 1024 - 1)))
{
if (i == -1) /* check if read errored */
return (-1);
buff[i] = '\0'; /* terminate buff with \0 to use with _strcat */
n = 0; /* last loop if \n is found in the stdin read */
while (buff[n] != '\0')
{
if (buff[n] == '\n')
t2 = 1;
n++;
}
/* copy what's read to buff into get_line's buffer */
if (t == 0) /* malloc the first time */
{
i++;
*str = malloc(sizeof(char) * i);
*str = _strcpy(*str, buff);
size = i;
t = 1;
}
else /* _realloc via _strcat with each loop */
{
size += i;
*str = _strcat(*str, buff);
}
}
return (size);
}