Commit 80299cbd authored by Son Pham's avatar Son Pham

Lab 10.3 completed

parent 21288949
No preview for this file type
......@@ -13,6 +13,8 @@
#define STRING_LENGTH 256 // String length
void execute_command(char* s);
int main(int argc, char* argv[]) {
// Print the fun ASCII shell image
printf("\n");
......@@ -38,35 +40,63 @@ int main(int argc, char* argv[]) {
// Read command line
fgets(s, sizeof(s), stdin); // Read 255 character upuntil the new line
// Parse command line into a list of argument
char ** param_list = NULL;
char * token = strtok(s, " \n");
int n_args = 0; // Counting the number of arguments in the string
// Separate the command lines by semi-colon first.
char ** command_list = NULL;
char * command = strtok(s, ";");
int n_commands = 0; // Count the number of commands.
while(token) {
param_list = realloc(param_list, sizeof(char*) *++n_args); // TODO: Wrapper
param_list[n_args-1] = token;
token = strtok(NULL, " \n");
while(command) {
command_list = realloc(command_list, sizeof(char*) *++n_commands);
// TODO: Wrapper for realloc
command_list[n_commands-1] = command;
command = strtok(NULL, " \n");
}
// Allocate one extra element for the last NULL */
param_list = realloc(param_list, sizeof (char*) * (n_args+1));
param_list[n_args] = 0;
// Allocate one extra element for the last NULL */
command_list = realloc(command_list, sizeof (char*) * (n_commands+1));
command_list[n_commands] = 0;
// Spawn a child to execute command line
pid_t pid;
int status;
// Execute each of the command in the command list
int i = 0;
while(command_list[i]) {
execute_command(command_list[i]);
i++;
}
}
}
/**
* Execute the command by parsing the string
*/
void execute_command(char* s) {
// Parse each command line into a list of argument
char ** param_list = NULL;
char * token = strtok(s, " \n");
int n_args = 0; // Counting the number of arguments in the string
while(token) {
param_list = realloc(param_list, sizeof(char*) *++n_args); // TODO: Wrapper
param_list[n_args-1] = token;
token = strtok(NULL, " \n");
}
// Allocate one extra element for the last NULL */
param_list = realloc(param_list, sizeof (char*) * (n_args+1));
param_list[n_args] = 0;
// Spawn a child to execute command line
pid_t pid;
int status;
pid = Fork();
if (pid == 0) { // child process exec
int value = execv(param_list[0], param_list); // TODO: WRAPPER
} else { // Parent process
waitpid(pid, &status, WEXITSTATUS(status));
if (status == 0) {
printf("[ishell: program terminated successfully]\n");
} else {
printf("[ishell: program terminated abnormally] [%d]\n");
}
pid = Fork();
if (pid == 0) { // child process exec
int value = execv(param_list[0], param_list); // TODO: WRAPPER for execv
} else { // Parent process
waitpid(pid, &status, WEXITSTATUS(status));
if (status == 0) {
printf("[ishell: program terminated successfully]\n");
} else {
printf("[ishell: program terminated abnormally] [%d]\n");
}
}
}
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment