-
Notifications
You must be signed in to change notification settings - Fork 2
/
Shell.c
61 lines (53 loc) · 1.51 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
// C Program to design a shell in Linux
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#define MAXCOM 1000 // max number of letters to be supported
#define MAXLIST 100 // max number of commands to be supported
// Clearing the shell using escape sequences
#define clear() printf("\033[H\033[J")
// Greeting shell during startup
void init_shell()
{
clear();
printf("\n\n\n\n******************"
"************************");
printf("\n\n\n\t**** WELCOM TO KAT-OS****");
printf("\n\n\t-FOR THE KOOT_KOOT-");
printf("\n\n\n\n*******************"
"***********************");
char* username = getenv("USER");
printf("\n\n\nUSER is: @%s", username);
printf("\n");
sleep(1);
clear();
}
// Function to take input
int takeInput(char* str) {
char str;
printf("\n>>>");
scanf("%d", &str);
return str;
}
int main() {
init_shell();
while(1) {
char[] command = takeInput();
if (fork() == 0) {
// Newly spawned child Process. This will be taken over by "ls -l"
int status_code = execvp("/Shell/" + command, null);
printf("ls -l has taken control of this child process. This won't execute unless it terminates abnormally!\n");
if (status_code == -1) {
printf("Terminated Incorrectly\n");
return 1;
}
}
//try executing /shell/$command
//return error if it doesnt work
//start over
}
return 0;
}