xref: /onnv-gate/usr/src/cmd/env/env.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*13093SRoger.Faulkner@Oracle.COM  * Common Development and Distribution License (the "License").
6*13093SRoger.Faulkner@Oracle.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*13093SRoger.Faulkner@Oracle.COM 
22*13093SRoger.Faulkner@Oracle.COM /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24*13093SRoger.Faulkner@Oracle.COM  */
25*13093SRoger.Faulkner@Oracle.COM 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
280Sstevel@tonic-gate  *	  All Rights Reserved
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  *	env [ - ] [ name=value ]... [command arg...]
330Sstevel@tonic-gate  *	set environment, then execute command (or print environment)
340Sstevel@tonic-gate  *	- says start fresh, otherwise merge with inherited environment
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <stdlib.h>
400Sstevel@tonic-gate #include <errno.h>
410Sstevel@tonic-gate #include <unistd.h>
420Sstevel@tonic-gate #include <limits.h>
430Sstevel@tonic-gate #include <ctype.h>
440Sstevel@tonic-gate #include <locale.h>
450Sstevel@tonic-gate #include <string.h>
460Sstevel@tonic-gate #include <unistd.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static	void	Usage();
500Sstevel@tonic-gate extern	char	**environ;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 
530Sstevel@tonic-gate int
main(int argc,char ** argv)540Sstevel@tonic-gate main(int argc, char **argv)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate 	char	**p;
570Sstevel@tonic-gate 	int	opt;
580Sstevel@tonic-gate 	int	i;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #if	!defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
640Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
650Sstevel@tonic-gate #endif
660Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	/* check for non-standard "-" option */
690Sstevel@tonic-gate 	if ((argc > 1) && (strcmp(argv[1], "-")) == 0) {
70*13093SRoger.Faulkner@Oracle.COM 		(void) clearenv();
710Sstevel@tonic-gate 		for (i = 1; i < argc; i++)
720Sstevel@tonic-gate 			argv[i] = argv[i+1];
730Sstevel@tonic-gate 		argc--;
740Sstevel@tonic-gate 	}
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	/* get options */
770Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "i")) != EOF) {
780Sstevel@tonic-gate 		switch (opt) {
790Sstevel@tonic-gate 		case 'i':
80*13093SRoger.Faulkner@Oracle.COM 			(void) clearenv();
810Sstevel@tonic-gate 			break;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 		default:
840Sstevel@tonic-gate 			Usage();
850Sstevel@tonic-gate 		}
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	/* get environment strings */
890Sstevel@tonic-gate 	while (argv[optind] != NULL && strchr(argv[optind], '=') != NULL) {
900Sstevel@tonic-gate 		if (putenv(argv[optind])) {
910Sstevel@tonic-gate 			(void) perror(argv[optind]);
920Sstevel@tonic-gate 			exit(1);
930Sstevel@tonic-gate 		}
940Sstevel@tonic-gate 		optind++;
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	/* if no utility, output environment strings */
980Sstevel@tonic-gate 	if (argv[optind] == NULL) {
990Sstevel@tonic-gate 		p = environ;
1000Sstevel@tonic-gate 		while (*p != NULL)
1010Sstevel@tonic-gate 			(void) puts(*p++);
1020Sstevel@tonic-gate 	} else {
1030Sstevel@tonic-gate 		(void) execvp(argv[optind],  &argv[optind]);
1040Sstevel@tonic-gate 		(void) perror(argv[0]);
1050Sstevel@tonic-gate 		exit(((errno == ENOENT) || (errno == ENOTDIR)) ? 127 : 126);
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 	return (0);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static	void
Usage()1120Sstevel@tonic-gate Usage()
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
1150Sstevel@tonic-gate 	    "Usage: env [-i] [name=value ...] [utility [argument ...]]\n"
1160Sstevel@tonic-gate 	    "       env [-] [name=value ...] [utility [argument ...]]\n"));
1170Sstevel@tonic-gate 	exit(1);
1180Sstevel@tonic-gate }
119