1 /* xputenv.c: set an environment variable without return. */
4 * Copyright 2003-05 Olaf Weber.
5 * Copyright 1993-98 Karl Berry.
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.
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.
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
22 #include <kpathsea/config.h>
27 /* Avoid implicit declaration warning. But since some systems do
28 declare it, don't use a prototype, for fear of conflicts. */
30 #endif /* not WIN32 */
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.
36 static const char **saved_env;
37 static int saved_count;
40 * We have different arguments from the "standard" function. A separate
41 * var and value tends to be much more practical.
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.
49 xputenv(const char *var, const char *value)
57 /* kpse_debug2(KPSE_DEBUG_VARS, "kpse_putenv($%s,%s)", var, value); */
60 cur_item = concat3(var, "=", value);
61 /* Include '=' in length. */
62 var_lim = strlen(var) + 1;
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.
70 old_item = getenv(var);
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. */
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. */
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.
95 if (cur_loc == saved_count) {
98 saved_env = XRETALLOC(saved_env, saved_count, const char *);
100 /* We owned the old string. */
101 free(saved_env[cur_loc]);
103 saved_env[cur_loc] = cur_item;
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. */
114 xputenv_int P2C(const_string, var_name, int, num)
116 char str[MAX_INT_LENGTH];
117 sprintf (str, "%d", num);
119 xputenv (var_name, str);