-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_getline.c
89 lines (84 loc) · 1.55 KB
/
_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "shell.h"
/**
* assign_lineptr - Reassigns the lineptr variable for _getline.
* @line_ptr: A buffer to store an input string.
* @n: The size of lineptr.
* @buffer: The string to assign to lineptr.
* @b: The size of buffer.
*/
void assign_lineptr(char **line_ptr, size_t *n, char *buffer, size_t b)
{
char **lineptr = line_ptr;
char *buf = buffer;
size_t *new = n;
size_t c = b;
if (*lineptr == NULL)
{
if (c > 120)
*new = c;
else
*new = 120;
*lineptr = buf;
}
else if (*new < c)
{
if (c > 120)
*new = c;
else
*new = 120;
*lineptr = buf;
}
else
{
_strcpy(*lineptr, buf);
free(buf);
}
}
/**
* _getline - Reads input from a stream.
* @line_ptr: A buffer to store the input.
* @n: The size of lineptr.
* @stream: The stream to read from.
*
* Return: The number of bytes read.
*/
ssize_t _getline(char **line_ptr, size_t *n, FILE *stream)
{
size_t *new = n;
char **lineptr = line_ptr;
static ssize_t input;
ssize_t ret = 0;
char c = 'x';
char *buffer = NULL;
int r = 0;
if (input == 0)
fflush(stream);
else
return (-1);
buffer = malloc(sizeof(char) * 120);
if (!buffer)
return (-1);
while (c != '\n')
{
r = read(STDIN_FILENO, &c, 1);
if (r == -1 || (r == 0 && input == 0))
{
free(buffer);
return (-1);
}
if (r == 0 && input != 0)
{
input++;
break;
}
if (input >= 120)
buffer = _realloc(buffer, 120, input + 1);
buffer[input] = c, input++;
}
buffer[input] = '\n';
assign_lineptr(lineptr, new, buffer, input);
ret = input;
if (r != 0)
input = 0;
return (ret);
}