-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
199 lines (167 loc) · 5.81 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include "./include/cd.h"
#include "./include/pwdd.h"
#include "./include/echo.h"
#include "./include/variable.h"
#include "./include/prompt.h"
#include "./include/hostnameandusername.h"
#include "./include/ls.h"
#include "./include/history.h"
#include "./include/discover.h"
#include "./include/process.h"
#include "./include/backgroundvariable.h"
struct processNameAndPid proNamAndPid[MAX_NO_OF_PROCESS];
int NO_OF_PROCESSES = 0;
char HOSTNAME[PATHANDNAMESIZE];
char USERNAME[PATHANDNAMESIZE];
char CURRPATH[PATHANDNAMESIZE];
char PREVPATH[PATHANDNAMESIZE];
char HOMEDIR[PATHANDNAMESIZE];
char HISTORYPATH[PATHANDNAMESIZE];
int flagForAmpersand = 0;
int flagForPromptPrintAfterBackground = 0;
int flagForBackgroundRunningProcess = 0;
time_t time_taken = 0;
void main()
{
system("clear");
int lenHomeDir = getHostNameAndUserName();
char history[HISTORYSIZE][PATHANDNAMESIZE];
strcpy(HISTORYPATH, HOMEDIR);
strcat(HISTORYPATH, "/history.txt");
int noOfCommandsInHistory = loadHistory(history);
getcwd(PREVPATH, PATHANDNAMESIZE); // so that it is not empty in the first run
getcwd(CURRPATH, PATHANDNAMESIZE); // same
while (1)
{
prompt(lenHomeDir, time_taken);
time_taken = 0;
if (flagForBackgroundRunningProcess == 1)
{
checkIfProcessDone();
if (flagForPromptPrintAfterBackground)
{
flagForPromptPrintAfterBackground = 0;
prompt(lenHomeDir, time_taken);
}
}
char command[PATHANDNAMESIZE];
char *commandCopy = NULL;
commandCopy = (char *)calloc(PATHANDNAMESIZE, sizeof(char));
size_t commandsize = PATHANDNAMESIZE;
getline(&commandCopy, &commandsize, stdin);
if (!strcmp(commandCopy, "\n"))
continue;
strcpy(command, commandCopy);
// printf("%s", history[noOfCommandsInHistory - 1]);
if (strcmp(history[noOfCommandsInHistory - 1], command) != 0)
{
if (noOfCommandsInHistory == HISTORYSIZE)
{
for (int h = 1; h < HISTORYSIZE; h++)
strcpy(history[h - 1], history[h]);
strcpy(history[noOfCommandsInHistory - 1], command);
writeHistory(history, HISTORYSIZE);
}
else
{
strcpy(history[noOfCommandsInHistory++], command);
writeHistory(history, noOfCommandsInHistory);
}
}
flagForAmpersand = 0;
// for (int iForAmpersand = 0; iForAmpersand < strlen(commandCopy); iForAmpersand++)
// if (commandCopy[iForAmpersand] == '&')
// {
// commandCopy[iForAmpersand] = ';';
// flagForAmpersand = 1;
// }
char *tok = commandCopy;
char *string[PATHANDNAMESIZE];
memset(string, '\0', PATHANDNAMESIZE);
char *commaSeparatedCommands[PATHANDNAMESIZE];
memset(commaSeparatedCommands, '\0', PATHANDNAMESIZE);
// for (int j = 0; j < PATHANDNAMESIZE; j++)
// {
// string[j] = NULL;
// commaSeparatedCommands[j] = NULL;
// }
commaSeparatedCommands[0] = strtok_r(tok, ";", &tok);
int iteratorForCommaSeparatedCommands = 1;
while ((commaSeparatedCommands[iteratorForCommaSeparatedCommands] = strtok_r(NULL, ";", &tok)))
{
iteratorForCommaSeparatedCommands++;
}
int iForCmd = 0;
while (iForCmd < iteratorForCommaSeparatedCommands)
{
char *cmd = strtok_r(NULL, " \n", &commaSeparatedCommands[iForCmd]);
if (cmd == NULL)
break;
if (strcmp(cmd, "cd") == 0)
{
int i = 0;
string[i] = strtok_r(NULL, "\n/\\", &commaSeparatedCommands[iForCmd]);
if (string[i] != NULL) // for handling commands such as cd abc<space> ; echo shis h u uy fuy
{
for (int k = strlen(string[i]) - 1; k >= 0; k--)
{
if (*(string[i] + k) == 32)
*(string[i] + k) = 0;
else
break;
}
int flag = 0;
do
{
changeDirectory(string[i++], &flag);
} while ((string[i] = strtok_r(NULL, "\n/\\", &commaSeparatedCommands[iForCmd])));
}
}
else if (!strcmp(cmd, "pwd"))
{
presentWorkingDirectory();
}
else if (!strcmp(cmd, "echo"))
{
echo(commaSeparatedCommands[iForCmd]);
}
else if (!strcmp(cmd, "quit") || !strcmp(cmd, "exit"))
{
return;
// printf("\n exit was here \n");
}
else if (!strcmp(cmd, "ls"))
{
ls(commaSeparatedCommands[iForCmd]);
}
else if (!strcmp(cmd, "history"))
{
printHistory(history, noOfCommandsInHistory);
}
else if (!strcmp(cmd, "discover"))
{
discover(commaSeparatedCommands[iForCmd]);
}
else if (!strcmp(cmd, "pinfo"))
{
pinfo(commaSeparatedCommands[iForCmd]);
}
else
{
if (process(cmd, commaSeparatedCommands[iForCmd]))
{
// do nothing
}
else
printf("\033[0;91;49mInvalid Command\033[0m\n");
}
iForCmd++;
}
}
}