home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / ENV Server / server src / LoadEnv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-31  |  2.2 KB  |  112 lines  |  [TEXT/ALFA]

  1. /*
  2. ** LoadEnv.c
  3. **
  4. ** Functions to read environment files which contain
  5. ** environment variable definitions.
  6. */
  7.  
  8. #include <MacHeaders>
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. #include "LoadEnv.h"
  12. #include "logging.h"
  13. #include "hash.h"
  14. #include "utils.h"
  15.  
  16. #define skip_spc(x)   while (isspace(x)) x++
  17. #define find_end(x)   while (isalphanum(x) || ((x)=='_')) x--
  18.  
  19.  
  20. /************************/
  21. void LoadEnvFile(const char *cfname)
  22. {
  23.     FILE *cfile;
  24.     char msg[100];
  25.     
  26.     cfile = fopen(cfname, "r");
  27.     if (cfile == NULL)
  28.     {
  29. #if LOGGING
  30.         LogEntry("Couldn't read environment file, looking for:");
  31.         LogEntry(cfname);
  32. #endif
  33.         return;
  34.     }
  35. #if LOGGING
  36.     LogEntry("Loading environment file");
  37. #endif
  38.     
  39.     while ( !feof(cfile))
  40.     {
  41.         char s[256], *p;
  42.         char *start;            /* start of a possible xxx=yyy string */
  43.         char *eqp=NULL;            /* pointer to equal sign */
  44.         
  45.         (void)fgets( s, 255, cfile);
  46.         p = mystrchr (s, '\n');    /* remove any newline that may be there */
  47.         if (p) *p = '\000';
  48.         
  49.         p=s;
  50.         
  51.         {    /** catch a comment and terminate the string there **/
  52.             char *pp;
  53.             pp = mystrchr (p, '#');
  54.             if (pp) *pp = '\000';
  55.         }
  56.         
  57.         skip_spc(p);            /* skip initial whitespace */
  58.         
  59.         if ( isalnum(*p) || (*p=='_'))    /* Beginning of expression?? */
  60.         {
  61.             start = p;            /* read in xxx part */
  62.             while ( isalnum(*p) || *p=='_')
  63.                 p++;
  64.             
  65.             skip_spc(p);        /* skip whitespace again */
  66.             
  67.             if (*p == '=')        /* found =, now read yyy */
  68.                 eqp = p++;
  69.             else
  70.                 continue;        /* no equals sign, so skip this line */
  71.             
  72.             skip_spc(p);        /* skip whitespace again */
  73.  
  74.             while ( *p)        /* increment over var's value */
  75.                 p++;
  76.  
  77.             /*
  78.             ** At this point, *p is the ending nul of the string.
  79.             ** Now, backup over the line, changing all trailing
  80.             ** whitespace to nulls, shortening the string.
  81.             */
  82.             p--;
  83.             while ( (*p == ' ') || (*p == '\t'))
  84.                 *p-- = '\000';
  85.             
  86.             /*** done with this line ***/
  87.             
  88. #if LOGGING
  89.             sprintf(msg, "Attempting to add >%s<", start);
  90.             LogEntry(msg);
  91.             if (hash_add_entry( start) == H_ERROR)
  92.             {
  93.                 sprintf(msg, "Failed to add >%s<", start);
  94.                 LogEntry(msg);
  95.             }
  96. #else
  97.             if (hash_add_entry( start) == H_ERROR)
  98.             {
  99.             }
  100. #endif
  101.         } /* if */
  102.     } /* while !feof() */
  103.     fclose(cfile);
  104.  
  105. #if LOGGING
  106.     LogEntry("Finished reading environment file.");
  107. #endif
  108. } /* LoadEnvFile */
  109.  
  110.  
  111.  
  112.