-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_input.c
More file actions
46 lines (43 loc) · 719 Bytes
/
read_input.c
File metadata and controls
46 lines (43 loc) · 719 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
46
#include "main.h"
/**
* read_input - stores whatever is passed to it as standard input
*
* Return: string containing the input
*/
char *read_input(void)
{
char *line = NULL;
ssize_t bytes_read;
size_t bufsize;
int i;
bufsize = 0;
bytes_read = getline(&line, &bufsize, stdin);
if (!line)
{
perror("Error allocating memory for buffer");
return (0);
}
if (bytes_read == 1)
{
free(line);
return (NULL);
}
else if (bytes_read == EOF)
{
if (isatty(STDIN_FILENO))
write(STDOUT_FILENO, "\n", 1);
free(line);
exit(0);
}
else
{
for (i = 0; line[i] == ' ' && line[i + 1] == ' '; i++)
;
if (!line[i] && line[i + 1] == '\n')
{
free(line);
return (0);
}
}
return (line);
}