home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / readnews / active.c next >
C/C++ Source or Header  |  1990-11-19  |  2KB  |  95 lines

  1. /*
  2.  * active file handling routines
  3.  *
  4.  * format of file:
  5.  *    <groupname> ' ' <#> ' ' <#> ' ' flag '\n'
  6.  *            (seq)    (low)
  7.  */
  8.  
  9. #include "defs.h"
  10.  
  11. static int lineno;
  12. static active    *alist;
  13.  
  14. static
  15. getline(f, g, d, d2)
  16. register FILE *f;
  17. char *g, *d, *d2;
  18. {
  19.     register int c;
  20.     register char *s;
  21.  
  22.     lineno++;
  23.     s = g;
  24.     while ((c = getc(f)) != ' ' && c != EOF)
  25.         *s++ = c;
  26.     *s = '\0';
  27.  
  28.     if (c != EOF) {
  29.         s = d;
  30.         while ((c = getc(f)) != EOF && isdigit(c))
  31.             *s++ = c;
  32.         *s = '\0';
  33.  
  34.         s = d2;
  35.         if (c == ' ')
  36.             while ((c = getc(f)) != EOF && isdigit(c))
  37.                 *s++ = c;
  38.         *s = '\0';
  39.  
  40.         if (c == ' ')
  41.             while ((c = getc(f)) != EOF && c != '\n')
  42.                 ;        /* eat flag */
  43.     }
  44.  
  45.     if (c != EOF && (c != '\n' || !*d || !*d2))
  46.         error("%s: bad format: line %d", "active", lineno);
  47.     return c != EOF;
  48. }
  49.  
  50.  
  51. /*
  52.  * build internal active file structure
  53.  */
  54. active *
  55. readactive()
  56. {
  57.     register FILE    *f;
  58.     register active    *ap, *last;
  59.     char gbuf[BUFSIZ / 2], dbuf[BUFSIZ / 4], dbuf2[BUFSIZ / 4];
  60.  
  61.     alist = last = NULL;
  62.     f = fopenf(ctlfile("active"), "r");
  63.     lineno = 0;
  64.     while (getline(f, gbuf, dbuf, dbuf2)) {
  65.         ap = NEW(active);
  66.         ap->a_name = newstr(gbuf);
  67.         ap->a_seq = atol(dbuf);
  68.         ap->a_low = atol(dbuf2);
  69.         ap->a_next = NULL;
  70.         if (!alist)
  71.             alist = ap;
  72.         else
  73.             last->a_next = ap;
  74.         last = ap;
  75.     }
  76.     fclose(f);
  77.     return alist;
  78. }
  79.  
  80.  
  81. /*
  82.  * return pointer to named group
  83.  */
  84. active *
  85. activep(grp)
  86. register char *grp;
  87. {
  88.     register active    *ap;
  89.  
  90.     for (ap = alist; ap; ap = ap->a_next)
  91.         if (CMP(grp, ap->a_name) == 0)
  92.             break;
  93.     return ap;
  94. }
  95.