1: /* $Header$ */
   2: 
   3: /*
   4:  * Author: Peter J. Nicklin
   5:  */
   6: #include "null.h"
   7: #include "pdlist.h"
   8: #include "yesno.h"
   9: 
  10: /*
  11:  * pdinit() returns a pointer to the head block of a new project directory
  12:  * list, or dies if out of memory.
  13:  */
  14: PDLIST *
  15: pdinit()
  16: {
  17:     char *malloc();         /* memory allocator */
  18:     PDLIST *pdlist;         /* pointer to list head block */
  19: 
  20:     if ((pdlist = (PDLIST *) malloc(sizeof(PDLIST))) == NULL)
  21:         nomorecore();
  22:     pdlist->nd = 0;
  23:     pdlist->head = NULL;
  24:     return(pdlist);
  25: }
  26: 
  27: 
  28: 
  29: /*
  30:  * pdprepend() saves null-terminated project directory pathnames
  31:  * somewhere and inserts a pointer to the directory at the head of list
  32:  * pdlist. Returns a pointer to the pathname, or dies if out of
  33:  * memory.
  34:  */
  35: char *
  36: pdprepend(ppathname, pathname, project, pdlist)
  37:     char *ppathname;        /* project directory project pathname */
  38:     char *pathname;         /* project directory regular pathname */
  39:     char *project;          /* project directory's project */
  40:     PDLIST *pdlist;         /* pointer to list head block */
  41: {
  42:     char *malloc();         /* memory allocator */
  43:     char *strcpy();         /* string copy */
  44:     int strlen();           /* string length */
  45:     PDBLK *pdbptr;          /* pointer to list block */
  46: 
  47:     if (pdlist == NULL)
  48:         return(NULL);
  49:     if ((pdbptr = (PDBLK *) malloc(sizeof(PDBLK))) == NULL ||
  50:         (pdbptr->ppath = malloc((unsigned)(strlen(ppathname)+1))) == NULL ||
  51:         (pdbptr->rpath = malloc((unsigned)(strlen(pathname)+1))) == NULL)
  52:         nomorecore();
  53:     strcpy(pdbptr->rpath, pathname);
  54:     strcpy(pdbptr->ppath, ppathname);
  55:     pdbptr->project = project;
  56:     pdbptr->next = pdlist->head;
  57:     pdlist->head = pdbptr;
  58:     pdlist->nd++;
  59:     return(pdbptr->ppath);
  60: }
  61: 
  62: 
  63: 
  64: /*
  65:  * pdrm() removes a project directory list.
  66:  */
  67: void
  68: pdrm(pdlist)
  69:     PDLIST *pdlist;         /* pointer to list head block */
  70: {
  71:     PDBLK *nxtblk;          /* next list block */
  72: 
  73:     while (pdlist->head != NULL)
  74:         {
  75:         nxtblk = pdlist->head->next;
  76:         free(pdlist->head->ppath);
  77:         free(pdlist->head->rpath);
  78:         free((char *) pdlist->head);
  79:         pdlist->head = nxtblk;
  80:         }
  81:     free((char *) pdlist);
  82: }
  83: 
  84: 
  85: 
  86: /*
  87:  * pdsort() sorts list pdlist according to comparison function compar().
  88:  * compar() is to be called with two arguments and must return an integer
  89:  * greater than, equal to, or less than 0, depending on the lexicographic
  90:  * relationship between the two arguments.
  91:  */
  92: 
  93: static int (*sscmp)();          /* string compare function */
  94: 
  95: void
  96: pdsort(compar, pdlist)
  97:     int (*compar)();        /* compare two strings */
  98:     PDLIST *pdlist;         /* pointer to list head block */
  99: {
 100:     char *malloc();         /* memory allocator */
 101:     int bpi;            /* block pointer array index */
 102:     int comparb();          /* compare 2 list blocks */
 103:     PDBLK **bp;         /* pointer to block pointer array */
 104:     PDBLK *curblk;          /* current list block */
 105: 
 106:     if (pdlist->nd <= 0)
 107:         return;
 108:     else if ((bp = (PDBLK **) malloc((unsigned)pdlist->nd*sizeof(PDBLK *)))==NULL)
 109:         nomorecore();
 110:     for (bpi=0, curblk=pdlist->head; curblk != NULL; bpi++, curblk=curblk->next)
 111:         bp[bpi] = curblk;
 112: 
 113:     sscmp = compar;
 114:     qsort((char *) bp, pdlist->nd, sizeof(PDBLK *), comparb);
 115: 
 116:     for (bpi=0, curblk=pdlist->head=bp[bpi++]; bpi < pdlist->nd; bpi++)
 117:         curblk = curblk->next = bp[bpi];
 118:     curblk->next = NULL;
 119: 
 120:     free((char *) bp);
 121: }
 122: 
 123: 
 124: 
 125: /*
 126:  * comparb() compares project directory pathnames in 2 list blocks.
 127:  * Returns whatever sscmp() returns. sscmp() is a string compare function.
 128:  */
 129: static int
 130: comparb(b1, b2)
 131:     PDBLK **b1;         /* block pointer */
 132:     PDBLK **b2;         /* block pointer */
 133: {
 134:     return(sscmp((*b1)->ppath, (*b2)->ppath));
 135: }

Defined functions

comparb defined in line 129; used 2 times
Last modified: 1985-07-03
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1090
Valid CSS Valid XHTML 1.0 Strict