home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / src / cmd / uucp / anlwrk.c next >
Encoding:
C/C++ Source or Header  |  1979-01-10  |  3.2 KB  |  172 lines

  1. #include "uucp.h"
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4.  
  5.  
  6. #define LLEN 10
  7. #define SAME 0
  8.  
  9. /*******
  10.  *    anlwrk(file, wvec)    create a vector of command arguments
  11.  *    char *file, **wvec;
  12.  *
  13.  *    return codes:
  14.  *        0  -  no more work in this file
  15.  *        positive number  -  number of arguments
  16.  */
  17.  
  18. anlwrk(file, wvec)
  19. char *file, **wvec;
  20. {
  21.     static char str[BUFSIZ];
  22.     static FILE *fp = NULL;
  23.  
  24.     if (file[0] == '\0')
  25.         return(0);
  26.     if (fp == NULL) {
  27.         fp = fopen(file, "r");
  28.         if (fp == NULL)
  29.             return(0);
  30.     }
  31.  
  32.     if (fgets(str, BUFSIZ, fp) == NULL) {
  33.         fclose(fp);
  34.         unlink(file);
  35.         file[0] = '\0';
  36.         fp = NULL;
  37.         return(0);
  38.     }
  39.  
  40.     return(getargs(str, wvec));
  41. }
  42.  
  43.  
  44. /***
  45.  *    iswrk(file, reqst, dir, pre)
  46.  *    char *file, *reqst, *dir, *pre;
  47.  *
  48.  *    iswrk  -  this routine will check the work list (list).
  49.  *    If it is empty or the present work is exhausted, it
  50.  *    will call gtwrk to generate a new list.
  51.  *    The "reqst" field will be the string "chk" or "get" to
  52.  *    check for work, or get the next work file respectively.
  53.  *
  54.  *    return codes:
  55.  *        0  -  no more work (or some error)
  56.  *        1  -  there is work
  57.  */
  58.  
  59. iswrk(file, reqst, dir, pre)
  60. char *file, *reqst, *dir, *pre;
  61. {
  62.     static char **listp, *list[LLEN];
  63.  
  64.     if (listp == NULL || *listp == NULL || listp > (list + LLEN)
  65.       || !prefix(pre, *listp)) {
  66.         int i;
  67.         for (i = 0, listp = list; i < LLEN; i++) {
  68.             if (*listp != NULL)
  69.                 free(*listp);
  70.             *listp++ = NULL;
  71.         }
  72.         if (gtwrk(dir, pre, listp = list, LLEN) != 0)
  73.             /* alloc error */
  74.             return(0);
  75.     }
  76.  
  77.     if (*listp == NULL)
  78.         return(0);
  79.  
  80.     if (strcmp(reqst, "get") == SAME)
  81.         sprintf(file, "%s/%s", dir, *listp++);
  82.     return(1);
  83. }
  84.  
  85.  
  86. /***
  87.  *    gtwvec(file, dir, wkpre, wrkvec)    get work vector 
  88.  *    char *file, *dir, *wkpre, **wrkvec;
  89.  *
  90.  *    return codes:
  91.  *        positive number  -  number of arguments
  92.  *        0 -  no arguments - fail
  93.  */
  94.  
  95. gtwvec(file, dir, wkpre, wrkvec)
  96. char *file, *dir, *wkpre, **wrkvec;
  97. {
  98.     int nargs;
  99.  
  100.     while ((nargs = anlwrk(file, wrkvec)) == 0) {
  101.         if (!iswrk(file, "get", dir, wkpre))
  102.             return(0);
  103.     }
  104.     return(nargs);
  105. }
  106.  
  107. #define ANYREAD 04
  108.  
  109. /***
  110.  *    gtwrk(dir, pre, list, llen)
  111.  *    char *dir, *pre, **list;
  112.  *    int llen;
  113.  *
  114.  *    gtwrk  -  this routine will build a sorted list
  115.  *    of files in a directory.
  116.  *    "dir" is the directory name to search for file names
  117.  *    beginning with the prefix (pre).
  118.  *    "list" is the pointer to the list and "llen" is the
  119.  *    length of the list.
  120.  *
  121.  *    return codes:  0  |  FAIL
  122.  */
  123.  
  124. gtwrk(dir, pre, list, llen)
  125. char *dir, *pre, **list;
  126. int llen;
  127. {
  128.     struct stat s;
  129.     char filename[NAMESIZE], *p;
  130.     char **first, **last;
  131.     FILE *pdir;
  132.     extern int compar();
  133.     extern char *calloc();
  134.  
  135.     first = last = list;
  136.     if ((pdir = fopen(dir, "r")) == NULL)
  137.         return(FAIL);
  138.     while (gnamef(pdir, filename)) {
  139.         if (!prefix(pre, filename))
  140.             continue;
  141.         if (stat(filename, &s) == -1)
  142.             continue;
  143.         if ((s.st_mode & ANYREAD) == 0)
  144.             continue;
  145.         if ((p = calloc(strlen(filename) + 1, sizeof (char))) == NULL)
  146.             return(FAIL);
  147.  
  148.         strcpy(p, filename);
  149.         if ((last - first) < llen)
  150.             *last++ = p;
  151.     }
  152.  
  153.     fclose(pdir);
  154.     qsort(first, last - first, sizeof *last, compar);
  155.     return(0);
  156. }
  157.  
  158.  
  159. /***
  160.  *    compar(p1, p2)
  161.  *    char **p1, **p2;
  162.  *
  163.  *    compar  -  this routine is used by qsort.
  164.  *
  165.  */
  166.  
  167. compar(p1, p2)
  168. char **p1, **p2;
  169. {
  170.     return(strcmp(*p1, *p2));
  171. }
  172.