-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_handler.c
56 lines (47 loc) · 944 Bytes
/
input_handler.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
#include "shell.h"
/**
* remove_newline - Removes the newline character from a string.
* @str: The string to modify.
*/
void remove_newline(char *str)
{
size_t len = strlen(str);
if (len > 0 && str[len - 1] == '\n')
{
str[len - 1] = '\0';
}
}
/**
* read_input_handler - Reads a line of input from the user.
*
* Return: A pointer to the input string.
*/
char *read_input_handler(void)
{
char *input = NULL;
size_t len = 0;
if (getline(&input, &len, stdin) == -1)
{
free(input);
return NULL;
}
remove_newline(input);
return input;
}
/**
* r_input - Reads a line of input from the user.
*
* Return: A pointer to the input string.
*/
char *r_input(void)
{
char *input = NULL;
size_t size = 0;
if (getline(&input, &size, stdin) == -1)
{
free(input);
return NULL;
}
input[str_cmp(input, "\n")] = '\0';
return input;
}