-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_readline.c
46 lines (38 loc) · 813 Bytes
/
_readline.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
#include "main.h"
/**
* _readLine - functionn reads command line input in the interactive shell
*
* Return: returns a pointer to the buffer containing the read input
*/
char *_readLine()
{
/* declare variables */
ssize_t n_read;
char *line_ptr;
size_t n;
/* initialize variables */
line_ptr = NULL;
n = 0;
errno = 0;
/*signal(SIGINT, SIG_IGN)*/
if (line_ptr != NULL)
free(line_ptr);
/* read input command line */
n_read = getline(&line_ptr, &n, stdin);
if (n_read == -1 && errno != 0)
{
perror("getline");
free(line_ptr);
return (NULL);
}
/* add condition to catch Ctrl+C signal */
/* executes when Ctrl+D is used to signall end-of-file */
if (n_read == -1 && errno == 0)
{
free(line_ptr);
if (isatty(STDIN_FILENO))
putchar('\n');
exit(0);
}
return (line_ptr);
}