annotate kpathsea/xputenv.c @ 2716:2b5c27299cde

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