home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / octave-1.1.1p1-base.tgz / octave-1.1.1p1-base.tar / fsf / octave / kpathsea / xputenv.c < prev    next >
C/C++ Source or Header  |  1994-02-01  |  3KB  |  104 lines

  1. /* xputenv.c: set an environment variable without return.
  2.  
  3. Copyright (C) 1993 Karl Berry.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This program 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
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include <kpathsea/config.h>
  20.  
  21.  
  22. /* This `x' function is different from the others in that it takes
  23.    different parameters than the standard function; but I find it much
  24.    more convenient to pass the variable and the value separately.  Also,
  25.    this way we can guarantee that the environment value won't become
  26.    garbage.  Also, putenv just overwrites old entries with
  27.    the new, and we want to reclaim that space -- this may be called
  28.    hundreds of times on a run.
  29.    
  30.    But naturally, some systems do it differently. In this case, it's
  31.    net2 that is smart and does its own saving/freeing. If you can write
  32.    a test for configure to check this, please send it to me. Until then,
  33.    you'll have to define SMART_PUTENV yourself. */
  34.  
  35. void
  36. xputenv P2C(const_string, var_name,  const_string, value)
  37. {
  38.   static const_string *saved_env_items;
  39.   static unsigned saved_len;
  40.   string old_item = NULL;
  41.   string new_item = concat3 (var_name, "=", value);
  42.  
  43. #ifndef SMART_PUTENV
  44.   /* Check if we have saved anything yet.  */
  45.   if (!saved_env_items)
  46.     {
  47.       saved_env_items = XTALLOC1 (const_string);
  48.       saved_env_items[0] = var_name;
  49.       saved_len = 1;
  50.     }
  51.   else
  52.     {
  53.       /* Check if we've assigned VAR_NAME before.  */
  54.       unsigned i;
  55.       unsigned len = strlen (var_name);
  56.       for (i = 0; i < saved_len && !old_item; i++)
  57.         {
  58.           if (STREQ (saved_env_items[i], var_name))
  59.             {
  60.               old_item = getenv (var_name);
  61.               assert (old_item);
  62.               old_item -= (len + 1);  /* Back up to the `NAME='.  */
  63.             }
  64.         }
  65.       
  66.       if (!old_item)
  67.         {
  68.           /* If we haven't seen VAR_NAME before, save it.  Assume it is
  69.              in safe storage.  */
  70.           saved_len++;
  71.           XRETALLOC (saved_env_items, saved_len, const_string);
  72.           saved_env_items[saved_len - 1] = var_name;
  73.         }
  74.     }
  75. #endif /* not SMART_PUTENV */
  76.  
  77.   /* As far as I can see there's no way to distinguish between the
  78.      various errors; putenv doesn't have errno values.  */
  79.   if (putenv (new_item) < 0)
  80.     FATAL1 ("putenv (%s) failed", new_item);
  81.   
  82. #ifndef SMART_PUTENV
  83.   /* Can't free `new_item' because its contained value is now in
  84.      `environ', but we can free `old_item', since it's been replaced.  */
  85.   if (old_item)
  86.     free (old_item);
  87. #endif /* not SMART_PUTENV */
  88. }
  89.  
  90.  
  91. /* A special case for setting a variable to a numeric value
  92.    (specifically, KPATHSEA_DPI).  We don't need to dynamically allocate
  93.    and free the string for the number, since it's saved as part of the
  94.    environment value.  */
  95.  
  96. void
  97. xputenv_int P2C(const_string, var_name,  int, num)
  98. {
  99.   char str[MAX_INT_LENGTH];
  100.   sprintf (str, "%d", num);
  101.   
  102.   xputenv (var_name, str);
  103. }
  104.