-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
69 lines (65 loc) · 1.8 KB
/
main.c
File metadata and controls
69 lines (65 loc) · 1.8 KB
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
#include "headers.h"
void remove_extra_spaces(char *str) {
int len = strlen(str);
int i, j;
bool space_found = false;
int new_length = 0;
for (i = 0; i < len; i++) {
if (str[i] != ' ' && str[i] != '\t') {
str[new_length++] = str[i];
space_found = false;
} else if (i > 0 && !space_found) {
str[new_length++] = ' ';
space_found = true;
}
}
// Handle trailing space
if (new_length > 0 && str[new_length - 1] == ' ') {
str[new_length - 1] = '\0';
} else {
str[new_length] = '\0';
}
}
int main()
{
getcwd(homedir, N);
strcpy(curdir, homedir);
strcpy(predir, "-1");
char* out = (char*) malloc(N);
strcpy(out, "-1");
// Keep accepting commands
while (1)
{
// Print appropriate prompt with username, systemname and directory before accepting input
prompt(out);
strcpy(out, "-1");
char* inp = (char*) malloc(N);
fgets(inp, N, stdin);
char* input = (char*) malloc(N);
int cr = 0;
for(int i = 0; i < strlen(inp); ++i){
input[cr++] = inp[i];
if(inp[i] == '&')
input[cr++] = ';';
}
input[cr] = '\0';
remove_extra_spaces(input);
checkprocess();
if(strstr(input, "pastevents") == NULL){
add(input);
}
// tokenize based on ; and then based on &
char* temp = (char*) malloc(N);
strcpy(temp, input);
char* here = strtok_r(temp, ";", &temp);
// printf("%s\n", here);
int cnt = 0;
while(here != NULL){
if(!strcmp(out, "-1"))
out = call(here);
else
call(here);
here = strtok_r(NULL, ";", &temp);
}
}
}