|
|
1.1 ! root 1: /* $Header: slsort.c,v 1.2 85/03/18 13:19:42 nicklin Exp $ */ ! 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 <stdio.h> ! 15: #include "null.h" ! 16: #include "slist.h" ! 17: #include "yesno.h" ! 18: ! 19: extern char *PGN; /* program name */ ! 20: ! 21: static int (*sscmp)(); /* string compare function */ ! 22: ! 23: slsort(compar, slist) ! 24: int (*compar)(); /* compare two strings */ ! 25: SLIST *slist; /* pointer to list head block */ ! 26: { ! 27: char **kp; /* pointer to key pointer array */ ! 28: char *malloc(); /* memory allocator */ ! 29: char **skp; /* ptr to start of key ptr array */ ! 30: int comparb(); /* compare 2 list blocks */ ! 31: SLBLK *curblk; /* current list block */ ! 32: ! 33: if (slist->nk <= 0) ! 34: return(YES); ! 35: else if ((skp = (char **) malloc((unsigned)slist->nk*sizeof(char *))) == NULL) ! 36: { ! 37: if (*PGN != '\0') ! 38: fprintf(stderr, "%s: ", PGN); ! 39: fprintf(stderr, "out of memory\n"); ! 40: return(NO); ! 41: } ! 42: for (kp = skp, curblk = slist->head; curblk != NULL; kp++, curblk = curblk->next) ! 43: *kp = curblk->key; ! 44: ! 45: sscmp = compar; ! 46: qsort((char *) skp, slist->nk, sizeof(char *), comparb); ! 47: ! 48: for (kp = skp, curblk = slist->head; curblk != NULL; kp++, curblk = curblk->next) ! 49: curblk->key = *kp; ! 50: ! 51: free((char *) skp); ! 52: return(YES); ! 53: } ! 54: ! 55: ! 56: ! 57: /* ! 58: * comparb() compares key strings in 2 list blocks. Returns whatever ! 59: * sscmp() returns. sscmp() is a string compare function. ! 60: */ ! 61: static int ! 62: comparb(s1, s2) ! 63: char **s1; /* string pointer */ ! 64: char **s2; /* string pointer */ ! 65: { ! 66: return(sscmp(*s1, *s2)); ! 67: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.