-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.c
321 lines (310 loc) · 6.2 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Kaustav Vats (2016048)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define COLOR_RED "\x1b[31m"
#define COLOR_GREEN "\x1b[32m"
#define COLOR_YELLOW "\x1b[33m"
#define COLOR_BLUE "\x1b[34m"
#define COLOR_MAGENTA "\x1b[35m"
#define COLOR_CYAN "\x1b[36m"
#define COLOR_RESET "\x1b[0m"
char input[100];
char *pArg[100];
char *path = "/mnt/d/College/Semester-4/Operating System/Assignments/Assignment 0/Shell in C/commands/";
char *pPath[30];
void Help()
{
printf("\n-----Need Help?-----\n");
printf("Commands Available\n");
printf("cd\n");
printf("exit\n");
printf("ls\n");
printf("pwd\n");
printf("rm\n");
printf("cat\n");
printf("date\n");
printf("mkdir\n");
printf("history\n");
printf("echo\n\n");
}
void pwd()
{
char currentwd[1024];
getcwd(currentwd, sizeof(currentwd));
printf(COLOR_YELLOW "%s" COLOR_GREEN " >> " COLOR_RESET, currentwd );
}
void getpwd()
{
char currentwd[1024];
getcwd(currentwd, sizeof(currentwd));
printf("%s\n", currentwd);
}
int main(int argc, char const *argv[])
{
printf("Ultra Simple Shell: Type" COLOR_RED " 'exit' " COLOR_RESET "to exit the shell.\n");
while ( 1 )
{
printf(COLOR_CYAN "ush@" COLOR_RESET );
pwd();
if( fgets(input, 100, stdin) == NULL )
{
break;
}
if ( strcmp(input, "\n") == 0 )
{
continue;
}
// Append command to a file.
FILE *f;
f = fopen("/mnt/d/College/Semester-4/Operating System/Assignments/Assignment 0/Shell in C/log.txt", "a");
if ( strlen(input) != 0 )
{
// printf("Our Input: %s\n", input);
fprintf(f, "%s", input);
}
fclose(f);
if (input[strlen(input) - 1] == '\n')
{
input[strlen(input) - 1] = '\0';
}
char *parser;
parser = strtok(input, " ");
int j = 0;
while ( parser != NULL )
{
pArg[j] = parser;
parser = strtok(NULL, " ");
j++;
}
pArg[j] = NULL;
strcpy(pPath, path);
strcat(pPath, pArg[0]);
// Exit command
if ( strcmp(pArg[0], "exit") == 0 )
{
printf(COLOR_GREEN "Thankyou for using Ultra Simple Shell.\n" COLOR_RESET);
kill(0, SIGINT); // send an INT signal
kill(0, SIGKILL);
exit(0);
}
// Pwd command
else if ( strcmp(pArg[0], "pwd") == 0 )
{
getpwd();
}
// help command
else if ( strcmp(pArg[0], "help") == 0 )
{
Help();
}
// echo command
else if ( strcmp(pArg[0], "echo") == 0 )
{
if ( j == 1 )
{
if ( j == 1 )
{
printf("\n");
}
}
else
{
if ( strcmp(pArg[1], "-n") == 0 )
{
int j = 2;
while ( pArg[j] != NULL )
{
printf("%s ", pArg[j]);
j++;
}
}
else if ( strcmp(pArg[1], "-e") == 0 )
{
// printf("%s\n", "LOL1");
int j = 2;
while ( pArg[j] != NULL )
{
char *ptr;
ptr = pArg[j];
int i=0;
while ( i < strlen(pArg[j]) )
{
// printf("\n");
// printf("%s\n", ptr);
if ( ptr[i] == 92 )
{
if ( ptr[i+1] == 'n' )
{
// printf("%s\n", "lol2");
printf("\n");
}
else if ( ptr[i+1] == 'a' )
{
printf("\a");
}
else if ( ptr[i+1] == 'b' )
{
printf("\b");
}
else if ( ptr[i+1] == 'e' )
{
printf("\e");
}
else if ( ptr[i+1] == 'f' )
{
printf("\f");
}
else if ( ptr[i+1] == 'r' )
{
printf("\r");
}
else if ( ptr[i+1] == 't' )
{
printf("\t");
}
else if ( ptr[i+1] == 'v' )
{
printf("\v");
}
i++;
}
else
{
printf("%c", ptr[i]);
}
i++;
}
j++;
printf(" ");
}
printf("\n");
}
else if ( strcmp(pArg[1], "-E") == 0 )
{
int j = 2;
while ( pArg[j] != NULL )
{
printf("%s ", pArg[j]);
j++;
}
printf("\n");
}
else
{
char *p;
p = pArg[1];
if ( p[0] == 45 )
{
fprintf(stderr, "Syntax Error: echo\nUse: echo [option] [string]\nOptions: -n, -e, -E\nString in double quotes\n");
}
else
{
int j = 1;
while ( pArg[j] != NULL )
{
printf("%s ", pArg[j]);
j++;
}
printf("\n");
}
}
}
}
// cd command
else if ( strcmp(pArg[0], "cd") == 0 )
{
if ( j == 1 )
{
if ( chdir("/mnt") != 0 )
{
fprintf(stderr, "%s\n", "Path Problem!");
}
}
else if ( j == 2 )
{
if ( chdir(pArg[1]) != 0 )
{
fprintf(stderr, "%s\n", "Path Problem!");
}
}
else
{
char *p;
p = (char*)malloc(4095*sizeof(char));
int i = 1;
int k=0;
while ( pArg[i] != NULL )
{
char *ptr;
ptr = pArg[i];
int l = 0;
while ( l < strlen(pArg[i]) )
{
p[k] = ptr[l];
k++;
l++;
}
p[k] = ' ';
k++;
i++;
}
p[k-1] = '\0';
// printf("%s\n", p);
// printf("%d\n", strlen(p));
if ( chdir(p) != 0 )
{
fprintf(stderr, "%s\n", "Path Problem!");
}
}
}
// history command
else if ( strcmp(pArg[0], "history") == 0 )
{
if ( j == 1 )
{
FILE *f;
int MaxSize = 1024;
char Line[MaxSize];
if ( (f = fopen("/mnt/d/College/Semester-4/Operating System/Assignments/Assignment 0/Shell in C/log.txt", "rb")) == NULL )
{
fprintf(stderr, "%s\n", "Commands not stored properly, File Problem!");
}
int i=0;
// int j=0;
while ( fgets(Line, MaxSize, f) )
{
// if ( i%2 == 0 )
// {
// printf(" %d %s", j, Line);
// j++;
// }
printf(" %d %s", i, Line );
i++;
}
}
else
{
fprintf(stderr, "Syntax Error: history\nUse: history\n");
}
}
else if ( fork() == 0 )
{
// Child
if ( execvp(pPath, pArg) < 0 )
{
fprintf(stderr, "%s\n", "Could not execute command");
}
}
else
{
// Parent
wait(NULL);
}
}
return 0;
}