home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / compat / stdlib / putenv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-05  |  3.7 KB  |  133 lines

  1. /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
  2. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  3. #include <libc/stubs.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <libc/bss.h>
  7. #include <libc/environ.h>
  8.  
  9. /*
  10.  
  11.    This routine assumes that the environ array and all strings it
  12.    points to were malloc'd.  Nobody else should modify the environment
  13.    except crt1.c
  14.  
  15. */
  16.  
  17. extern char **environ;
  18. static char **prev_environ = NULL; /* to know when it's safe to call `free' */
  19. static int ecount = -1;
  20. static int emax = -1;
  21. static int putenv_bss_count = -1;
  22.  
  23.  
  24. /* This gets incremented every time some variable in the
  25.    environment is added, deleted, or changes its value.
  26.    It is meant to be used by functions that depend on values
  27.    of environment variables, but don't want to call `getenv'
  28.    unnecessarily (example: `__use_lfn').
  29.  
  30.    Users should compare this variable with their static
  31.    variable whose value is initialized to zero; thus this
  32.    variable begins with 1 so first time users look they will
  33.    call `getenv'.  */
  34. unsigned __environ_changed = 1;
  35.  
  36. int
  37. putenv(const char *val)
  38. {
  39.   int vlen = strlen(val);
  40.   char *epos = strchr(val, '=');
  41.   /* Feature: VAL without a `=' means delete the entry.  GNU `putenv'
  42.      works that way, and `env' from GNU Sh-utils counts on this behavior.  */
  43.   int nlen = epos ? epos - val + 1 : vlen;
  44.   int eindex;
  45.   
  46.   /* Force recomputation of the static counters.
  47.  
  48.      The second condition of the next if clause covers the case that
  49.      somebody pointed environ to another array, which invalidates
  50.      `ecount' and `emax'.  This can be used to change the environment
  51.      to something entirely different, or to effectively discard it
  52.      altogether.  GNU `env' from Sh-utils does just that.  */
  53.   if (putenv_bss_count != __bss_count
  54.       || environ       != prev_environ)
  55.   {
  56.     putenv_bss_count = __bss_count;
  57.     for (ecount=0; environ[ecount]; ecount++);
  58.     emax = ecount;
  59.     /* Bump the count to a value no function has yet seen,
  60.        even if they were dumped with us.  */
  61.     __environ_changed++;
  62.     if (putenv_bss_count != __bss_count)
  63.     {
  64.       putenv_bss_count = __bss_count;
  65.       prev_environ = environ;    /* it's malloced by crt1.c */
  66.     }
  67.   }
  68.  
  69.   for (eindex=0; environ[eindex]; eindex++)
  70.     if (strncmp(environ[eindex], val, nlen) == 0
  71.     && (epos || environ[eindex][nlen] == '='))
  72.     {
  73.       char *oval = environ[eindex];
  74.  
  75.       if (val[nlen] == 0) /* delete the entry */
  76.       {
  77.     free(oval);
  78.     environ[eindex] = environ[ecount-1];
  79.     environ[ecount-1] = 0;
  80.     ecount--;
  81.     __environ_changed++;
  82.     return 0;
  83.       }
  84.  
  85.       /* change existing entry */
  86.       if (strcmp(environ[eindex]+nlen, val+nlen) == 0)
  87.     return 0; /* they're the same */
  88.       environ[eindex] = (char *)malloc(vlen+1);
  89.       if (environ[eindex] == 0)
  90.       {
  91.     environ[eindex] = oval;
  92.     return -1;
  93.       }
  94.       free(oval);
  95.       strcpy(environ[eindex], val);
  96.       __environ_changed++;
  97.       return 0;
  98.     }
  99.  
  100.   /* delete nonexisting entry? */
  101.   if (val[nlen] == 0)
  102.     return 0;
  103.  
  104.   /* create new entry */
  105.   if (ecount >= emax)
  106.   {
  107.     char **enew;
  108.     emax += 10;
  109.     enew = (char **)malloc(emax * sizeof(char *));
  110.     if (enew == 0)
  111.       return -1;
  112.     memcpy(enew, environ, ecount * sizeof(char *));
  113.     /* If somebody set environ to another array, we can't
  114.        safely free it.  Better leak memory than crash.  */
  115.     if (environ == prev_environ)
  116.       free(environ);
  117.     environ = enew;
  118.     prev_environ = environ;
  119.   }
  120.  
  121.   environ[ecount] = (char *)malloc(vlen+1);
  122.   if (environ[ecount] == 0)
  123.     return -1;
  124.   strcpy(environ[ecount], val);
  125.  
  126.   ecount++;
  127.   environ[ecount] = 0;
  128.  
  129.   __environ_changed++;
  130.  
  131.   return 0;
  132. }
  133.