home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / STVI369G.ZIP / ENVEVAL.C < prev    next >
C/C++ Source or Header  |  1990-05-01  |  3KB  |  114 lines

  1. /*
  2.  *    Evaluate a string, expanding environment variables
  3.  *    where encountered.
  4.  *    We'll use the UNIX convention for representing environment
  5.  *    variables: $xxx, where xxx is the shortest string that
  6.  *    matches some environment variable.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11.  
  12. char    *getenv();
  13.  
  14. int
  15. EnvEval (s, len)
  16.   char *s;
  17.   int   len;
  18. /*------------------------------------------------------------------
  19.  *  s=    Pointer to buffer, currently containing string.  It will be
  20.  *    expanded in-place in the buffer.
  21.  *  len=Maximum allowable length of the buffer.  (In this version, we
  22.  *    use a static buffer of 256 bytes internally.)
  23.  *
  24.  * RETURNS:
  25.  *     0    on success.
  26.  *    -1    on failure.  In this case, s may contain a partially
  27.  *            converted string, but it won't contain a partial
  28.  *            string.  It will be the FULL string, with as
  29.  *            many substitutions as we could find.
  30.  */
  31.  
  32. {
  33. #define    LEN    256
  34.     char    buf [LEN];
  35.     char    *s1, *s2;
  36.     char    *b1;
  37.     int    done=0;
  38.  
  39.     if (len > LEN)
  40.         return (-1);
  41.  
  42.     s1 = s;
  43.  
  44.     /*  Check for '$', and expand when we find one.  */
  45.     while (!done) {
  46.         if ((s1 = strchr (s1, '$')) == NULL)
  47.             done = 1;
  48.         else {
  49.             /*
  50.              *  Here's where the real work gets done.
  51.              *  We'll find the env.var., and convert
  52.              *  it into buf, then copy back into s
  53.              *  and continue.
  54.              */
  55.             char    c;
  56.             int    need, got;
  57.  
  58.             /* Test successively longer strings, to see
  59.              * if they're env.vars.
  60.              */
  61.             for (s2=++s1+1;    ; s2++) {
  62.                 c = *s2;    /* save it */
  63.                 *s2 = '\0';
  64.                 b1 = getenv (s1);
  65.                 *s2 = c;        /* restore it */
  66.                 if (b1)         /* found it */
  67.                     break;
  68.                 if (!*s2)        /* nothing to try */
  69.                     goto Failed;
  70.             }
  71.             --s1;    /* Back to the '$' */
  72.  
  73.             /* OK, we've found one (between s1 & s2,
  74.              * non-inclusive).  Its value is in b1.
  75.              * Do the substitution into bufp,
  76.              * and copy back into s.
  77.              */
  78.             need = strlen(b1) + strlen(s2) + 1;
  79.             got  = len - (s1-s);
  80.             if (need > got)
  81.                 goto Failed;
  82.             strcpy (buf, b1);
  83.             strcat (buf, s2);
  84.             strcpy (s1, buf);
  85.         }
  86.     }
  87.  
  88.     /*  If we get here, the converted value is in s  */
  89.     return (0);
  90.  
  91.    Failed:
  92.     return (-1);
  93. }
  94.  
  95.  
  96. /* #define SAMPLE */
  97. #ifdef SAMPLE  /***************************************************/
  98.  
  99. main (int argc, char **argv)
  100. {
  101.     int    i, ret;
  102.  
  103.     for (i=1; i<argc; i++) {
  104.         printf ("Convert  %s  to", argv [i]);
  105.         ret = EnvEval (argv [i], 80);
  106.         printf ("  %s", argv [i]);
  107.         if (ret)
  108.             printf ("  -  Failed");
  109.         putchar ('\n');
  110.     }
  111. }
  112.  
  113. #endif
  114.