11590Srgrimes/* 21590Srgrimes * Copyright (c) 1988, 1993, 1994 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 4. Neither the name of the University nor the names of its contributors 141590Srgrimes * may be used to endorse or promote products derived from this software 151590Srgrimes * without specific prior written permission. 161590Srgrimes * 171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271590Srgrimes * SUCH DAMAGE. 281590Srgrimes */ 291590Srgrimes 301590Srgrimes#ifndef lint 3191792Smikestatic const char copyright[] = 321590Srgrimes"@(#) Copyright (c) 1988, 1993, 1994\n\ 331590Srgrimes The Regents of the University of California. All rights reserved.\n"; 341590Srgrimes#endif /* not lint */ 351590Srgrimes 3691792Smike#if 0 371590Srgrimes#ifndef lint 3891004Smikestatic char sccsid[] = "@(#)env.c 8.3 (Berkeley) 4/2/94"; 391590Srgrimes#endif /* not lint */ 4091004Smike#endif 411590Srgrimes 4291004Smike#include <sys/cdefs.h> 4391004Smike__FBSDID("$FreeBSD: releng/10.3/usr.bin/env/env.c 216370 2010-12-11 08:32:16Z joel $"); 4491004Smike 451590Srgrimes#include <err.h> 4691004Smike#include <errno.h> 471590Srgrimes#include <stdio.h> 481590Srgrimes#include <string.h> 491590Srgrimes#include <stdlib.h> 501590Srgrimes#include <unistd.h> 511590Srgrimes 52147493Sgad#include "envopts.h" 53147493Sgad 541590Srgrimesextern char **environ; 551590Srgrimes 56147490Sgadint env_verbosity; 57147490Sgad 5892920Simpstatic void usage(void); 5927100Scharnier 601590Srgrimesint 61102944Sdwmalonemain(int argc, char **argv) 621590Srgrimes{ 63169177Sache char *altpath, **ep, *p, **parg; 641590Srgrimes char *cleanenv[1]; 65147491Sgad int ch, want_clear; 66171195Sscf int rtrn; 671590Srgrimes 68147493Sgad altpath = NULL; 69147491Sgad want_clear = 0; 70178289Sgad while ((ch = getopt(argc, argv, "-iP:S:u:v")) != -1) 711590Srgrimes switch(ch) { 721590Srgrimes case '-': 7350444Ssheldonh case 'i': 74147491Sgad want_clear = 1; 751590Srgrimes break; 76147493Sgad case 'P': 77147493Sgad altpath = strdup(optarg); 78147493Sgad break; 79147493Sgad case 'S': 80147493Sgad /* 81147493Sgad * The -S option, for "split string on spaces, with 82147493Sgad * support for some simple substitutions"... 83147493Sgad */ 84147493Sgad split_spaces(optarg, &optind, &argc, &argv); 85147493Sgad break; 86178289Sgad case 'u': 87178289Sgad if (env_verbosity) 88178289Sgad fprintf(stderr, "#env unset:\t%s\n", optarg); 89178289Sgad rtrn = unsetenv(optarg); 90178289Sgad if (rtrn == -1) 91178289Sgad err(EXIT_FAILURE, "unsetenv %s", optarg); 92178289Sgad break; 93147490Sgad case 'v': 94147490Sgad env_verbosity++; 95147490Sgad if (env_verbosity > 1) 96147490Sgad fprintf(stderr, "#env verbosity now at %d\n", 97147490Sgad env_verbosity); 98147490Sgad break; 991590Srgrimes case '?': 1001590Srgrimes default: 10127100Scharnier usage(); 1021590Srgrimes } 103147491Sgad if (want_clear) { 104147491Sgad environ = cleanenv; 105147491Sgad cleanenv[0] = NULL; 106147491Sgad if (env_verbosity) 107147491Sgad fprintf(stderr, "#env clearing environ\n"); 108147491Sgad } 109169177Sache for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) { 110147490Sgad if (env_verbosity) 111147490Sgad fprintf(stderr, "#env setenv:\t%s\n", *argv); 112171195Sscf *p = '\0'; 113171195Sscf rtrn = setenv(*argv, p + 1, 1); 114171195Sscf *p = '='; 115171195Sscf if (rtrn == -1) 116171195Sscf err(EXIT_FAILURE, "setenv %s", *argv); 117147490Sgad } 1181590Srgrimes if (*argv) { 119147493Sgad if (altpath) 120147493Sgad search_paths(altpath, argv); 121147490Sgad if (env_verbosity) { 122147490Sgad fprintf(stderr, "#env executing:\t%s\n", *argv); 123147490Sgad for (parg = argv, argc = 0; *parg; parg++, argc++) 124147490Sgad fprintf(stderr, "#env arg[%d]=\t'%s'\n", 125147490Sgad argc, *parg); 126147490Sgad if (env_verbosity > 1) 127147490Sgad sleep(1); 128147490Sgad } 1291590Srgrimes execvp(*argv, argv); 13091004Smike err(errno == ENOENT ? 127 : 126, "%s", *argv); 1311590Srgrimes } 1321590Srgrimes for (ep = environ; *ep; ep++) 1331590Srgrimes (void)printf("%s\n", *ep); 1341590Srgrimes exit(0); 1351590Srgrimes} 13627100Scharnier 13727100Scharnierstatic void 138102944Sdwmaloneusage(void) 13927100Scharnier{ 14027100Scharnier (void)fprintf(stderr, 141178289Sgad "usage: env [-iv] [-P utilpath] [-S string] [-u name]\n" 142178289Sgad " [name=value ...] [utility [argument ...]]\n"); 14327100Scharnier exit(1); 14427100Scharnier} 145