diff --git a/ash.c b/ash.c index 6dd80bf..3bb0d54 100644 --- a/ash.c +++ b/ash.c @@ -19,7 +19,6 @@ char *readLine(void) while (1) { - char in = fgetc(stdin); if (in == EOF || in == '\n') @@ -46,6 +45,32 @@ char *readLine(void) } } +void addToJobs(char **jobCommandsAmper, char *constantFullCommand, pid_t *jobIDs, pid_t parseID) +{ + if (jobsRunning == 0) + { + processCounter = 1; + jobsRunning++; + jobCommandsAmper[processCounter] = strdup(constantFullCommand); + jobIDs[processCounter] = parseID; + printf("[%d] %d\n", processCounter, parseID); + } + else + { + jobsRunning++; + processCounter++; + int countdown = processCounter; + while (jobIDs[countdown] == '\0') + { + countdown--; + } + countdown++; + jobCommandsAmper[countdown] = strdup(constantFullCommand); + jobIDs[countdown] = parseID; + printf("[%d] %d\n", processCounter, parseID); + } +} + void splitToArgs(char *fullLine, char **userArgs) { @@ -89,28 +114,7 @@ void executeAndDontWait(char **userArgs, char *fullCommand, char **jobCommandsAm if (childID != 0) { - if (jobsRunning == 0) - { - processCounter = 1; - jobsRunning++; - jobCommandsAmper[processCounter] = strdup(fullCommand); - jobIDs[processCounter] = childID; - printf("[%d] %d\n", processCounter, childID); - } - else - { - jobsRunning++; - processCounter++; - int countdown = processCounter; - while (jobIDs[countdown] == '\0') - { - countdown--; - } - countdown++; - jobCommandsAmper[countdown] = strdup(fullCommand); - jobIDs[countdown] = childID; - printf("[%d] %d\n", processCounter, childID); - } + addToJobs(jobCommandsAmper, fullCommand, jobIDs, childID); } if (childID == 0) @@ -118,28 +122,27 @@ void executeAndDontWait(char **userArgs, char *fullCommand, char **jobCommandsAm execvp(userArgs[0], userArgs); exit(0); - // int processCounter = 1; - - // char *keepCommand = calloc(16384, 1); - // strcpy(keepCommand, fullCommand); + // int processCounter = 1; - // while (amperProcesses[processCounter]!=0) - // { - // processCounter++; - // } + // char *keepCommand = calloc(16384, 1); + // strcpy(keepCommand, fullCommand); - // amperProcesses[processCounter] = processCounter; + // while (amperProcesses[processCounter]!=0) + // { + // processCounter++; + // } - // printf("\b\b\b\b\b[%d] %d\nash> ", processCounter, getpid()); - // fflush(stdout); + // amperProcesses[processCounter] = processCounter; - // int errorCode; - // waitpid(childID, &errorCode, 0); + // printf("\b\b\b\b\b[%d] %d\nash> ", processCounter, getpid()); + // fflush(stdout); - // printf("\n[%d] %s\nash> ", processCounter, keepCommand); - // exit(0); - //} + // int errorCode; + // waitpid(childID, &errorCode, 0); + // printf("\n[%d] %s\nash> ", processCounter, keepCommand); + // exit(0); + //} } else @@ -153,7 +156,7 @@ void jobStates(pid_t *jobIDs, char **jobCommandsAmper) for (int i = 0; i <= processCounter; i++) { - if (jobIDs[i]==0) + if (jobIDs[i] == 0) { continue; } @@ -167,39 +170,37 @@ void jobStates(pid_t *jobIDs, char **jobCommandsAmper) jobsRunning--; jobIDs[i] = 0; } - - } } } void historyCommand(char *commandType, char **historyOfUser, char *OGdirectory, char **jobsCommandAmper, pid_t *jobIDs) { - if (historyOfUser[0]==NULL) + if (historyOfUser[0] == NULL) { printf("No history\n"); return; } - - if (commandType==NULL && historyAmount < 11) + + if (commandType == NULL && historyAmount < 11) { for (int i = 1; i <= historyAmount; i++) { - printf("%3d: %s\n", i, historyOfUser[i-1]); + printf("%3d: %s\n", i, historyOfUser[i - 1]); - if (historyOfUser[i]==NULL) + if (historyOfUser[i] == NULL) { break; } } } - else if (commandType==NULL) + else if (commandType == NULL) { - for (int i = historyAmount-9; i <= historyAmount; i++) + for (int i = historyAmount - 9; i <= historyAmount; i++) { - printf("%3d: %s\n", i, historyOfUser[i-1]); + printf("%3d: %s\n", i, historyOfUser[i - 1]); - if (historyOfUser[i]==NULL) + if (historyOfUser[i] == NULL) { break; } @@ -218,23 +219,20 @@ void historyCommand(char *commandType, char **historyOfUser, char *OGdirectory, char *fullCommandWanted = calloc(16384, 1); // if doesnt exist, return - if (historyOfUser[histNum-1]==NULL) + if (historyOfUser[histNum - 1] == NULL) { printf("That value has no history!\n"); return; } - strcpy(fullCommandWanted, historyOfUser[histNum-1]); + strcpy(fullCommandWanted, historyOfUser[histNum - 1]); // Running the history command char **historyArgs = calloc(16384, 1); splitToArgs(fullCommandWanted, historyArgs); - - runCommand(historyArgs, fullCommandWanted, OGdirectory, historyOfUser, jobsCommandAmper, jobIDs); + runCommand(historyArgs, fullCommandWanted, OGdirectory, historyOfUser, jobsCommandAmper, jobIDs); } - - } void addToHistory(char *userString, char **historyOfUser) @@ -251,13 +249,13 @@ void addToHistory(char *userString, char **historyOfUser) int amperCheck(char **userArgs) { int count = 0; - while (userArgs[count]!=NULL) + while (userArgs[count] != NULL) { count++; } - if (strcmp(userArgs[count-1], "&") == 0) + if (strcmp(userArgs[count - 1], "&") == 0) { - userArgs[count-1] = '\0'; + userArgs[count - 1] = '\0'; return 1; } else @@ -269,9 +267,9 @@ int amperCheck(char **userArgs) int pipeCheck(char **userArgs) { int count = 1; - while (userArgs[count]!=NULL) + while (userArgs[count] != NULL) { - if (strcmp(userArgs[count-1], "|") == 0) + if (strcmp(userArgs[count - 1], "|") == 0) { return 1; } @@ -279,87 +277,97 @@ int pipeCheck(char **userArgs) } return 0; - } +void shutOff(int pipeNum, int fildes[pipeNum][2]) +{ + for (int i = 0; i < pipeNum; i++) + { + close(fildes[i][0]); + close(fildes[i][1]); + } +} -void executeWithPipes(char ***userArgs, int amper, int pipeNum, char *constantFullCommand) +void executeWithPipes(char ***userArgs, int amper, int pipeNum, char *fullCommand, char **jobCommandsAmper, pid_t *jobIDs) { - pid_t child1, child2; - int fildes[2]; + pid_t *allChildren = calloc(16384, 1); - pipeNum--; + int fildes[pipeNum][2]; - if (pipeNum == 0) + for (int i = 0; i < pipeNum; i++) { - execvp(userArgs[pipeNum][0], userArgs[pipeNum]); - exit(0); + pipe(fildes[i]); } - //pipeNum = 1; - pipe(fildes); - child1 = fork(); - if (child1 == 0) + allChildren[0] = fork(); + + if (allChildren[0] == 0) { - // printf("hello1v2\n"); - // printf("%s\n", userArgs[pipeNum-1][0]); - // printf("%s\n", userArgs[pipeNum-1][1]); - dup2(fildes[1], STDOUT_FILENO); - close(fildes[0]); - close(fildes[1]); + dup2(fildes[0][1], STDOUT_FILENO); + shutOff(pipeNum, fildes); + execvp(userArgs[0][0], userArgs[0]); + exit(0); + } - executeWithPipes(userArgs, amper, pipeNum, constantFullCommand); + for (int i = 1; i < pipeNum; i++) + { + allChildren[i] = fork(); + if (allChildren[i] == 0) + { + dup2(fildes[i - 1][0], STDIN_FILENO); + dup2(fildes[i][1], STDOUT_FILENO); + shutOff(pipeNum, fildes); + execvp(userArgs[i][0], userArgs[i]); + exit(0); + } } - else - { - int errorMsg; - dup2(fildes[0], STDIN_FILENO); - close(fildes[0]); - close(fildes[1]); - waitpid(child1, &errorMsg, 0); + allChildren[pipeNum] = fork(); + if (allChildren[pipeNum] == 0) + { + dup2(fildes[pipeNum - 1][0], STDIN_FILENO); + shutOff(pipeNum, fildes); execvp(userArgs[pipeNum][0], userArgs[pipeNum]); exit(0); - - // Creating a second child under the same parent - // child2 = fork(); - - // if (child2 == 0) - // { - // dup2(fildes[0], STDIN_FILENO); - // close(fildes[0]); - // close(fildes[1]); - // // printf("hello2\n"); - // // printf("%s\n", userArgs[pipeNum][0]); - // // printf("%s\n", userArgs[pipeNum][1]); - // execvp(userArgs[pipeNum][0], userArgs[pipeNum]); - // // printf("hello2v2\n"); - // exit(0); - // } - // else - // { - // // printf("hello3\n"); - - // close(fildes[0]); - // close(fildes[1]); - // int errorMsg; - // waitpid(child2, &errorMsg, 0); - // } - } + int errCode; + shutOff(pipeNum, fildes); + if (amper == 0) + { + for (int i = 0; i < pipeNum; i++) + { + waitpid(allChildren[i], &errCode, 0); + } + } + else + { + pid_t amperFork = fork(); + if (amperFork == 0) + { + for (int i = 0; i < pipeNum; i++) + { + waitpid(allChildren[i], &errCode, 0); + } + exit(0); + } + else + { + addToJobs(jobCommandsAmper, fullCommand, jobIDs, amperFork); + } + } } char ***createPipeArgInput(char **userArgs, int amper, char *constantFullCommand, char **jobCommandsAmper, pid_t *jobIDs) { int buffsize1 = 10; int buffsize2 = 10; - char ***pipedArgs = malloc(buffsize1*sizeof(char **)); - *pipedArgs = malloc(buffsize2*sizeof(char *)); + char ***pipedArgs = malloc(buffsize1 * sizeof(char **)); + *pipedArgs = malloc(buffsize2 * sizeof(char *)); int count = 0; int i = 0; int j = 0; @@ -379,78 +387,33 @@ char ***createPipeArgInput(char **userArgs, int amper, char *constantFullCommand if (count >= buffsize1) { buffsize1 += buffsize1; - pipedArgs = realloc(pipedArgs, buffsize1*sizeof(char **)); + pipedArgs = realloc(pipedArgs, buffsize1 * sizeof(char **)); } i = 0; buffsize2 = 10; - pipedArgs[count] = malloc(buffsize2*sizeof(char*)); + pipedArgs[count] = malloc(buffsize2 * sizeof(char *)); continue; - } - + // printf("adding %s at %d to %d", userArgs[j], i, count); - pipedArgs[count][i] = malloc(sizeof(char)*strlen(userArgs[j])); + pipedArgs[count][i] = malloc(sizeof(char) * strlen(userArgs[j])); strcpy(pipedArgs[count][i], userArgs[j]); j++; i++; if (i >= buffsize2) { buffsize2 += buffsize2; - pipedArgs[count] = realloc(pipedArgs[count], buffsize2*sizeof(char *)); + pipedArgs[count] = realloc(pipedArgs[count], buffsize2 * sizeof(char *)); } - } pipedArgs[count][i] = NULL; - int numOfPipes = 0; - - - - pid_t runningPipeCommand = fork(); - - if (runningPipeCommand == 0) - { - executeWithPipes(pipedArgs, amper, maxPipes + 1, constantFullCommand); - } - else - { - if (amper == 0) - { - int errorMsg; - waitpid(runningPipeCommand, &errorMsg, 0); - } - else - { - if (jobsRunning == 0) - { - processCounter = 1; - jobsRunning++; - jobCommandsAmper[processCounter] = strdup(constantFullCommand); - jobIDs[processCounter] = runningPipeCommand; - printf("[%d] %d\n", processCounter, runningPipeCommand); - } - else - { - jobsRunning++; - processCounter++; - int countdown = processCounter; - while (jobIDs[countdown] == '\0') - { - countdown--; - } - countdown++; - jobCommandsAmper[countdown] = strdup(constantFullCommand); - jobIDs[countdown] = runningPipeCommand; - printf("[%d] %d\n", processCounter, runningPipeCommand); - } - } - } - + executeWithPipes(pipedArgs, amper, maxPipes, constantFullCommand, jobCommandsAmper, jobIDs); } -void runCommand(char **userArgs, char *constantFullCommand, char *OGdirectory, -char **historyCommands, char **jobCommandsAmper, pid_t *jobIDs) +void runCommand(char **userArgs, char *constantFullCommand, char *OGdirectory, + char **historyCommands, char **jobCommandsAmper, pid_t *jobIDs) { // Checking if ampersand @@ -529,7 +492,7 @@ int main() // Taking a constant version of full line char *constantFullCommand = calloc(16384, 1); - strcpy(constantFullCommand, fullCommand); + strcpy(constantFullCommand, fullCommand); // Resetting an args 2d array char **userArgs = calloc(16384, 1); @@ -539,7 +502,6 @@ int main() runCommand(userArgs, constantFullCommand, OGdirectory, historyCommands, jobsCommandAmper, jobIDs); free(userArgs); - } return 0;