-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
83 lines (73 loc) · 1.76 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "headers.h"
#include "util.h"
#include "history_handler.h"
#include "zombie_killer.h"
#include "parser.h"
#include <signal.h>
char *shellName;
void rip_child(int signum) {
if (signum == SIGCHLD)
zombie_process_check();
}
void quit() {
killbg();
printf("cya\n");
_exit(0);
}
void printExitCode() {
if (exit_code == 0) {
printf(":) ");
} else {
printf(":( ");
}
exit_code = 0;
}
int main() {
clearScreen();
welcomeMessage();
shellName = getShellName();
if (getcwd(homeDir, size_buff) == NULL) {
perror("getcwd failed");
}
if (homeDir[strlen(homeDir) - 1] != '/') {
strcat(homeDir, "/");
}
//signal(SIGCHLD, rip_child);
signal(SIGINT, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
//signal(SIGTTIN, SIG_IGN);
strcpy(currDir, homeDir);
updateShowDir();
while (1) {
rip_child(SIGCHLD);
printCyan();
printf("%s", shellName);
printGreen();
printf("%s ", showDir);
printYellow();
printf("$ ");
resetColor();
char *line = malloc(size_buff);
//size_t t = size_buff;
char *line2 = line;
if(fgets(line, size_buff, stdin) == NULL){
//fprintf(stderr, "NULL LOL \n");
quit();
}
//getline(&line, &t, stdin);
size_t ln = strlen(line) - 1;
if (*line && line[ln] == '\n')
line[ln] = '\0';
line = trim_whitespace(line);
add_history(line);
getIndividualCommands(line);
free(line2);
printYellow();
printExitCode();
resetColor();
}
}
/*
getenv setenv - get unset and fetch environment variables from host environment list
*/