C programming

Wernerrab
major2.txt

#include "header.h" int main() { while(1) { char args[100][100]; // Semicolon seperated input goes here char str[512]; // Read in input into here pid_t pid; // For executing commands with a child process int usedChild = 0; // Flag for checking if child process was used for command int numArrEle = 0; // For keeping track of how many space sepearted strings there are in a command // Ask and take in user input printf("prompt> "); fgets(str, 513, stdin); // Removes the newline character from fgets int ln = strlen(str)-1; if (str[ln] == '\n') { str[ln] = '\0'; } StoreInputToChar2DArray(str, args, ';'); // Seperate semicolon seperated input // Run loop based on number of commands and execute each one for(int i = 0; i < 100; i++) { // Don't run command if it's whitespace if(strcmp(args[i], "\0") == 0) { continue; } char args2[100][100]; // Passed into the exec() functions, after converting to char pointer array char* args3[100]; // This is what is passed into the exec() functions StoreInputToChar2DArray2(args[i], args2, ' '); // Seperate space seperated input // Store 2d array into the char pointer array for (int j = 0; j < 100; j++) { if (strcmp(args2[j], "\0") == 0) // Let's us know when to stop searching array { numArrEle = j; args3[j] = NULL; break; } args3[j] = args2[j]; } // Run built in "cd" command if(strcmp(args3[0], "cd") == 0) { if(args3[1] == NULL) { char argument[50] = "/home/"; char* name = getenv("USER"); strcat(argument, name); if(chdir(argument) == -1) { perror("chdir error"); } } else { if(chdir(args3[1]) == -1) { perror("chdir error"); } } } // Run I/O redirection else if(numArrEle > 2 && (strcmp(args3[numArrEle-2], ">") == 0 || strcmp(args3[numArrEle-2], "<") == 0)) { // Output redirection if(strcmp(args3[numArrEle-2], ">") == 0) { // Open file descriptor int fd = open(args3[numArrEle-1], O_RDWR|O_CREAT, 0644); int stdOut = dup(1); if(fd == -1 || stdOut == -1) { perror("open error"); exit(EXIT_FAILURE); } dup2(fd, 1); close(fd); args3[numArrEle-2] = NULL; // Now run command and output to file pid = fork(); usedChild = 1; if (pid > 0) { // Wait for child process to finish before prompting user for input again wait((int*)0); } else if (pid == 0) { // Command is executed using execvp() if (execvp(args3[0], args3) == -1) { printf("%s: command not found\n", args2[0]); break; // Break child process so it won't loop } } else { perror("fork error\n"); } // Restore stdout dup2(stdOut, 1); close(stdOut); } // Input redirection else if(strcmp(args3[numArrEle-2], "<") == 0) { // Open file descriptor int fd = open(args3[numArrEle-1], O_RDWR|O_TRUNC|O_CREAT, 0644); int stdIn = dup(0); if(fd == -1 || stdIn == -1) { perror("open error"); exit(EXIT_FAILURE); } dup2(fd, 0); close(fd); args3[numArrEle-2] = NULL; // Now run command using file as input pid = fork(); usedChild = 1; if (pid > 0) { // Wait for child process to finish before prompting user for input again wait((int*)0); } else if (pid == 0) { // Command is executed using execvp() if (execvp(args3[0], args3) == -1) { printf("%s: command not found\n", args2[0]); break; // Break child process so it won't loop } } else { perror("fork error\n"); } // Restore stdout dup2(stdIn, 0); close(stdIn); } } // Run all other commands else { pid = fork(); usedChild = 1; if (pid > 0) { // Wait for child process to finish before prompting user for input again wait((int*)0); } else if (pid == 0) { // Command is executed using execvp() if (execvp(args3[0], args3) == -1) { printf("%s: command not found\n", args2[0]); break; // Break child process so it won't loop } } else { perror("fork error\n"); } } // Clear arrays int strSize = strlen(args[i]); ClearCharArrays(args2,args3,args[i],strSize); } if (pid == 0 && usedChild == 1) { usedChild = 0; break; } memset(str, 0, sizeof(str)); } return 0; }