-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpath.c
More file actions
221 lines (195 loc) · 4.12 KB
/
path.c
File metadata and controls
221 lines (195 loc) · 4.12 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // For strtok() and strcmp()
#include <unistd.h> // For fork(), pid_t
#include <sys/wait.h> // For waitpid() and associated macros
char SHELL_NAME[50] = "Prompt";
int QUIT = 0;
#define BUFSIZE 20
char* buffer[BUFSIZE];
// Function to read a line from command into the buffer
char *readLine()
{
char *line = (char *)malloc(sizeof(char) * 1024); // Dynamically Allocate Buffer
char c;
int pos = 0, bufsize = 1024;
if (!line)
{
printf("\nBuffer Allocation Error.");
exit(EXIT_FAILURE);
}
while(1)
{
c=getchar();
if (c == EOF || c == '\n') // If End of File or New line, replace with Null character
{
line[pos] = '\0';
return line;
}
else
{
line[pos] = c;
}
pos ++;
// If we have exceeded the buffer
if (pos >= bufsize)
{
bufsize += 1024;
line = realloc(line, sizeof(char) * bufsize);
if (!line)
{
printf("\nBuffer Allocation Error.");
exit(EXIT_FAILURE);
}
}
}
}
// Function to split a line into constituent commands
char **splitLine(char *line)
{
char **tokens = (char **)malloc(sizeof(char *) * 64);
char *token;
char delim[10] = " \t\n\r\a";
int pos = 0, bufsize = 64;
if (!tokens)
{
printf("\nBuffer Allocation Error.");
exit(EXIT_FAILURE);
}
token = strtok(line, delim);
while (token != NULL)
{
tokens[pos] = token;
pos ++;
if (pos >= bufsize)
{
bufsize += 64;
line = realloc(line, bufsize * sizeof(char *));
if (!line) // Buffer Allocation Failed
{
printf("\nBuffer Allocation Error.");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, delim);
}
tokens[pos] = NULL;
return tokens;
}
// Function Declarations
int myShell_path(char **args);
// Definitions
char *builtin_cmd[] = {"path"};
int (*builtin_func[]) (char **) = {&myShell_path}; // Array of function pointers for call from execShell
int numBuiltin() // Function to return number of builtin commands
{
return sizeof(builtin_cmd)/sizeof(char *);
}
// Builtin command definitions
int myShell_path(char **args){
char **argv;
char* val1 = "x";
char* val2 = "-";
//printf("%s",args[1]);
if(args[1]==NULL){
printf("Pathnames:\n");
}
else if(args[1]==val1){
printf("add to pathnanme\n");
}
else if(args[1]==val2){
//remove pathname
}
return 0;
}
// Function to create child process and run command
int myShellLaunch(char **args)
{
pid_t pid, wpid;
int status;
pid = fork();
if (pid == 0)
{
// The Child Process
if (execvp(args[0], args) == -1)
{
perror("myShell: ");
}
exit(EXIT_FAILURE);
}
else if (pid < 0)
{
//Forking Error
perror("myShell: ");
}
else
{
// The Parent Process
do
{
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
// Function to execute command from terminal
int execShell(char **args)
{
int ret;
if (args[0] == NULL)
{
// Empty command
return 1;
}
// Loop to check for builtin functions
for (int i=0; i< numBuiltin(); i++) // numBuiltin() returns the number of builtin functions
{
if(strcmp(args[0], builtin_cmd[i])==0) // Check if user function matches builtin function name
return (*builtin_func[i])(args); // Call respective builtin function with arguments
}
ret = myShellLaunch(args);
return ret;
}
// When myShell is called Interactively
int myShellInteract()
{
char *line;
char **args;
while(QUIT == 0)
{
printf("%s> ", SHELL_NAME);
line=readLine();
args=splitLine(line);
execShell(args);
free(line);
free(args);
}
return 1;
}
// When myShell is called with a Script as Argument
int myShellScript(char* filename)
{
printf("Received Script. Opening %s", filename);
FILE *fptr;
char line[200];
char **args;
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("\nUnable to open file.");
return 1;
}
else
{
printf("\nFile Opened. Parsing. Parsed commands displayed first.");
while(fgets(line, sizeof(line), fptr)!= NULL)
{
printf("\n%s", line);
args=splitLine(line);
execShell(args);
}
}
free(args);
fclose(fptr);
return 1;
}