home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / kpathsea / xputenv.c < prev    next >
C/C++ Source or Header  |  2000-01-15  |  5KB  |  143 lines

  1. /* xputenv.c: set an environment variable without return.
  2.  
  3. Copyright (C) 1993, 94, 95, 96, 97, 98 Karl Berry.
  4.  
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  18.  
  19. #include <kpathsea/config.h>
  20.  
  21. #ifdef WIN32
  22. #include <stdlib.h>
  23. #else
  24. /* Avoid implicit declaration warning.  But since some systems do
  25.    declare it, don't use a prototype, for fear of conflicts.  */
  26. extern int putenv ();
  27. #endif /* not WIN32 */
  28.  
  29. /* This `x' function is different from the others in that it takes
  30.    different parameters than the standard function; but I find it much
  31.    more convenient to pass the variable and the value separately.  Also,
  32.    this way we can guarantee that the environment value won't become
  33.    garbage.  Also, putenv just overwrites old entries with
  34.    the new, and we want to reclaim that space -- this may be called
  35.    hundreds of times on a run.
  36.    
  37.    But naturally, some systems do it differently. In this case, it's
  38.    net2 that is smart and does its own saving/freeing.  configure tries
  39.    to determine this.  */
  40.  
  41. void
  42. xputenv P2C(const_string, var_name,  const_string, value)
  43. {
  44.   string old_item = NULL;
  45.   string new_item = concat3 (var_name, "=", value);
  46.   unsigned name_len = strlen (var_name);
  47.  
  48. #ifndef SMART_PUTENV
  49.  
  50.   static const_string *saved_env_items = NULL;
  51.   static unsigned saved_len;
  52.   boolean found = false;
  53.  
  54.   /* Check if we have saved anything yet.  */
  55.   if (!saved_env_items)
  56.     {
  57.       saved_env_items = XTALLOC1 (const_string);
  58.       saved_env_items[0] = var_name;
  59.       saved_len = 1;
  60.     }
  61.   else
  62.     {
  63.       /* Check if we've assigned VAR_NAME before.  */
  64.       unsigned i;
  65.       for (i = 0; i < saved_len && !found; i++)
  66.         {
  67.           if (STREQ (saved_env_items[i], var_name))
  68.             {
  69.               found = true;
  70.               old_item = getenv (var_name);
  71. #ifdef WIN32
  72.           /* win32 putenv() removes the variable if called with
  73.          "VAR=". So we have to cope with this case. Distinction
  74.          is not made between the value being "" or the variable
  75.          not set. */
  76.           if (old_item)
  77.         old_item -= (name_len + 1);
  78. #else
  79.               assert (old_item);
  80.               /* Back up to the `NAME=' in the environment before the
  81.                  value that getenv returns.  */
  82.               old_item -= (name_len + 1);
  83. #endif
  84.             }
  85.         }
  86.  
  87.       if (!found)
  88.         {
  89.           /* If we haven't seen VAR_NAME before, save it.  Assume it is
  90.              in safe storage.  */
  91.           saved_len++;
  92.           XRETALLOC (saved_env_items, saved_len, const_string);
  93.           saved_env_items[saved_len - 1] = var_name;
  94.         }
  95.     }
  96. #endif /* not SMART_PUTENV */
  97.  
  98.   /* If the old and the new values are identical, don't do anything.
  99.      This is both more memory-efficient and safer as far as our
  100.      assumptions (about how putenv is implemented in libc) go.  */
  101.   if (!old_item || !STREQ (old_item, new_item))
  102.     {
  103.       char *new_val;
  104.       /* As far as I can see there's no way to distinguish between the
  105.          various errors; putenv doesn't have errno values.  */
  106.       if (putenv (new_item) < 0)
  107.         FATAL1 ("putenv (%s) failed", new_item);
  108.  
  109.       /* If their putenv copied `new_item', we can free it.  */
  110.       new_val = getenv (var_name);
  111.       if (new_val && new_val - name_len - 1 != new_item)
  112.         free (new_item);
  113.  
  114. #ifndef SMART_PUTENV
  115.       /* Can't free `new_item' because its contained value is now in
  116.          `environ', but we can free `old_item', since it's been replaced.  */
  117. #ifdef WIN32
  118.       /* ... except on Win32, where old_item points to garbage if we set the
  119.          variable to "".  So we recognize this special case.  */
  120.       if (old_item && value && *value)
  121. #else
  122.       if (old_item)
  123. #endif
  124.         free (old_item);
  125. #endif /* not SMART_PUTENV */
  126.     }
  127. }
  128.  
  129.  
  130. /* A special case for setting a variable to a numeric value
  131.    (specifically, KPATHSEA_DPI).  We don't need to dynamically allocate
  132.    and free the string for the number, since it's saved as part of the
  133.    environment value.  */
  134.  
  135. void
  136. xputenv_int P2C(const_string, var_name,  int, num)
  137. {
  138.   char str[MAX_INT_LENGTH];
  139.   sprintf (str, "%d", num);
  140.   
  141.   xputenv (var_name, str);
  142. }
  143.