forked from EugeneJoe/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetline.c
70 lines (66 loc) · 1.04 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
#include "shellheader.h"
/**
* _getline - reads user inputfrom STDIN.
* @argv: pointer to array of pointers to arguments to the program
*
* Return: Input.
*/
char *_getline(char **argv)
{
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);
perror(argv[0]);
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)
{
perror(argv[0]);
return (NULL);
}
}
}
buff[i] = '\0';
printf("%s\n", buff);
hash_handle(buff);
return (buff);
}
/**
* hash_handle - purges anything after # from input.
* @buff: input.
*
* Return: VOID.
*/
void hash_handle(char *buff)
{
int i;
for (i = 0; buff[i] != '\0'; i++)
{
if (buff[i] == '#')
{
buff[i] = '\0';
break;
}
}
}