From 6880277bb6cac3bb4662e5ef715dcd9667197d39 Mon Sep 17 00:00:00 2001 From: Skylar Date: Wed, 30 Mar 2022 00:13:17 +1300 Subject: [PATCH] Added jobs command --- ash.c | 83 +++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 25 deletions(-) diff --git a/ash.c b/ash.c index 4b0e345..c3b2168 100644 --- a/ash.c +++ b/ash.c @@ -121,30 +121,7 @@ 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); - - // while (amperProcesses[processCounter]!=0) - // { - // processCounter++; - // } - - // amperProcesses[processCounter] = processCounter; - - // printf("\b\b\b\b\b[%d] %d\nash> ", processCounter, getpid()); - // fflush(stdout); - - // int errorCode; - // waitpid(childID, &errorCode, 0); - - // printf("\n[%d] %s\nash> ", processCounter, keepCommand); - // exit(0); - //} } - else { return; @@ -412,13 +389,69 @@ char ***createPipeArgInput(char **userArgs, int amper, char *constantFullCommand executeWithPipes(pipedArgs, amper, maxPipes, constantFullCommand, jobCommandsAmper, jobIDs); } -void jobCommand(uid_t *jobIDs, char **jobCommandsAmper) +char fileRead(pid_t jobToCheck) +{ + FILE *statusFile; + char fileName[1000]; + sprintf(fileName, "/proc/%d/stat", jobToCheck); + + statusFile = fopen(fileName, "r"); + + char status; + fscanf(statusFile, "%*s %*[^)]) %c", &status); + + fclose(statusFile); + + return status; +} + +char *whichState(char stateChar) +{ + char *jobStatus = calloc(16384, 1); + + switch(stateChar) + { + case 'S': + case 'D': + jobStatus = "Sleeping"; + break; + + case 'R': + jobStatus = "Runnable"; + break; + + case 'T': + case 't': + jobStatus = "Stopped"; + break; + + case 'I': + jobStatus = "Idle"; + break; + + case 'Z': + jobStatus = "Zombie"; + break; + + default: + jobStatus = "Unknown"; + break; + } + + return jobStatus; +} + +void jobCommand(pid_t *jobIDs, char **jobCommandsAmper) { + for (int i = 1; i <= processCounter; i++) { if (jobIDs[i] != 0) { - printf("[%d] %d %s\n", i, jobIDs[i], jobCommandsAmper[i]); + char stateCharacter = fileRead(jobIDs[i]); + char *stateOfJob = calloc(1000, 1); + stateOfJob = whichState(stateCharacter); + printf("[%d] <%s> %s\n", i, stateOfJob, jobCommandsAmper[i]); } } }