/usr.bin/env/env.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 144 lines · 101 code · 11 blank · 32 comment · 21 complexity · 25a67b85569b7bd6b9cff53935ebb096 MD5 · raw file

  1. /*
  2. * Copyright (c) 1988, 1993, 1994
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #ifndef lint
  30. static const char copyright[] =
  31. "@(#) Copyright (c) 1988, 1993, 1994\n\
  32. The Regents of the University of California. All rights reserved.\n";
  33. #endif /* not lint */
  34. #if 0
  35. #ifndef lint
  36. static char sccsid[] = "@(#)env.c 8.3 (Berkeley) 4/2/94";
  37. #endif /* not lint */
  38. #endif
  39. #include <sys/cdefs.h>
  40. __FBSDID("$FreeBSD$");
  41. #include <err.h>
  42. #include <errno.h>
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <stdlib.h>
  46. #include <unistd.h>
  47. #include "envopts.h"
  48. extern char **environ;
  49. int env_verbosity;
  50. static void usage(void);
  51. int
  52. main(int argc, char **argv)
  53. {
  54. char *altpath, **ep, *p, **parg;
  55. char *cleanenv[1];
  56. int ch, want_clear;
  57. int rtrn;
  58. altpath = NULL;
  59. want_clear = 0;
  60. while ((ch = getopt(argc, argv, "-iP:S:u:v")) != -1)
  61. switch(ch) {
  62. case '-':
  63. case 'i':
  64. want_clear = 1;
  65. break;
  66. case 'P':
  67. altpath = strdup(optarg);
  68. break;
  69. case 'S':
  70. /*
  71. * The -S option, for "split string on spaces, with
  72. * support for some simple substitutions"...
  73. */
  74. split_spaces(optarg, &optind, &argc, &argv);
  75. break;
  76. case 'u':
  77. if (env_verbosity)
  78. fprintf(stderr, "#env unset:\t%s\n", optarg);
  79. rtrn = unsetenv(optarg);
  80. if (rtrn == -1)
  81. err(EXIT_FAILURE, "unsetenv %s", optarg);
  82. break;
  83. case 'v':
  84. env_verbosity++;
  85. if (env_verbosity > 1)
  86. fprintf(stderr, "#env verbosity now at %d\n",
  87. env_verbosity);
  88. break;
  89. case '?':
  90. default:
  91. usage();
  92. }
  93. if (want_clear) {
  94. environ = cleanenv;
  95. cleanenv[0] = NULL;
  96. if (env_verbosity)
  97. fprintf(stderr, "#env clearing environ\n");
  98. }
  99. for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) {
  100. if (env_verbosity)
  101. fprintf(stderr, "#env setenv:\t%s\n", *argv);
  102. *p = '\0';
  103. rtrn = setenv(*argv, p + 1, 1);
  104. *p = '=';
  105. if (rtrn == -1)
  106. err(EXIT_FAILURE, "setenv %s", *argv);
  107. }
  108. if (*argv) {
  109. if (altpath)
  110. search_paths(altpath, argv);
  111. if (env_verbosity) {
  112. fprintf(stderr, "#env executing:\t%s\n", *argv);
  113. for (parg = argv, argc = 0; *parg; parg++, argc++)
  114. fprintf(stderr, "#env arg[%d]=\t'%s'\n",
  115. argc, *parg);
  116. if (env_verbosity > 1)
  117. sleep(1);
  118. }
  119. execvp(*argv, argv);
  120. err(errno == ENOENT ? 127 : 126, "%s", *argv);
  121. }
  122. for (ep = environ; *ep; ep++)
  123. (void)printf("%s\n", *ep);
  124. exit(0);
  125. }
  126. static void
  127. usage(void)
  128. {
  129. (void)fprintf(stderr,
  130. "usage: env [-iv] [-P utilpath] [-S string] [-u name]\n"
  131. " [name=value ...] [utility [argument ...]]\n");
  132. exit(1);
  133. }