/
wish.c
130 lines (112 loc) · 3.08 KB
/
wish.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
int main(int argc, char *argv[]){
char *input = NULL;
size_t length = 0;
if(argc > 1 && strcmp(argv[1], "batch.txt") == 0){
printf("batch command here\n");
}
else if(argc == 1){
// Command prompt begins
printf("wish> ");
while(getline(&input, &length, stdin) != EOF){
//Parse the input as a command and then execute
char * temp = malloc(sizeof(char) * strlen(input));
if(temp == NULL) {
printf("Error allocating memory\n");
exit(1);
}
strcpy(temp, input);
temp = strtok(input, " \n");
int argCount = 0;
// Determine the amount of args
while(temp != NULL){
temp = strtok(NULL, " \n");
argCount++;
}
// Correctly allocate the memory and parse the args
char** command = malloc(sizeof(char*) * argCount);
if(command == NULL) {
printf("Error allocating memory\n");
exit(1);
}
command[0] = strtok(input, " \t\n");
printf("%s ", command[0]);
// Parsing the arguments
int i = 0;
while(command[i] != NULL){
i++;
command[i] = strtok(NULL, " \t\n");
printf("%s ", command[i]);
}
printf("\n");
// Execute forked program here
pid_t child = fork();
// Attempt to execute the command
if(child == 0) {
int result = access(command[0], X_OK);
// Attempt to access it in a relative directory
if(result == 0){
execv(command[0],command);
}
// If not here, check builtin commands
else{
int pathLength = 0;
char* binPath = "/bin/";
pathLength = strlen(binPath) + strlen(command[0]);
binPath = malloc(sizeof(char) * pathLength);
if(binPath == NULL) {
printf("Error allocating memory\n");
exit(1);
}
strcpy(binPath,"/bin/");
strcat(binPath, command[0]);
command[0] = malloc(sizeof(char) * (strlen(command[0]) + strlen(binPath)));
strcpy(command[0],binPath);
result = access(binPath, X_OK);
if(result == 0){
execv(command[0],command);
}
else{
pathLength = 0;
char* userPath = "/usr";
pathLength = strlen(userPath) + strlen(command[0]);
userPath = malloc(sizeof(char) * pathLength);
strcpy(userPath,"/usr");
strcat(userPath, command[0]);
command[0] = malloc(sizeof(char) * (strlen(command[0]) + strlen(userPath)));
strcpy(command[0],userPath);
i = 0;
printf("\nInside user, %i args\n",argCount);
while( i < argCount){
printf("%s ",command[i]);
i++;
}
result = access(userPath, X_OK);
if(result == 0){
execv(command[0],command);
} else{
printf("Couldnt execute command\n");
exit(1);
}
}
}
} else {
// wait for child process to complete before waiting for another command
int returnStatus = 0;
waitpid(child, &returnStatus, 0);
printf("wish> ");
}
for(i = 0; i < argCount; i++)
free(command[i]);
free(command);
}
}
free(input);
return 0;
}