annotate lib-src/env.c @ 4162:1d00db08fcac

(install): Use .n, not .new, for temporary filenames.
author Richard M. Stallman <rms@gnu.org>
date Mon, 19 Jul 1993 19:11:09 +0000
parents fe951f9dd70b
children 84fcbbd80e3d
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
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2 Copyright (C) 1986 Free Software Foundation, Inc.
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"
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
62 calls "nemacs" in the envionment
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
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
99 main (argc, argv, envp)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
100 register int argc;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
101 register char **argv;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
102 char **envp;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
103 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
104 register char *tem;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
105
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
106 progname = argv[0];
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
107 argc--;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
108 argv++;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
109
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
110 nenv_size = 100;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
111 nenv = (char **) xmalloc (nenv_size * sizeof (char *));
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
112 *nenv = (char *) 0;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
113
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
114 /* "-" flag means to not inherit parent's environment */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
115 if (argc && !strcmp (*argv, "-"))
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 argc--;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
118 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 else
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
121 /* Else pass on existing env vars. */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
122 for (; *envp; envp++)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
123 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
124 tem = myindex (*envp, '=');
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
125 if (tem)
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 = '\000';
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
128 setenv (*envp, tem + 1);
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 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
131
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
132 while (argc > 0)
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 tem = myindex (*argv, '=');
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
135 if (tem)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
136 /* If arg contains a "=" it specifies to set a variable */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
137 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
138 *tem = '\000';
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
139 setenv (*argv, tem + 1);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
140 argc--;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
141 argv++;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
142 continue;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
143 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
144
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
145 if (**argv != '-')
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
146 /* Remaining args are program name and args to pass it */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
147 break;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
148
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
149 if (argc < 2)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
150 fatal ("no argument for `%s' option", *argv);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
151 if (!strcmp (*argv, "-u")
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
152 || !strcmp (*argv, "-unset"))
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
153 /* Unset a variable */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
154 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
155 argc--;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
156 argv++;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
157 setenv (*argv, (char *) 0);
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 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
161 else if (!strcmp (*argv, "-s") ||
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
162 !strcmp (*argv, "-set"))
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
163 /* Set a variable */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
164 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
165 argc--;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
166 argv++;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
167 tem = *argv;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
168 if (argc < 2)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
169 fatal ("no value specified for variable \"%s\"", tem);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
170 argc--;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
171 argv++;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
172 setenv (tem, *argv);
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 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
176 else if (!strcmp (*argv, "-") || !strcmp (*argv, "--"))
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
177 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
178 argc--;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
179 argv++;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
180 break;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
181 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
182 else
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
183 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
184 fatal ("unrecognized option `%s'", *argv);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
185 }
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
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
188 /* If no program specified print the environment and exit */
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
189 if (argc <= 0)
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 while (*nenv)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
192 printf ("%s\n", *nenv++);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
193 exit (0);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
194 }
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
195 else
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
196 {
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
197 extern int errno, sys_nerr;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
198 extern char *sys_errlist[];
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
199
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
200 environ = nenv;
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
201 (void) execvp (*argv, argv);
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 fprintf (stderr, "%s: cannot execute `%s'", progname, *argv);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
204 if (errno < sys_nerr)
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
205 fprintf (stderr, ": %s\n", sys_errlist[errno]);
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
206 else
fe951f9dd70b entered into RCS
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
207 putc ('\n', stderr);
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 }