-
Notifications
You must be signed in to change notification settings - Fork 0
/
My_getline.c
49 lines (45 loc) · 929 Bytes
/
My_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
* @info: parameter struct
* @ptr: address of pointer to buffer, preallocated or NULL
* @length: size of preallocated ptr buffer if not NULL
*
* Return: s
*/
int _getline(info_t *info, char **ptr, size_t *length)
{
static char buf[READ_BUF_SIZE];
static size_t i, len;
ssize_t r = 0;
size_t k;
char *p = *ptr;
char *new_p = NULL, *c;
size_t s = (p && length) ? *length : 0;
if (i == len)
{
i = len = 0;
r = read_buf(info, buf, &len);
if (r == -1 || (r == 0 && len == 0))
return (-1);
}
c = _strchr(buf + i, '\n');
k = c ? (unsigned int)(c - buf) + 1 : len;
new_p = _realloc(p, s, s ? s + k : k + 1);
if (!new_p)
{
if (p)
free(p);
return (-1);
}
if (s)
_strncat(new_p, buf + i, k - i);
else
_strncpy(new_p, buf + i, k - i + 1);
s += k - i;
i = k;
*ptr = new_p;
if (length)
*length = s;
return (s);
}