Mercurial > emacs
annotate lib-src/env.c @ 5523:84fcbbd80e3d
(main): Call strerror instead of using sys_errlist.
| author | Roland McGrath <roland@gnu.org> |
|---|---|
| date | Sat, 08 Jan 1994 21:47:36 +0000 |
| parents | fe951f9dd70b |
| children | 51451a050975 |
| 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 | |
| 99 main (argc, argv, envp) | |
| 100 register int argc; | |
| 101 register char **argv; | |
| 102 char **envp; | |
| 103 { | |
| 104 register char *tem; | |
| 105 | |
| 106 progname = argv[0]; | |
| 107 argc--; | |
| 108 argv++; | |
| 109 | |
| 110 nenv_size = 100; | |
| 111 nenv = (char **) xmalloc (nenv_size * sizeof (char *)); | |
| 112 *nenv = (char *) 0; | |
| 113 | |
| 114 /* "-" flag means to not inherit parent's environment */ | |
| 115 if (argc && !strcmp (*argv, "-")) | |
| 116 { | |
| 117 argc--; | |
| 118 argv++; | |
| 119 } | |
| 120 else | |
| 121 /* Else pass on existing env vars. */ | |
| 122 for (; *envp; envp++) | |
| 123 { | |
| 124 tem = myindex (*envp, '='); | |
| 125 if (tem) | |
| 126 { | |
| 127 *tem = '\000'; | |
| 128 setenv (*envp, tem + 1); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 while (argc > 0) | |
| 133 { | |
| 134 tem = myindex (*argv, '='); | |
| 135 if (tem) | |
| 136 /* If arg contains a "=" it specifies to set a variable */ | |
| 137 { | |
| 138 *tem = '\000'; | |
| 139 setenv (*argv, tem + 1); | |
| 140 argc--; | |
| 141 argv++; | |
| 142 continue; | |
| 143 } | |
| 144 | |
| 145 if (**argv != '-') | |
| 146 /* Remaining args are program name and args to pass it */ | |
| 147 break; | |
| 148 | |
| 149 if (argc < 2) | |
| 150 fatal ("no argument for `%s' option", *argv); | |
| 151 if (!strcmp (*argv, "-u") | |
| 152 || !strcmp (*argv, "-unset")) | |
| 153 /* Unset a variable */ | |
| 154 { | |
| 155 argc--; | |
| 156 argv++; | |
| 157 setenv (*argv, (char *) 0); | |
| 158 argc--; | |
| 159 argv++; | |
| 160 } | |
| 161 else if (!strcmp (*argv, "-s") || | |
| 162 !strcmp (*argv, "-set")) | |
| 163 /* Set a variable */ | |
| 164 { | |
| 165 argc--; | |
| 166 argv++; | |
| 167 tem = *argv; | |
| 168 if (argc < 2) | |
| 169 fatal ("no value specified for variable \"%s\"", tem); | |
| 170 argc--; | |
| 171 argv++; | |
| 172 setenv (tem, *argv); | |
| 173 argc--; | |
| 174 argv++; | |
| 175 } | |
| 176 else if (!strcmp (*argv, "-") || !strcmp (*argv, "--")) | |
| 177 { | |
| 178 argc--; | |
| 179 argv++; | |
| 180 break; | |
| 181 } | |
| 182 else | |
| 183 { | |
| 184 fatal ("unrecognized option `%s'", *argv); | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 /* If no program specified print the environment and exit */ | |
| 189 if (argc <= 0) | |
| 190 { | |
| 191 while (*nenv) | |
| 192 printf ("%s\n", *nenv++); | |
| 193 exit (0); | |
| 194 } | |
| 195 else | |
| 196 { | |
|
5523
84fcbbd80e3d
(main): Call strerror instead of using sys_errlist.
Roland McGrath <roland@gnu.org>
parents:
289
diff
changeset
|
197 extern int errno; |
|
84fcbbd80e3d
(main): Call strerror instead of using sys_errlist.
Roland McGrath <roland@gnu.org>
parents:
289
diff
changeset
|
198 extern char *strerror (); |
| 289 | 199 |
| 200 environ = nenv; | |
| 201 (void) execvp (*argv, argv); | |
| 202 | |
|
5523
84fcbbd80e3d
(main): Call strerror instead of using sys_errlist.
Roland McGrath <roland@gnu.org>
parents:
289
diff
changeset
|
203 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
|
204 progname, *argv, strerror (errno)); |
| 289 | 205 exit (errno != 0 ? errno : 1); |
| 206 } | |
| 207 } | |
| 208 | |
| 209 void | |
| 210 setenv (var, val) | |
| 211 register char *var, *val; | |
| 212 { | |
| 213 register char **e; | |
| 214 int len = strlen (var); | |
| 215 | |
| 216 { | |
| 217 register char *tem = myindex (var, '='); | |
| 218 if (tem) | |
| 219 fatal ("environment variable names can not contain `=': %s", var); | |
| 220 else if (*var == '\000') | |
| 221 fatal ("zero-length environment variable name specified"); | |
| 222 } | |
| 223 | |
| 224 for (e = nenv; *e; e++) | |
| 225 if (!strncmp (var, *e, len) && (*e)[len] == '=') | |
| 226 { | |
| 227 if (val) | |
| 228 goto set; | |
| 229 else | |
| 230 do | |
| 231 { | |
| 232 *e = *(e + 1); | |
| 233 } while (*e++); | |
| 234 return; | |
| 235 } | |
| 236 | |
| 237 if (!val) | |
| 238 return; /* Nothing to unset */ | |
| 239 | |
| 240 len = e - nenv; | |
| 241 if (len + 1 >= nenv_size) | |
| 242 { | |
| 243 nenv_size += 100; | |
| 244 nenv = (char **) xrealloc (nenv, nenv_size * sizeof (char *)); | |
| 245 e = nenv + len; | |
| 246 } | |
| 247 | |
| 248 set: | |
| 249 val = concat (var, "=", val); | |
| 250 if (*e) | |
| 251 free (*e); | |
| 252 else | |
| 253 *(e + 1) = (char *) 0; | |
| 254 *e = val; | |
| 255 return; | |
| 256 } | |
| 257 | |
| 258 void | |
| 259 fatal (msg, arg1, arg2) | |
| 260 char *msg, *arg1, *arg2; | |
| 261 { | |
| 262 fprintf (stderr, "%s: ", progname); | |
| 263 fprintf (stderr, msg, arg1, arg2); | |
| 264 putc ('\n', stderr); | |
| 265 exit (1); | |
| 266 } | |
| 267 | |
| 268 | |
| 269 extern char *malloc (), *realloc (); | |
| 270 | |
| 271 void | |
| 272 memory_fatal () | |
| 273 { | |
| 274 fatal ("virtual memory exhausted"); | |
| 275 } | |
| 276 | |
| 277 char * | |
| 278 xmalloc (size) | |
| 279 int size; | |
| 280 { | |
| 281 register char *value; | |
| 282 value = (char *) malloc (size); | |
| 283 if (!value) | |
| 284 memory_fatal (); | |
| 285 return (value); | |
| 286 } | |
| 287 | |
| 288 char * | |
| 289 xrealloc (ptr, size) | |
| 290 char *ptr; | |
| 291 int size; | |
| 292 { | |
| 293 register char *value; | |
| 294 value = (char *) realloc (ptr, size); | |
| 295 if (!value) | |
| 296 memory_fatal (); | |
| 297 return (value); | |
| 298 } | |
| 299 | |
| 300 /* Return a newly-allocated string whose contents concatenate | |
| 301 those of S1, S2, S3. */ | |
| 302 | |
| 303 char * | |
| 304 concat (s1, s2, s3) | |
| 305 char *s1, *s2, *s3; | |
| 306 { | |
| 307 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); | |
| 308 char *result = (char *) xmalloc (len1 + len2 + len3 + 1); | |
| 309 | |
| 310 strcpy (result, s1); | |
| 311 strcpy (result + len1, s2); | |
| 312 strcpy (result + len1 + len2, s3); | |
| 313 result[len1 + len2 + len3] = 0; | |
| 314 | |
| 315 return result; | |
| 316 } | |
| 317 | |
| 318 /* Return a pointer to the first occurrence in STR of C, | |
| 319 or 0 if C does not occur. */ | |
| 320 | |
| 321 char * | |
| 322 myindex (str, c) | |
| 323 char *str; | |
| 324 char c; | |
| 325 { | |
| 326 char *s = str; | |
| 327 | |
| 328 while (*s) | |
| 329 { | |
| 330 if (*s == c) | |
| 331 return s; | |
| 332 s++; | |
| 333 } | |
| 334 return 0; | |
| 335 } |
