1: /* $Header$ */ 2: 3: /* 4: * Author: Peter J. Nicklin 5: */ 6: 7: /* 8: * slsort() sorts list slist 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 "slist.h" 16: #include "yesno.h" 17: 18: static int (*sscmp)(); /* string compare function */ 19: 20: slsort(compar, slist) 21: int (*compar)(); /* compare two strings */ 22: SLIST *slist; /* pointer to list head block */ 23: { 24: char **kp; /* pointer to key pointer array */ 25: char *malloc(); /* memory allocator */ 26: char **skp; /* ptr to start of key ptr array */ 27: int comparb(); /* compare 2 list blocks */ 28: SLBLK *curblk; /* current list block */ 29: 30: if (slist->nk <= 0) 31: return(YES); 32: else if ((skp = (char **) malloc((unsigned)slist->nk*sizeof(char *))) == NULL) 33: { 34: warn("out of memory"); 35: return(NO); 36: } 37: for (kp = skp, curblk = slist->head; curblk != NULL; kp++, curblk = curblk->next) 38: *kp = curblk->key; 39: 40: sscmp = compar; 41: qsort((char *) skp, slist->nk, sizeof(char *), comparb); 42: 43: for (kp = skp, curblk = slist->head; curblk != NULL; kp++, curblk = curblk->next) 44: curblk->key = *kp; 45: 46: free((char *) skp); 47: return(YES); 48: } 49: 50: 51: 52: /* 53: * comparb() compares key strings in 2 list blocks. Returns whatever 54: * sscmp() returns. sscmp() is a string compare function. 55: */ 56: static int 57: comparb(s1, s2) 58: char **s1; /* string pointer */ 59: char **s2; /* string pointer */ 60: { 61: return(sscmp(*s1, *s2)); 62: }