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 * @(#)env.c 8.3 (Berkeley) 4/2/94 32 * $FreeBSD: head/usr.bin/env/env.c 326025 2017-11-20 19:49:47Z pfg $ 33 */ 34 35 #include <err.h> 36 #include <errno.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include <stdlib.h> 40 #include <unistd.h> 41 42 #include "envopts.h" 43 44 extern char **environ; 45 46 int env_verbosity; 47 48 static void usage(void); 49 50 int 51 main(int argc, char **argv) 52 { 53 char *altpath, **ep, *p, **parg; 54 char *cleanenv[1]; 55 int ch, want_clear; 56 int rtrn; 57 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 printf("%s\n", *ep); 124 exit(0); 125 } 126 127 static void 128 usage(void) 129 { 130 fprintf(stderr, 131 "usage: env [-iv] [-P utilpath] [-S string] [-u name]\n" 132 " [name=value ...] [utility [argument ...]]\n"); 133 exit(1); 134 } 135