Commit a7172371 authored by Son Pham's avatar Son Pham

Lab 10.4 Completed

parent 80299cbd
No preview for this file type
......@@ -14,6 +14,7 @@
#define STRING_LENGTH 256 // String length
void execute_command(char* s);
void read_and_execute_commands(char* s);
int main(int argc, char* argv[]) {
// Print the fun ASCII shell image
......@@ -32,36 +33,55 @@ int main(int argc, char* argv[]) {
// Set up string for input from the user
char s[256];
int newline_count; // Count the newline
// While loop, constantly taking user input
while(1) {
printf("ishell> ");
// Read command line
fgets(s, sizeof(s), stdin); // Read 255 character upuntil the new line
// If it is a newline
if (s[0] == '\n') {
newline_count += 1;
// If newline is entered twice, show all files in directory
if (newline_count == 2) {
char print_dir[] = "/bin/ls";
execute_command(print_dir);
newline_count = 0;
}
} else {
// Read
read_and_execute_commands(s);
newline_count = 0;
}
}
}
// 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.
/**
* Read all the commands
*/
void read_and_execute_commands(char*s) {
char ** command_list = NULL;
char * command = strtok(s, ";");
int n_commands = 0; // Count the number of commands.
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");
}
while(command) {
command_list = realloc(command_list, sizeof(char*) *++n_commands);
command_list[n_commands-1] = command;
command = strtok(NULL, " \n");
}
// Allocate one extra element for the last NULL */
command_list = realloc(command_list, sizeof (char*) * (n_commands+1));
command_list[n_commands] = 0;
// Allocate one extra element for the last NULL */
command_list = realloc(command_list, sizeof (char*) * (n_commands+1));
command_list[n_commands] = 0;
// Execute each of the command in the command list
int i = 0;
while(command_list[i]) {
execute_command(command_list[i]);
i++;
}
// Execute each of the command in the command list
int i = 0;
while(command_list[i]) {
execute_command(command_list[i]);
i++;
}
}
......
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