-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
305 lines (267 loc) · 8.55 KB
/
main.c
File metadata and controls
305 lines (267 loc) · 8.55 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "headers.h"
char initial_address[256];
char terminal_name[256];
int foreground = -1;
void ctrl_c(int signo)
{
if (foreground > 0)
{
kill(foreground, SIGINT);
}
}
int main(int argc, char *argv[])
{
// hostname
char hostname[256];
int result1 = gethostname(hostname, 256);
// // printf("System Name (Hostname): %s\n", hostname);
// // username
char *username = getlogin();
// // printf("Username: %s\n", username);
// // current working directory [spec-1]
// // char initial_address[256];
char *result = getcwd(initial_address, 256);
// // printf("%s",initial_address);
// // char showing_address[256];
char *past_commands[15];
int command_count = 0;
// Keep accepting commands
char hist_file_path[272];
snprintf(hist_file_path, sizeof(hist_file_path), "%s/%s", initial_address, "/hist_file.txt");
// printf("%s",hist_file_path);
FILE *file;
if (access(hist_file_path, F_OK | R_OK) != -1)
{
file = fopen(hist_file_path, "r");
}
else
{
file = fopen(hist_file_path, "a");
}
// int command_count = 0;
char line[256];
size_t line_buf_size;
while ((fgets(line, sizeof(line), file)) != NULL)
{
// printf("%s",line);
past_commands[command_count] = strdup(line);
command_count++;
}
signal(SIGINT, ctrl_c);
while (1)
{
int initial = dup(1);
// // current working directory
char current_address[256];
// char showing_address[256];
char *result2 = getcwd(current_address, 256);
prompt(initial_address);
char input[4096];
fgets(input, 4096, stdin);
input[strlen(input) - 1] = '\0';
char *commands[100];
int number_of_commands = 0;
char *cmd = strtok(input, ";");
while (cmd != NULL)
{
commands[number_of_commands] = strdup(cmd);
cmd = strtok(NULL, ";");
number_of_commands += 1;
}
if (feof(stdin))
{
printf("\n");
kill(0, SIGKILL);
break;
}
// printf("%s",commands[number_of_commands-1]);
// return;
for (int c = 0; c < number_of_commands; c++)
{
char command[256];
char copy_command[256];
strcpy(command, commands[c]);
strcpy(copy_command, commands[c]);
// printf("%s\n",commands[c]);
// return;
char *tokens[100];
int number_of_tokens = 0;
char *token = strtok(copy_command, " ");
while (token != NULL)
{
tokens[number_of_tokens++] = strdup(token);
token = strtok(NULL, " ");
}
if (strcmp("pastevents", tokens[0]))
{
// printf("%s",command);
if (command_count > 0)
{
if (strcmp(command, past_commands[command_count - 1]))
{
// printf("%s\n",command);
// printf("%s\n",past_commands[command_count - 1]);
if (command_count < 15)
{
past_commands[command_count++] = strdup(command);
}
else
{
for (int i = 1; i < 15; i++)
{
char temp[256];
strcpy(temp, past_commands[i]);
// printf("%s\n",temp);
free(past_commands[i - 1]);
past_commands[i - 1] = strdup(temp);
}
free(past_commands[14]);
past_commands[14] = strdup(command);
}
}
}
else
{
past_commands[command_count++] = strdup(command);
}
}
// I/O Redirection [spec-9]
int input_flag = 0;
int output_flag = 0;
int overwrite_flag = 0;
for (int i = 0; i < number_of_tokens; i++)
{
if (!strcmp(tokens[i], "<"))
{
// printf("%s ",tokens[i]);
input_flag = 1;
}
if (!strcmp(tokens[i], ">"))
{
// printf("%s ",tokens[i]);
output_flag = 1;
}
if (!strcmp(tokens[i], ">>"))
{
// printf("%s ",tokens[i]);
overwrite_flag = 1;
}
}
// printf("%d %d %d\n",input_flag,output_flag,overwrite_flag);
// continue;
if (input_flag || output_flag || overwrite_flag)
{
// free(*tokens);
// printf("Here first");
char *new_command = io_redrn(command, input_flag, output_flag, overwrite_flag, past_commands, command_count, initial_address, current_address);
dup2(initial, STDOUT_FILENO);
close(initial);
continue;
}
// pipes [spec-10]
int pipe_flag = 0;
for (int i = 0; i < number_of_tokens - 1; i++)
{
if (!strcmp(tokens[i], "|") && !strcmp(tokens[i + 1], "|"))
{
pipe_flag = 2;
}
}
if (!strcmp(tokens[0], "|") || !strcmp(tokens[number_of_tokens - 1], "|"))
{
pipe_flag = 2;
}
if (pipe_flag == 2)
{
// printf("Invalid Pipe Usage!\n");
printf("Invalid Usage of Pipe!\n");
continue;
}
pipe_flag = 0;
for (int i = 0; i < number_of_tokens; i++)
{
if (!strcmp(tokens[i], "|"))
{
pipe_flag = 1;
}
}
if (pipe_flag)
{
printf("pipe first");
int pip = pipes(command, past_commands, command_count, initial_address, current_address);
continue;
}
// warp [spec-3]
if (!strcmp(tokens[0], "warp"))
{
warp(command, initial_address, current_address);
continue;
}
// peek [spec-4]
if (!strcmp(tokens[0], "peek"))
{
int p2 = peek(command);
continue;
}
// pastevents [spec-5]
if (!strcmp(tokens[0], "pastevents"))
{
// printf("%d\n",command_count);
// continue;
command_count = pastevents(command, past_commands, command_count, initial_address, current_address);
continue;
}
// proclore [spec-7]
if (!strcmp(tokens[0], "proclore"))
{
int p = proclore(command);
continue;
}
// seek [spec-8]
if (!strcmp(tokens[0], "seek"))
{
int x = seek(command);
continue;
}
// activities [spec-12]
if (!strcmp(tokens[0], "activities"))
{
int act = activities(command);
continue;
}
// ping [spec-13]
if (!strcmp(tokens[0], "ping"))
{
int pi = ping(command);
continue;
}
// fg and bg [spec-14]
if (!strcmp(tokens[0], "fg"))
{
int fgp = atoi(tokens[1]);
int fgr = fg(fgp);
continue;
}
if (!strcmp(tokens[0], "bg"))
{
int bgp = atoi(tokens[1]);
int bgr = bg(bgp);
continue;
}
// neonate [spec-15]
if (!strcmp(tokens[0], "neonate"))
{
int neo = neonate(command);
continue;
}
// iman [spec-16]
if (!strcmp(tokens[0], "iMan"))
{
int im = iman(tokens[1]);
continue;
}
// system commands [spec-6]
int sys = system_calls(command);
}
}
}