-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_input.c
48 lines (46 loc) · 1.02 KB
/
read_input.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
// Implemetation of the command line input fcuntions
#include "read_input.h"
char *prompt1_read()
{
char *result = NULL;
char buffer[MAX_COMMAND_LEN] = {};
int i = 0;
int char_read;
print_ps1();
while((char_read = getchar()) != '\n')
{
if(char_read == EOF)
{
printf("exit\n");
exit(0);
}
else buffer[i++] = (char)char_read;
}
buffer[i++] = '\n';
buffer[i] = 0;
result = (char*)malloc((i + 1) * sizeof(char));
strcpy(result, buffer);
return result;
}
char *prompt2_read()
{
char *result = NULL;
char buffer[MAX_COMMAND_LEN] = {};
int i = 0;
int char_read;
print_ps2();
while((char_read = getchar()) != '\n')
{
if(char_read == EOF)
{
printf("exit\n");
exit(0);
}
else buffer[i++] = (char)char_read;
}
buffer[i++] = '\n';
buffer[i] = 0;
result = (char*)malloc((i + 1) * sizeof(char));
strcpy(result, buffer);
return result;
}