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