-
Notifications
You must be signed in to change notification settings - Fork 0
/
Custom Shell.c
70 lines (65 loc) · 1.27 KB
/
Custom 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdbool.h>
void Execute(char *input)
{
char *command[1024];
char *token;
int Index = 0;
token = strtok(input, " \n");
while (token != NULL)
{
command[Index++] = token;
token = strtok(NULL, " \n");
}
command[Index] = NULL;
if (strcmp(command[0], "exit") == 0)
{
exit(0);
}
pid_t pid = fork();
if (pid == 0)
{
int execution = execvp(command[0], command);
// if(execution)
printf("Execution Status %d", execution);
if (execution == -1)
{
perror("Execution Failed");
exit(EXIT_FAILURE);
}
}
else if (pid)
{
wait(NULL);
}
if (pid == -1)
{
perror("fork failed");
exit(EXIT_FAILURE);
}
}
void handleMain()
{
char input[1024];
printf("Starting Custom Shell..... \n");
while (true)
{
printf("myCustomShell> ");
char *inputToken = fgets(input, sizeof(input), stdin);
if (inputToken == NULL)
{
printf("Null Input Detected! ");
break;
}
Execute(input);
}
}
int main()
{
handleMain();
return 0;
}