-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_line.c
66 lines (60 loc) · 904 Bytes
/
get_line.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
#include "shell.h"
/**
* _getline - Read The Input By User From Stdin
* Return: Input
*/
char *_getline()
{
int i, buffsize = BUFSIZE, rd;
char c = 0;
char *buff = malloc(buffsize);
if (buff == NULL)
{
free(buff);
return (NULL);
}
for (i = 0; c != EOF && c != '\n'; i++)
{
fflush(stdin);
rd = read(STDIN_FILENO, &c, 1);
if (rd == 0)
{
free(buff);
exit(EXIT_SUCCESS);
}
buff[i] = c;
if (buff[0] == '\n')
{
free(buff);
return ("\0");
}
if (i >= buffsize)
{
buff = _realloc(buff, buffsize, buffsize + 1);
if (buff == NULL)
{
return (NULL);
}
}
}
buff[i] = '\0';
hashtag_handle(buff);
return (buff);
}
/**
* hashtag_handle - remove everything after #
* @buff: input;
* Return:void
*/
void hashtag_handle(char *buff)
{
int i;
for (i = 0; buff[i] != '\0'; i++)
{
if (buff[i] == '#')
{
buff[i] = '\0';
break;
}
}
}