-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
105 lines (79 loc) · 2.24 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "main.h"
// takes input from the terminal
void getInputToTerminal(){
inputGiven = (char *)malloc(SIZE);
// Checking memory allocation error
if(inputGiven == NULL){
printf("Oops! Memory Error!\n");
return;
}
// Taking Input
char * checkError = fgets(inputGiven, SIZE, stdin);
// int checkError = gets(inputGiven);
if (checkError < 0) {
printf("Oops! Input Error!\n");
return;
}
// Condition for CtrlD
else if (checkError == 0) {
printf("\n");
exit(0);
}
// Checking the clear condition
if (strcmp(inputGiven, "clear") == 0) {
clear;
}
strcpy(copyOfInput, inputGiven);
}
// stores the current dir to currdir
void getCurrentDirectory() {
if(getcwd(currentDir, SIZE) == NULL) {
perror("");
exit(0);
}
return;
}
// Stores the home directory - initiliazed only once in the whole program.
void getPseudoHome() {
getCurrentDirectory();
strcpy(pseudoHome, currentDir);
}
int main()
{
//Getting the username of the current active user
getlogin_r(username, sizeof(username));
//Getting the systemname
gethostname(systemname, sizeof(systemname));
// Prints the welcome post.
welcomePrompt();
// Store the home initially.
getPseudoHome();
// Initially lastCD = home path only.
strcpy(lastCD, pseudoHome);
// make total no. of processes = 0
totalNoOfJobs = 0;
noOfForeProcesses = 0;
// Loads the history file
loadHistory();
while (1)
{
// check if any child process terminated
signal(SIGCHLD, sigchildHandler);
// Signal Handlers for CtrlC and Z
signal(SIGINT, signalControl);
signal(SIGTSTP, signalControl);
// Get current directory
getCurrentDirectory();
// Prints the welcome message
printInPrompt();
// Takes the full command with the arguments
getInputToTerminal();
// Checks commands
commandHandler();
// writes to history
if(changeHistory()==0) {
continue;
}
}
return 0;
}