-
Notifications
You must be signed in to change notification settings - Fork 3
/
getline.c
51 lines (50 loc) · 972 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
50
51
#define MAX 4096
#define READING 1
#include "holberton.h"
#include <stdio.h>
#include <signal.h>
/**
* _getline - function that gets a line from the stdin.
* @pos: pointer to the actual buffer position.
* @pathname: path of the file to read.
* Return: pointer to the buffer that stores the line.
*/
char *_getline(ssize_t *pos, char *pathname)
{
int fd = 0, flag = 1, state = 0;
ssize_t n = 0;
char *cpy = NULL, *buf = NULL;
size_t size = MAX, aux = 0;
fd = open(pathname, O_RDONLY);
if (fd == -1)
return (NULL);
buf = _calloc(size, 1);
state = READING;
while (state)
{
if (!buf)
return (NULL);
n = read(fd, buf, MAX);
*pos += n;
if (buf[0] == 0)
break;
if (n == -1)
{
free(buf);
return (NULL);
}
if (n != MAX)
break;
aux = size, size += size;
buf = (char *)_realloc(buf, aux, size);
cpy = buf;
buf += aux;
flag = 0;
}
*pos -= 1;
if (buf[*pos] == '\n')
*pos -= 1;
if (flag)
return (buf);
return (cpy);
}