-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.c
62 lines (44 loc) · 1.3 KB
/
shell.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
#include "main.h"
void exit_handler(int signum) {
exit(EXIT_SUCCESS);
}
int main() {
char *input, *EXIT = "exit", **argv, **commands;
char user[100], host[200];
char *curdir = (char *) malloc(250 * sizeof(char));
memset(bg_proc, -1, sizeof(bg_proc));
char curhome[250];
int len = readlink("/proc/self/exe", curhome, 250);
if (len < 0)
getcwd(HOME, 250);
else {
int i;
for (i = strlen(curhome) - 1; curhome[i] != '/'; i--)
curhome[i] = '\0';
curhome[i] = '\0';
strcpy(HOME, curhome);
char temp[20];
strcpy(temp, "cd ~");
argv = tokenize_input(temp, W_DELIM);
execute(argv);
free(argv);
}
int state = 1;
do {
signal(SIGINT, exit_handler);
cuserid(user);
gethostname(host, 200);
getcwd(curdir, 250);
printf(MGN "<%s@%s:%s> " RESET, user, host, process_dir(curdir));
input = get_input();
commands = tokenize_input(input, ";");
for (int i = 0; commands[i] != NULL && state; i++) {
argv = tokenize_input(commands[i], W_DELIM);
state = execute(argv);
free(argv);
}
free(input);
free(commands);
} while (state);
return 0;
}