-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_getline.c
52 lines (51 loc) · 1.02 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
#include "holberton.h"
#include <termios.h>
/**
* _getline - is a wrapper to use the getline function but with some
* considerations. The first is that it traps the Ctr C keyboard to
* invalidate the common behaviour. In our shell the CTR C doesn't do
* anything special.'
*
* Return: -1 for ctr c, -2 for ctrd
*/
char *_getline(void)
{
ssize_t res = 0;
size_t bufsize = 0;
int i = 0;
char *buf = NULL;
signal(SIGINT, _sigint_handler);
while (1)
{
if (isatty(STDIN_FILENO))
write(STDIN_FILENO, "$ ", 2);
res = getline(&buf, &bufsize, stdin);
if (res == EOF)
{
if (buf)
free(buf);
write(STDIN_FILENO, "\n", 1);
exit(0); }
if (buf[0] != '\n')
{
for (i = 0; buf[i] != '\0'; i++)
{
if (buf[i] == '\n')
return (buf); }}
else
{
continue; }
res = getline(&buf, &bufsize, stdin);
if (res == EOF)
{
if (buf)
free(buf);
write(STDIN_FILENO, "\n", 1);
exit(0); }}
if (res == EOF)
{
write(STDIN_FILENO, "\n", 1);
exit(0); }
if (buf)
free(buf);
return (NULL); }