2999
|
1 /* xputenv.c: set an environment variable without return. |
|
2 |
|
3 Copyright (C) 1993, 94, 95, 96 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 static const_string *saved_env_items = NULL; |
|
45 static unsigned saved_len; |
|
46 string old_item = NULL; |
|
47 string new_item = concat3 (var_name, "=", value); |
|
48 boolean found = false; |
|
49 |
|
50 #ifndef SMART_PUTENV |
|
51 /* Check if we have saved anything yet. */ |
|
52 if (!saved_env_items) |
|
53 { |
|
54 saved_env_items = XTALLOC1 (const_string); |
|
55 saved_env_items[0] = var_name; |
|
56 saved_len = 1; |
|
57 } |
|
58 else |
|
59 { |
|
60 /* Check if we've assigned VAR_NAME before. */ |
|
61 unsigned i; |
|
62 unsigned len = strlen (var_name); |
|
63 for (i = 0; i < saved_len && !found; i++) |
|
64 { |
|
65 if (STREQ (saved_env_items[i], var_name)) |
|
66 { |
|
67 found = true; |
|
68 old_item = getenv (var_name); |
|
69 #ifdef WIN32 |
|
70 /* win32 putenv() removes the variable if called with |
|
71 "VAR=". So we have to cope with this case. Distinction |
|
72 is not made between the value being "" or the variable |
|
73 not set. */ |
|
74 if (old_item) |
|
75 old_item -= (len + 1); |
|
76 #else |
|
77 assert (old_item); |
|
78 /* Back up to the `NAME=' in the environment before the |
|
79 value that getenv returns. */ |
|
80 old_item -= (len + 1); |
|
81 #endif |
|
82 } |
|
83 } |
|
84 |
|
85 if (!found) |
|
86 { |
|
87 /* If we haven't seen VAR_NAME before, save it. Assume it is |
|
88 in safe storage. */ |
|
89 saved_len++; |
|
90 XRETALLOC (saved_env_items, saved_len, const_string); |
|
91 saved_env_items[saved_len - 1] = var_name; |
|
92 } |
|
93 } |
|
94 #endif /* not SMART_PUTENV */ |
|
95 |
|
96 /* As far as I can see there's no way to distinguish between the |
|
97 various errors; putenv doesn't have errno values. */ |
|
98 if (putenv (new_item) < 0) |
|
99 FATAL1 ("putenv (%s) failed", new_item); |
|
100 |
|
101 #ifndef SMART_PUTENV |
|
102 /* Can't free `new_item' because its contained value is now in |
|
103 `environ', but we can free `old_item', since it's been replaced. */ |
|
104 if (old_item) |
|
105 free (old_item); |
|
106 #endif /* not SMART_PUTENV */ |
|
107 } |
|
108 |
|
109 |
|
110 /* A special case for setting a variable to a numeric value |
|
111 (specifically, KPATHSEA_DPI). We don't need to dynamically allocate |
|
112 and free the string for the number, since it's saved as part of the |
|
113 environment value. */ |
|
114 |
|
115 void |
|
116 xputenv_int P2C(const_string, var_name, int, num) |
|
117 { |
|
118 char str[MAX_INT_LENGTH]; |
|
119 sprintf (str, "%d", num); |
|
120 |
|
121 xputenv (var_name, str); |
|
122 } |