job.c (1165B)
1 #include <unistd.h> 2 #include <stdbool.h> 3 #include <string.h> 4 5 #include "90s.h" 6 7 typedef struct job { 8 pid_t pid; 9 char *command; 10 bool status; 11 struct job *next; 12 } job; 13 14 job *jobs = NULL; 15 16 int num_jobs() { 17 job *current = jobs; 18 int count = 0; 19 while (current != NULL) { 20 count++; 21 current = current->next; 22 } 23 return count; 24 } 25 26 int add_job(pid_t pid, char *command, bool status) { 27 job *current = jobs; 28 job *new_job = memalloc(sizeof(job)); 29 new_job->pid = pid; 30 char *buf = memalloc(strlen(command) + 1); 31 strcpy(buf, command); 32 new_job->command = buf; 33 new_job->status = status; 34 new_job->next = NULL; 35 if (current == NULL) { 36 jobs = new_job; 37 return 0; 38 } 39 int index = 1; 40 while (current->next != NULL) { 41 current = current->next; 42 index++; 43 } 44 current->next = new_job; 45 return index; 46 } 47 48 job *get_job(int index) { 49 job *current = jobs; 50 if (index == 0) { 51 return current; 52 } 53 if (index > num_jobs()) { 54 return NULL; 55 } 56 for (int i = 0; i < index; i++) { 57 current = current->next; 58 } 59 return current; 60 }