env.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* env.c - manipulate environment and execute a program
  2. in that environment
  3. Mly 861126 */
  4. /* Copyright (C) 1986, Free Software Foundation, Inc.
  5. NO WARRANTY
  6. BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  7. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
  8. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  9. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  10. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  11. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
  13. AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
  14. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  15. CORRECTION.
  16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  17. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  18. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  19. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  20. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  21. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  22. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  23. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  24. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  25. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  26. GENERAL PUBLIC LICENSE TO COPY
  27. 1. You may copy and distribute verbatim copies of this source file
  28. as you receive it, in any medium, provided that you conspicuously
  29. and appropriately publish on each copy a valid copyright notice
  30. "Copyright (C) 1986 Free Software Foundation"; and include
  31. following the copyright notice a verbatim copy of the above disclaimer
  32. of warranty and of this License.
  33. 2. You may modify your copy or copies of this source file or
  34. any portion of it, and copy and distribute such modifications under
  35. the terms of Paragraph 1 above, provided that you also do the following:
  36. a) cause the modified files to carry prominent notices stating
  37. that you changed the files and the date of any change; and
  38. b) cause the whole of any work that you distribute or publish,
  39. that in whole or in part contains or is a derivative of this
  40. program or any part thereof, to be freely distributed
  41. and licensed to all third parties on terms identical to those
  42. contained in this License Agreement (except that you may choose
  43. to grant more extensive warranty protection to third parties,
  44. at your option).
  45. 3. You may copy and distribute this program or any portion of it in
  46. compiled, executable or object code form under the terms of Paragraphs
  47. 1 and 2 above provided that you do the following:
  48. a) cause each such copy to be accompanied by the
  49. corresponding machine-readable source code, which must
  50. be distributed under the terms of Paragraphs 1 and 2 above; or,
  51. b) cause each such copy to be accompanied by a
  52. written offer, with no time limit, to give any third party
  53. free (except for a nominal shipping charge) a machine readable
  54. copy of the corresponding source code, to be distributed
  55. under the terms of Paragraphs 1 and 2 above; or,
  56. c) in the case of a recipient of this program in compiled, executable
  57. or object code form (without the corresponding source code) you
  58. shall cause copies you distribute to be accompanied by a copy
  59. of the written offer of source code which you received along
  60. with the copy you received.
  61. 4. You may not copy, sublicense, distribute or transfer this program
  62. except as expressly provided under this License Agreement. Any attempt
  63. otherwise to copy, sublicense, distribute or transfer this program is void and
  64. your rights to use the program under this License agreement shall be
  65. automatically terminated. However, parties who have received computer
  66. software programs from you with this License Agreement will not have
  67. their licenses terminated so long as such parties remain in full compliance.
  68. In other words, you are welcome to use, share and improve this program.
  69. You are forbidden to forbid anyone else to use, share and improve
  70. what you give them. Help stamp out software-hoarding! */
  71. /*
  72. If first argument is "-", then a new environment is constructed
  73. from scratch; otherwise the environment is inherited from the parent
  74. process, except as modified by other options.
  75. So, "env - foo" will invoke the "foo" program in a null environment,
  76. whereas "env foo" would invoke "foo" in the same environment as that
  77. passed to "env" itself.
  78. Subsequent arguments are interpreted as follows:
  79. * "variable=value" (ie an arg containing a "=" character)
  80. means to set the specified environment variable to that value.
  81. `value' may be of zero length ("variable="). Note that setting
  82. a variable to a zero-length value is different from unsetting it.
  83. * "-u variable" or "-unset variable"
  84. means to unset that variable
  85. If that variable isn't set, does nothing.
  86. * "-s variable value" or "-set variable value"
  87. same as "variable=value"
  88. * "-" or "--"
  89. are used to indicate that the following argument is the program
  90. to invoke. This is only necessary when the program's name
  91. begins with "-" or contains a "="
  92. * anything else
  93. The first remaining argument specifies a program to invoke
  94. (it is searched for according to the specification of the PATH
  95. environment variable) and any arguments following that are
  96. passed as arguments to that program
  97. If no program-name is specified following the environment
  98. specifications the the resulting environment is printed
  99. (The is like specifying a program-name of "printenv")
  100. Examples:
  101. If the environment passed to "env" is
  102. { USER=rms EDITOR=emacs PATH=.:/gnubin:/hacks }
  103. * "env DISPLAY=gnu:0 nemacs"
  104. calls "nemacs" in the envionment
  105. { EDITOR=emacs USER=rms DISPLAY=gnu }
  106. * "env - USER=foo /hacks/hack bar baz"
  107. will call the "hack" program on arguments "bar" and "baz"
  108. in an environment in which the only variable is "USER"
  109. Note that the "-" option will clear out the PATH variable,
  110. so one should be careful to specify in which directory
  111. to find the program to call
  112. * "env -u EDITOR USER=foo PATH=/energy -- e=mc2 bar baz"
  113. The program "/energy/e=mc2" is called with environment
  114. { USER=foo PATH=/energy }
  115. */
  116. #ifdef EMACS
  117. #define NO_SHORTNAMES
  118. #include "../src/config.h"
  119. #endif /* EMACS */
  120. #include <stdio.h>
  121. extern int execvp ();
  122. extern char *index ();
  123. char *xmalloc (), *xrealloc ();
  124. char *concat ();
  125. extern char **environ;
  126. char **nenv;
  127. int nenv_size;
  128. char *progname;
  129. void setenv ();
  130. void fatal ();
  131. main (argc, argv, envp)
  132. register int argc;
  133. register char **argv;
  134. char **envp;
  135. {
  136. register char *tem;
  137. progname = argv[0];
  138. argc--;
  139. argv++;
  140. nenv_size = 100;
  141. nenv = (char **) xmalloc (nenv_size * sizeof (char *));
  142. *nenv = (char *) 0;
  143. /* "-" flag means to not inherit parent's environment */
  144. if (argc && !strcmp (*argv, "-"))
  145. {
  146. argc--;
  147. argv++;
  148. }
  149. else
  150. /* Else pass on existing env vars. */
  151. for (; *envp; envp++)
  152. {
  153. tem = index (*envp, '=');
  154. if (tem)
  155. {
  156. *tem = '\000';
  157. setenv (*envp, tem + 1);
  158. }
  159. }
  160. while (argc > 0)
  161. {
  162. tem = index (*argv, '=');
  163. if (tem)
  164. /* If arg contains a "=" it specifies to set a variable */
  165. {
  166. *tem = '\000';
  167. setenv (*argv, tem + 1);
  168. argc--; argv++;
  169. continue;
  170. }
  171. if (**argv != '-')
  172. /* Remaining args are program name and args to pass it */
  173. break;
  174. if (argc < 2)
  175. fatal ("No argument following \"%s\" switch", *argv);
  176. if (!strcmp (*argv, "-u") ||
  177. !strcmp (*argv, "-unset"))
  178. /* Unset a variable */
  179. {
  180. argc--; argv++;
  181. setenv (*argv, 0);
  182. argc--; argv++;
  183. }
  184. else if (!strcmp (*argv, "-s") ||
  185. !strcmp (*argv, "-set"))
  186. /* Set a variable */
  187. {
  188. argc--; argv++;
  189. tem = *argv;
  190. if (argc < 2)
  191. fatal ("No value specified for variable \"%s\"",
  192. tem);
  193. argc--; argv++;
  194. setenv (tem, *argv);
  195. argc--; argv++;
  196. }
  197. else if (!strcmp (*argv, "-") || !strcmp (*argv, "--"))
  198. {
  199. argc--; argv++;
  200. break;
  201. }
  202. else
  203. {
  204. fatal ("unknown switch \"%s\"", *argv);
  205. }
  206. }
  207. /* If no program specified print the environment and exit */
  208. if (argc <= 0)
  209. {
  210. while (*nenv)
  211. printf ("%s\n", *nenv++);
  212. exit (0);
  213. }
  214. else
  215. {
  216. extern int errno, sys_nerr;
  217. extern char *sys_errlist[];
  218. environ = nenv;
  219. (void) execvp (*argv, argv);
  220. fprintf (stderr, "%s: Cannot execute \"%s\"",
  221. progname, *argv);
  222. if (errno < sys_nerr)
  223. fprintf (stderr, ": %s\n" , sys_errlist[errno]);
  224. else
  225. putc ('\n', stderr);
  226. exit (errno != 0 ? errno : 1);
  227. }
  228. }
  229. void
  230. setenv (var, val)
  231. register char *var, *val;
  232. {
  233. register char **e;
  234. int len = strlen (var);
  235. {
  236. register char *tem = index (var, '=');
  237. if (tem)
  238. fatal ("Environment variable names may not contain \"=\": %s",
  239. var);
  240. else if (*var == '\000')
  241. fatal ("Zero-length environment variable name specified.");
  242. }
  243. for (e = nenv; *e; e++)
  244. if (!strncmp (var, *e, len) &&
  245. (*e)[len] == '=')
  246. {
  247. if (val)
  248. goto set;
  249. else
  250. do { *e = *(e + 1); } while (*e++);
  251. return;
  252. }
  253. if (!val)
  254. return; /* Nothing to unset */
  255. len = e - nenv;
  256. if (len + 1 >= nenv_size)
  257. {
  258. nenv_size += 100;
  259. nenv = (char **) xrealloc (nenv, nenv_size * sizeof (char *));
  260. e = nenv + len;
  261. }
  262. set:
  263. val = concat (var, "=", val);
  264. if (*e)
  265. free (*e);
  266. else
  267. *(e + 1) = (char *) 0;
  268. *e = val;
  269. return;
  270. }
  271. void
  272. fatal (msg, arg1, arg2)
  273. char *msg, *arg1, *arg2;
  274. {
  275. fprintf (stderr, "%s: ", progname);
  276. fprintf (stderr, msg, arg1, arg2);
  277. putc ('\n', stderr);
  278. exit (1);
  279. }
  280. extern char *malloc (), *realloc ();
  281. void
  282. memory_fatal ()
  283. {
  284. fatal ("Out of memory");
  285. }
  286. char *
  287. xmalloc (size)
  288. int size;
  289. {
  290. register char *value;
  291. value = (char *) malloc (size);
  292. if (!value) memory_fatal ();
  293. return (value);
  294. }
  295. char *
  296. xrealloc (ptr, size)
  297. char *ptr;
  298. int size;
  299. {
  300. register char *value;
  301. value = (char *) realloc (ptr, size);
  302. if (!value) memory_fatal ();
  303. return (value);
  304. }
  305. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
  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. strcpy (result, s1);
  313. strcpy (result + len1, s2);
  314. strcpy (result + len1 + len2, s3);
  315. *(result + len1 + len2 + len3) = 0;
  316. return result;
  317. }
  318. /*
  319. * Local variables:
  320. * compile-command: "cc -g -o env env.c"
  321. * end:
  322. */