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