1: /* $Header$ */ 2: 3: /* 4: * Author: Peter J. Nicklin 5: */ 6: 7: /* 8: * slssort() sorts list slslist according to comparison function compar(). 9: * compar() is to be called with two arguments and must return an integer 10: * greater than, equal to, or less than 0, depending on the lexicographic 11: * relationship between the two arguments. Returns integer YES if 12: * successful, otherwise NO if out of memory. 13: */ 14: #include "null.h" 15: #include "slslist.h" 16: #include "yesno.h" 17: 18: static int (*sscmp)(); /* string compare function */ 19: 20: slssort(compar, slslist) 21: int (*compar)(); /* compare two strings */ 22: SLSLIST *slslist; /* pointer to list head block */ 23: { 24: char *malloc(); /* memory allocator */ 25: int bpi; /* block pointer array index */ 26: int comparb(); /* compare 2 list blocks */ 27: SLSBLK **bp; /* pointer to block pointer array */ 28: SLSBLK *curblk; /* current list block */ 29: 30: if (slslist->nk <= 0) 31: return(YES); 32: else if ((bp = (SLSBLK **) malloc((unsigned)slslist->nk*sizeof(SLSBLK *)))==NULL) 33: { 34: warn("out of memory"); 35: return(NO); 36: } 37: for (bpi=0, curblk=slslist->head; curblk != NULL; bpi++, curblk=curblk->next) 38: bp[bpi] = curblk; 39: 40: sscmp = compar; 41: qsort((char *) bp, slslist->nk, sizeof(SLSBLK *), comparb); 42: 43: for (bpi=0, curblk=slslist->head=bp[bpi++]; bpi < slslist->nk; bpi++) 44: curblk = curblk->next = bp[bpi]; 45: curblk->next = NULL; 46: slslist->tail = curblk; 47: 48: free((char *) bp); 49: return(YES); 50: } 51: 52: 53: 54: /* 55: * comparb() compares key strings in 2 list blocks. Returns whatever 56: * sscmp() returns. sscmp() is a string compare function. 57: */ 58: 59: static int 60: comparb(b1, b2) 61: SLSBLK **b1; /* block pointer */ 62: SLSBLK **b2; /* block pointer */ 63: { 64: return(sscmp((*b1)->key, (*b2)->key)); 65: }