home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / DLIBSSRC.ZIP / PUTENV.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-06  |  2.0 KB  |  79 lines

  1. #include <stdio.h>
  2.  
  3. extern char    *_envp;
  4.  
  5. #define    ENVSIZ    2048
  6.  
  7. static    int    envset = FALSE;        /* local env created? */
  8.  
  9. int putenv(entry)
  10. char *entry;
  11. /*
  12.  *    Add <entry> to the environment.  <entry> can be any of the following
  13.  *    forms:
  14.  *        <VARIABLE>
  15.  *        <VARIABLE>=
  16.  *        <VARIABLE>=<value>
  17.  *    The first form removes <VARIABLE> from the environment.  getenv()
  18.  *    will return NULL if looking for this variable.  The second form adds
  19.  *    <VARIABLE> to the environment, with a null value.  getenv() will
  20.  *    return a pointer to a '\0' character if looking for this variable.
  21.  *    Many environment handlers don't support such "tag variables", so
  22.  *    their use is not recommended.  The final form is the most common,
  23.  *    and safest to use.  <VARIABLE> is installed (or replaced) with the
  24.  *    value <value>.  It should be noted that this function itself is not
  25.  *    supported in many systems and should be used will care to prevent
  26.  *    overflowing the space allocated for the environment.
  27.  */
  28. {
  29.     register char *p, *q, *t, c;
  30.     register int len;
  31.     char *malloc(), *getenv();
  32.  
  33.     if(!envset) {                    /* no local env */
  34.         if((p = malloc(ENVSIZ)) == NULL)
  35.             return(FALSE);
  36.         q = _envp;
  37.         _envp = p;
  38.         envset = TRUE;
  39.         if(q) {
  40.             while(*q)
  41.                 while(*p++ = *q++)
  42.                     ;
  43.         }
  44.         else
  45.             *p++ = '\0';
  46.         *p++ = '\0';
  47.         *p = 0xFF;
  48.     }
  49.     for(t=entry; (c = *t) && (c != '='); ++t)
  50.         ;
  51.     *t = '\0';
  52.     if(p = getenv(entry)) {                /* remove old var */
  53.         q = p;
  54.         while(*q++)            /* find end of old val */
  55.             ;
  56.         p -= (len = strlen(entry));
  57.         while(strncmp(--p, entry, len))    /* find start of old var */
  58.             ;
  59.         while(*q)            /* copy environment tail */
  60.             while(*p++ = *q++)
  61.                 ;
  62.         *p++ = '\0';            /* tie off environment */
  63.         *p = 0xFF;
  64.     }
  65.     if(c == '=') {                    /* install new var */
  66.         p = _envp;
  67.         while(*p)            /* find end of env */
  68.             while(*p++)
  69.                 ;
  70.         *t = c;
  71.         q = entry;
  72.         while(*p++ = *q++)        /* copy new entry */
  73.             ;
  74.         *p++ = '\0';            /* tie off environment */
  75.         *p = 0xFF;
  76.     }
  77.     return(TRUE);
  78. }
  79.