home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / unixtex-6.1b-src.tgz / tar.out / contrib / unixtex / kpathsea / xputenv.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  4KB  |  108 lines

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