annotate lib-src/env.c @ 34308:6d490e8ef117

(gc_sweep): Prevent symbols read during loadup from being freed.
author Gerd Moellmann <gerd@gnu.org>
date Wed, 06 Dec 2000 21:03:16 +0000
parents e9e928d02747
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
289
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1 /* env - manipulate environment and execute a program in that environment
5523
84fcbbd80e3d (main): Call strerror instead of using sys_errlist.
Roland McGrath <roland@gnu.org>
parents: 289
diff changeset
2 Copyright (C) 1986, 1994 Free Software Foundation, Inc.
289
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
3
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
6 the Free Software Foundation; either version 2, or (at your option)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
7 any later version.
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
8
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
12 GNU General Public License for more details.
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
13
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
15 along with this program; if not, write to the Free Software
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
17
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
18 /* Mly 861126 */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
19
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
20 /* If first argument is "-", then a new environment is constructed
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
21 from scratch; otherwise the environment is inherited from the parent
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
22 process, except as modified by other options.
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
23
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
24 So, "env - foo" will invoke the "foo" program in a null environment,
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
25 whereas "env foo" would invoke "foo" in the same environment as that
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
26 passed to "env" itself.
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
27
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
28 Subsequent arguments are interpreted as follows:
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
29
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
30 * "variable=value" (i.e., an arg containing a "=" character)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
31 means to set the specified environment variable to that value.
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
32 `value' may be of zero length ("variable="). Note that setting
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
33 a variable to a zero-length value is different from unsetting it.
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
34
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
35 * "-u variable" or "-unset variable"
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
36 means to unset that variable.
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
37 If that variable isn't set, does nothing.
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
38
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
39 * "-s variable value" or "-set variable value"
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
40 same as "variable=value".
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
41
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
42 * "-" or "--"
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
43 are used to indicate that the following argument is the program
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
44 to invoke. This is only necessary when the program's name
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
45 begins with "-" or contains a "=".
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
46
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
47 * anything else
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
48 The first remaining argument specifies a program to invoke
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
49 (it is searched for according to the specification of the PATH
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
50 environment variable) and any arguments following that are
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
51 passed as arguments to that program.
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
52
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
53 If no program-name is specified following the environment
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
54 specifications, the resulting environment is printed.
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
55 This is like specifying a program-name of "printenv".
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
56
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
57 Examples:
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
58 If the environment passed to "env" is
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
59 { USER=rms EDITOR=emacs PATH=.:/gnubin:/hacks }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
60
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
61 * "env DISPLAY=gnu:0 nemacs"
5523
84fcbbd80e3d (main): Call strerror instead of using sys_errlist.
Roland McGrath <roland@gnu.org>
parents: 289
diff changeset
62 calls "nemacs" in the environment
289
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
63 { USER=rms EDITOR=emacs PATH=.:/gnubin:/hacks DISPLAY=gnu:0 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
64
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
65 * "env - USER=foo /hacks/hack bar baz"
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
66 calls the "hack" program on arguments "bar" and "baz"
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
67 in an environment in which the only variable is "USER".
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
68 Note that the "-" option clears out the PATH variable,
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
69 so one should be careful to specify in which directory
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
70 to find the program to call.
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
71
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
72 * "env -u EDITOR USER=foo PATH=/energy -- e=mc2 bar baz"
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
73 The program "/energy/e=mc2" is called with environment
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
74 { USER=foo PATH=/energy }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
75 */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
76
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
77 #ifdef EMACS
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
78 #define NO_SHORTNAMES
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
79 #include "../src/config.h"
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
80 #endif /* EMACS */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
81
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
82 #include <stdio.h>
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
83
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
84 extern int execvp ();
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
85
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
86 char *xmalloc (), *xrealloc ();
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
87 char *concat ();
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
88
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
89 extern char **environ;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
90
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
91 char **nenv;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
92 int nenv_size;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
93
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
94 char *progname;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
95 void setenv ();
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
96 void fatal ();
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
97 char *myindex ();
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
98
5528
e9e928d02747 Declare strerror.
Roland McGrath <roland@gnu.org>
parents: 5527
diff changeset
99 extern char *strerror ();
e9e928d02747 Declare strerror.
Roland McGrath <roland@gnu.org>
parents: 5527
diff changeset
100
e9e928d02747 Declare strerror.
Roland McGrath <roland@gnu.org>
parents: 5527
diff changeset
101
289
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
102 main (argc, argv, envp)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
103 register int argc;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
104 register char **argv;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
105 char **envp;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
106 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
107 register char *tem;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
108
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
109 progname = argv[0];
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
110 argc--;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
111 argv++;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
112
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
113 nenv_size = 100;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
114 nenv = (char **) xmalloc (nenv_size * sizeof (char *));
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
115 *nenv = (char *) 0;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
116
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
117 /* "-" flag means to not inherit parent's environment */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
118 if (argc && !strcmp (*argv, "-"))
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
119 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
120 argc--;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
121 argv++;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
122 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
123 else
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
124 /* Else pass on existing env vars. */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
125 for (; *envp; envp++)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
126 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
127 tem = myindex (*envp, '=');
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
128 if (tem)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
129 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
130 *tem = '\000';
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
131 setenv (*envp, tem + 1);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
132 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
133 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
134
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
135 while (argc > 0)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
136 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
137 tem = myindex (*argv, '=');
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
138 if (tem)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
139 /* If arg contains a "=" it specifies to set a variable */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
140 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
141 *tem = '\000';
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
142 setenv (*argv, tem + 1);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
143 argc--;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
144 argv++;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
145 continue;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
146 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
147
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
148 if (**argv != '-')
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
149 /* Remaining args are program name and args to pass it */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
150 break;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
151
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
152 if (argc < 2)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
153 fatal ("no argument for `%s' option", *argv);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
154 if (!strcmp (*argv, "-u")
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
155 || !strcmp (*argv, "-unset"))
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
156 /* Unset a variable */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
157 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
158 argc--;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
159 argv++;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
160 setenv (*argv, (char *) 0);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
161 argc--;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
162 argv++;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
163 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
164 else if (!strcmp (*argv, "-s") ||
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
165 !strcmp (*argv, "-set"))
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
166 /* Set a variable */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
167 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
168 argc--;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
169 argv++;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
170 tem = *argv;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
171 if (argc < 2)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
172 fatal ("no value specified for variable \"%s\"", tem);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
173 argc--;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
174 argv++;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
175 setenv (tem, *argv);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
176 argc--;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
177 argv++;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
178 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
179 else if (!strcmp (*argv, "-") || !strcmp (*argv, "--"))
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
180 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
181 argc--;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
182 argv++;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
183 break;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
184 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
185 else
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
186 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
187 fatal ("unrecognized option `%s'", *argv);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
188 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
189 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
190
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
191 /* If no program specified print the environment and exit */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
192 if (argc <= 0)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
193 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
194 while (*nenv)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
195 printf ("%s\n", *nenv++);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
196 exit (0);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
197 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
198 else
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
199 {
5523
84fcbbd80e3d (main): Call strerror instead of using sys_errlist.
Roland McGrath <roland@gnu.org>
parents: 289
diff changeset
200 extern int errno;
84fcbbd80e3d (main): Call strerror instead of using sys_errlist.
Roland McGrath <roland@gnu.org>
parents: 289
diff changeset
201 extern char *strerror ();
289
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
202
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
203 environ = nenv;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
204 (void) execvp (*argv, argv);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
205
5523
84fcbbd80e3d (main): Call strerror instead of using sys_errlist.
Roland McGrath <roland@gnu.org>
parents: 289
diff changeset
206 fprintf (stderr, "%s: cannot execute `%s': %s\n",
84fcbbd80e3d (main): Call strerror instead of using sys_errlist.
Roland McGrath <roland@gnu.org>
parents: 289
diff changeset
207 progname, *argv, strerror (errno));
289
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
208 exit (errno != 0 ? errno : 1);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
209 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
210 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
211
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
212 void
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
213 setenv (var, val)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
214 register char *var, *val;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
215 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
216 register char **e;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
217 int len = strlen (var);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
218
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
219 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
220 register char *tem = myindex (var, '=');
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
221 if (tem)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
222 fatal ("environment variable names can not contain `=': %s", var);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
223 else if (*var == '\000')
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
224 fatal ("zero-length environment variable name specified");
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
225 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
226
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
227 for (e = nenv; *e; e++)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
228 if (!strncmp (var, *e, len) && (*e)[len] == '=')
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
229 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
230 if (val)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
231 goto set;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
232 else
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
233 do
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
234 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
235 *e = *(e + 1);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
236 } while (*e++);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
237 return;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
238 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
239
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
240 if (!val)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
241 return; /* Nothing to unset */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
242
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
243 len = e - nenv;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
244 if (len + 1 >= nenv_size)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
245 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
246 nenv_size += 100;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
247 nenv = (char **) xrealloc (nenv, nenv_size * sizeof (char *));
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
248 e = nenv + len;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
249 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
250
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
251 set:
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
252 val = concat (var, "=", val);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
253 if (*e)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
254 free (*e);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
255 else
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
256 *(e + 1) = (char *) 0;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
257 *e = val;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
258 return;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
259 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
260
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
261 void
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
262 fatal (msg, arg1, arg2)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
263 char *msg, *arg1, *arg2;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
264 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
265 fprintf (stderr, "%s: ", progname);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
266 fprintf (stderr, msg, arg1, arg2);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
267 putc ('\n', stderr);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
268 exit (1);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
269 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
270
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
271
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
272 extern char *malloc (), *realloc ();
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
273
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
274 void
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
275 memory_fatal ()
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
276 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
277 fatal ("virtual memory exhausted");
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
278 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
279
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
280 char *
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
281 xmalloc (size)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
282 int size;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
283 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
284 register char *value;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
285 value = (char *) malloc (size);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
286 if (!value)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
287 memory_fatal ();
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
288 return (value);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
289 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
290
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
291 char *
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
292 xrealloc (ptr, size)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
293 char *ptr;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
294 int size;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
295 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
296 register char *value;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
297 value = (char *) realloc (ptr, size);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
298 if (!value)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
299 memory_fatal ();
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
300 return (value);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
301 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
302
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
303 /* Return a newly-allocated string whose contents concatenate
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
304 those of S1, S2, S3. */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
305
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
306 char *
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
307 concat (s1, s2, s3)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
308 char *s1, *s2, *s3;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
309 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
310 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
311 char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
312
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
313 strcpy (result, s1);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
314 strcpy (result + len1, s2);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
315 strcpy (result + len1 + len2, s3);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
316 result[len1 + len2 + len3] = 0;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
317
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
318 return result;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
319 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
320
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
321 /* Return a pointer to the first occurrence in STR of C,
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
322 or 0 if C does not occur. */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
323
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
324 char *
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
325 myindex (str, c)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
326 char *str;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
327 char c;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
328 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
329 char *s = str;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
330
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
331 while (*s)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
332 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
333 if (*s == c)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
334 return s;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
335 s++;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
336 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
337 return 0;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
338 }
5527
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5523
diff changeset
339
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5523
diff changeset
340 #ifndef HAVE_STRERROR
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5523
diff changeset
341 char *
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5523
diff changeset
342 strerror (errnum)
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5523
diff changeset
343 int errnum;
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5523
diff changeset
344 {
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5523
diff changeset
345 extern char *sys_errlist[];
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5523
diff changeset
346 extern int sys_nerr;
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5523
diff changeset
347
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5523
diff changeset
348 if (errnum >= 0 && errnum < sys_nerr)
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5523
diff changeset
349 return sys_errlist[errnum];
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5523
diff changeset
350 return (char *) "Unknown error";
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5523
diff changeset
351 }
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5523
diff changeset
352
51451a050975 [! HAVE_STRERROR] (strerror): Define the function.
Roland McGrath <roland@gnu.org>
parents: 5523
diff changeset
353 #endif /* ! HAVE_STRERROR */