-
Notifications
You must be signed in to change notification settings - Fork 0
/
getLine.c
49 lines (41 loc) · 950 Bytes
/
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
#include "shell.h"
/**
* _getLine - gets the next line of input from STDIN
* @Inf: parameter struct
* @length: size of reallocated ptr buffer if not NULL
* @ptr: address of pointer to buffer, reallocated or NULL
*
* Return: s
*/
int _getLine(info_t *Inf, char **ptr, size_t *length)
{
static char buf[READ_BUF_SIZE];
static size_t i, len;
size_t k;
ssize_t r = 0, s = 0;
char *p = NULL, *new_p = NULL, *c;
p = *ptr;
if (p && length)
s = *length;
if (i == len)
i = len = 0;
r = readBuffer(Inf, buf, &len);
if (r == -1 || (r == 0 && len == 0))
return (-1);
c = searchStr(buf + i, '\n');
k = c ? 1 + (unsigned int)(c - buf) : len;
new_p = myReallocate(p, s, s ? s + k : k + 1);
if (!new_p) /* MALLOC FAILURE! */
return (p ? free(p), -1 : -1);
if (s)
_strCat(new_p, buf + i, k - i);
else
_strcpy(new_p, buf + i, k - i + 1);
s += k - i;
i = k;
p = new_p;
if (length)
*length = s;
*ptr = p;
return (s);
}