tentative fix for issue 3 (ex 53)
[mplib] / src / texk / kpathsea / xputenv.c
1 /* xputenv.c: set an environment variable without return. */
2
3 /*
4  *  Copyright 2003-05 Olaf Weber.
5  *  Copyright 1993-98 Karl Berry.
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public
9  *  License as published by the Free Software Foundation; either
10  *  version 2.1 of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
20  */
21
22 #include <kpathsea/config.h>
23
24 #ifdef WIN32
25 #include <stdlib.h>
26 #else
27 /* Avoid implicit declaration warning.  But since some systems do
28    declare it, don't use a prototype, for fear of conflicts.  */
29 extern int putenv ();
30 #endif /* not WIN32 */
31
32 /* These record the strings we've set and have to keep around.
33  * This function can be called many times during a run, and this
34  * allows us to reclaim memory we allocated.
35  */
36 static const char **saved_env;
37 static int          saved_count;
38
39 /*
40  * We have different arguments from the "standard" function.  A separate
41  * var and value tends to be much more practical.
42  *
43  * The standards for putenv are clear: put the passed string into the
44  * environment, and if you alter that string, the environment changes.
45  * Of course various implementations are broken in a number of ways,
46  * which include making copies of the passed string, and more.
47  */
48 void
49 xputenv(const char *var, const char *value)
50 {
51     char  *cur_item;
52     char  *old_item;
53     char  *new_item;
54     size_t var_lim;
55     int    cur_loc;
56
57     /* kpse_debug2(KPSE_DEBUG_VARS, "kpse_putenv($%s,%s)", var, value); */
58     
59     old_item = NULL;
60     cur_item = concat3(var, "=", value);
61     /* Include '=' in length. */
62     var_lim = strlen(var) + 1;
63     
64     /* Have we stored something for this value already?  */
65     for (cur_loc = 0; cur_loc != saved_count; ++cur_loc) {
66         if (strncmp(saved_env[cur_loc], cur_item, var_lim) == 0) {
67             /* Get the old value.  We need this is case another part
68              * of the program didn't use us to change the environment.
69              */
70             old_item = getenv(var);
71             break;
72         }
73     }
74
75     if (old_item && strcmp(old_item, cur_item+var_lim) == 0) {
76         /* Set same value as is in environment, don't bother to set. */
77         free(cur_item);
78         return;
79     } else {
80         /* We set a different value. */
81         if (putenv(cur_item) < 0)
82             FATAL1("putenv(%s)", cur_item);
83         /* Get the new string. */
84         new_item = getenv(var);
85         if (new_item != cur_item+var_lim) {
86             /* Our new string isn't used, don't keep it around. */
87             free(cur_item);
88             return;
89         }
90     }
91
92     /* If we get here, it means getenv() returned a reference to cur_item.
93      * So we save cur_item, and free the old string we also owned.
94      */
95     if (cur_loc == saved_count) {
96         /* No old string. */
97         saved_count++;
98         saved_env = XRETALLOC(saved_env, saved_count, const char *);
99     } else {
100         /* We owned the old string. */
101         free(saved_env[cur_loc]);
102     }
103     saved_env[cur_loc] = cur_item;
104
105     return;
106 }
107
108 /* A special case for setting a variable to a numeric value
109    (specifically, KPATHSEA_DPI).  We don't need to dynamically allocate
110    and free the string for the number, since it's saved as part of the
111    environment value.  */
112
113 void
114 xputenv_int P2C(const_string, var_name,  int, num)
115 {
116   char str[MAX_INT_LENGTH];
117   sprintf (str, "%d", num);
118   
119   xputenv (var_name, str);
120 }