home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume26 / feedpipe / part01 / readcfg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-14  |  1.6 KB  |  84 lines

  1. /*
  2. readcfg.c
  3. Written by D'Arcy J.M. Cain
  4.  
  5. Reads configuration file for feedpipe
  6. fills in the fp_cfg array and returns the number of active lines
  7. */
  8.  
  9. #include    <stdio.h>
  10. #include    <stdlib.h>
  11. #include    <string.h>
  12. #include    <ctype.h>
  13. #include    <malloc.h>
  14. #include    "feedpipe.h"
  15.  
  16. int        readcfg(FILE *fp)
  17. {
  18.     char    *str;
  19.     int        nents = 0, k;
  20.  
  21.     if ((fp_cfg = malloc(1)) == NULL)
  22.         return(0);
  23.  
  24.     while ((str = xgetline(fp, 1)) != NULL)
  25.     {
  26.         if ((fp_cfg = realloc(fp_cfg, sizeof(FP_CFG) * (nents+1))) == NULL)
  27.         {
  28.             free(str);
  29.             return(0);
  30.         }
  31.  
  32.         if ((fp_cfg[nents].name = strtok(str, " \t")) == NULL ||
  33.             (fp_cfg[nents].owner = strtok(NULL, " \t")) == NULL ||
  34.             (fp_cfg[nents].group = strtok(NULL, " \t")) == NULL )
  35.         {
  36.             free(fp_cfg);
  37.             fprintf(stderr, "%s: Invalid configuration for \"%s\"\n",
  38.                             progname, str);
  39.             return(0);
  40.         }
  41.  
  42.         if (!strcmp(fp_cfg[nents].owner, "-"))
  43.             fp_cfg[nents].owner = NULL;
  44.  
  45.         if (!strcmp(fp_cfg[nents].group, "-"))
  46.             fp_cfg[nents].group = NULL;
  47.  
  48.         fp_cfg[nents].cmd = fp_cfg[nents].group;
  49.  
  50.         while (*fp_cfg[nents].cmd++)
  51.             ;
  52.  
  53.         fp_cfg[nents].mode = strtol(fp_cfg[nents].cmd, &fp_cfg[nents].cmd, 0);
  54.  
  55.         while (isspace(*fp_cfg[nents].cmd))
  56.             *fp_cfg[nents].cmd++ = 0;
  57.  
  58.         if (!*fp_cfg[nents].cmd)
  59.         {
  60.             free(fp_cfg);
  61.             fprintf(stderr, "%s: Invalid configuration for \"%s\"\n",
  62.                             progname, str);
  63.             return(0);
  64.         }
  65.  
  66.         for (k = 0; k < nents; k++)
  67.         {
  68.             if (!strcmp(fp_cfg[k].name, fp_cfg[nents].name))
  69.             {
  70.                 free(fp_cfg);
  71.                 fprintf(stderr, "%s: Duplicate pipe \"%s\"\n", progname, str);
  72.                 return(0);
  73.             }
  74.         }
  75.  
  76.         nents++;
  77.     }
  78.  
  79.     if (!nents)
  80.         free(fp_cfg);
  81.  
  82.     return(nents);
  83. }
  84.