#include #include #include #include "../global.h" #include "../custom.h" #include "shared_util.h" #include "shared_lock.h" #include "shared_cs_util.h" #include "shared_super_list.h" #include "shared_strtrm.h" #define MAX_LINE 200 /* maximum length of a line in the ROSTER_FNAME text file */ COURSE_LIST_NODE * sort_course_list(COURSE_LIST_NODE *head) { COURSE_LIST_NODE *q, *tail, *p, *r; if (head != NULL) { tail = head; while (tail->next) { q = tail->next; if (strcmp(q->crs, head->crs) < 0) { tail->next = q->next; q->next = head; head = q; } else { r = head; p = r->next; while (strcmp(q->crs, p->crs) > 0) { r = p; p = r->next; } if (q == p) tail = q; else { tail->next = q->next; q->next = p; r->next = q; } } } } return head; } /* build course list */ COURSE_LIST_NODE * build_course_list(FILE *fp) { COURSE_LIST_NODE *head = 0; COURSE_LIST_NODE *ptr, *current = 0; char line[MAX_LINE +1]; while(fgets (line, MAX_LINE, fp)) { strtrm(line); /* shared_strtrm.c */ ptr = (COURSE_LIST_NODE *)malloc(sizeof(COURSE_LIST_NODE)); if(!ptr) cs_critical_error(ERR_MALLOC_FAILED, ""); strncpy(ptr->crs, line, MAX_PATH); ptr->next = 0; if(!head) { head = ptr; } else current->next = ptr; current = ptr; } return head; } void super_free_course_list(COURSE_LIST_NODE *head) { COURSE_LIST_NODE *ptr; while (head) { ptr = head->next; free (head); head = ptr; } } COURSE_LIST_NODE * excluding_delete_course_list(char *file, char *crs) { FILE *fp; char line[MAX_PATH +1]; COURSE_LIST_NODE *ptr, *head = 0, *current =0; fp = fopen(file, "r"); if(!fp) return head; get_shared_lock(fileno(fp), 1); while(fgets (line, MAX_PATH, fp)) { strtrm(line); /* shared_strtrm.c */ if(strcmp(line, crs) != 0) { ptr = (COURSE_LIST_NODE *)malloc(sizeof(COURSE_LIST_NODE)); if(!ptr) cs_critical_error(ERR_MALLOC_FAILED, ""); strncpy(ptr->crs, line, MAX_PATH); ptr->next = 0; if(!head) { head = ptr; } else current->next = ptr; current = ptr; } } release_lock(fileno(fp)); fclose(fp); return head; } void write_to_course_file(COURSE_LIST_NODE *head, char *file) { COURSE_LIST_NODE *ptr; FILE *fp; fp = fopen(file, "w"); if(!fp) return; get_exclusive_lock(fileno(fp), 1); for(ptr = head; ptr; ptr = ptr->next) fprintf(fp, "%s\n", ptr->crs); release_lock(fileno(fp)); fclose(fp); }