/*********************************************************** * Name of program: UNIKernel * Authors: Ryan Wehrman and Sarah Van Norden * Description: This will be a custom kernel that will be able to run commands. **********************************************************/ /*This Kernel will have the functionality to run programs and not quit out until the user presses CTRL+D or 'quit'. I would like to make it so that they can put in a variety of inputs to quit out of the Kernel In order for this to work we will need to have some intercepts of the keys the user inputs. CTRL+C cannot close out this Kerenl The commands for quitting should me: 'quit', 'Quit', 'exit', 'Exit', 'Logout', 'logout' or any variation of those.*/ /* These are the included libraries. You may need to add more. */ #include #include #include #include #include #include #include #include /* Put any symbolic constants (defines) here */ #define True 1 #define False 0 char lastcommand[80]=""; /* This is the start of the shell. In here we will be able to run commands and as of 2 it is running basic commands from execvp*/ int main(int argc, char *argv[]) { while(1){ //Initilization up here int showpid[5]; pid_t pid; char inputstr[80]; char * arguments[10]; int argnumber = 0; int i; char lastcommand[80]; char * brokenString = NULL; printf("UNIShell$ "); fgets(inputstr, 80, stdin); inputstr[strcspn(inputstr, "\n")] = 0; //This mess is for Ryan as he types all four to quit out lol if (strncmp(inputstr, "quit", 4) == 0 || strncmp(inputstr, "Exit", 4) == 0 || strncmp(inputstr, "exit", 4) == 0 || strncmp(inputstr, "Quit", 4) == 0){ printf("Exiting!\n"); exit(0); } if (strncmp(inputstr,"lc",2)!=0){ strcpy(lastcommand,inputstr); } else { strcpy(inputstr,lastcommand); } // This will make BrokenString and split the input on whitespaces brokenString = strtok(inputstr, " " ); //printf(brokenString); //^^ Print for testing /* while (brokenString != NULL){ printf ("%s\n", brokenString); brokenString = strtok (NULL, " "); }*/ //^ Was an attempt at a check that did not respond the way I wanted to //Sets the memory to 0's when starting as it does not trash collect memset(arguments, 0, sizeof(arguments)); memset(showpid, 0, sizeof(showpid)); //This while loop will send the BrokenString and store it as an array so that we can call it later while(brokenString != NULL){ arguments[argnumber] = brokenString; brokenString = strtok(NULL, " "); argnumber++; } if (strncmp(inputstr, "cd", 2) == 0){ chdir(arguments[1]); setenv("PWD", arguments[1], 1); } /*if (strncmp(inputstr, "showpid", 7) == 0){ for (i=0; i<5; i++){ printf("%d \n", showpid[i]); } continue; }*/ /*arguments[0] = "ps"; arguments[1] = "-aux"; arguments[2] = NULL;*/ //^Some hardcoded arguments //Printing for test // printf("%s", arguments[0]); //This is a the fork that will find the child function and execute commands given there if ((pid = fork()) == 0){ execvp(arguments[0], arguments); if(strncmp(inputstr, "showpid", 7) != 0 || strncmp(inputstr, "cd", 2) != 0 || strncmp(inputstr, "..", 2) != 0 || strncmp(inputstr,"lc",2) != 0){ printf("Error: Command Not Found\n"); //Do we still need this print statement now that we have showpid and cd functionality?? } exit(0); } //Else will wait for the child to finish then go back to the start else{ showpid[0] = showpid[1]; showpid[1] = showpid[2]; showpid[2] = showpid[3]; showpid[3] = showpid[4]; showpid[4] = pid; waitpid(-1, NULL, 0); } if (strncmp(inputstr, "showpid", 7) == 0){ for (i=0; i<5; i++){ printf("%d \n", showpid[i]); } continue; } } }