xref: /titanic_54/usr/src/cmd/env/env.c (revision 23a1ccea6aac035f084a7a4cdc968687d1b02daf)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*23a1cceaSRoger A. Faulkner  * Common Development and Distribution License (the "License").
6*23a1cceaSRoger A. Faulkner  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*23a1cceaSRoger A. Faulkner 
22*23a1cceaSRoger A. Faulkner /*
23*23a1cceaSRoger A. Faulkner  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24*23a1cceaSRoger A. Faulkner  */
25*23a1cceaSRoger A. Faulkner 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
287c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  *	env [ - ] [ name=value ]... [command arg...]
337c478bd9Sstevel@tonic-gate  *	set environment, then execute command (or print environment)
347c478bd9Sstevel@tonic-gate  *	- says start fresh, otherwise merge with inherited environment
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <stdlib.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <unistd.h>
427c478bd9Sstevel@tonic-gate #include <limits.h>
437c478bd9Sstevel@tonic-gate #include <ctype.h>
447c478bd9Sstevel@tonic-gate #include <locale.h>
457c478bd9Sstevel@tonic-gate #include <string.h>
467c478bd9Sstevel@tonic-gate #include <unistd.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static	void	Usage();
507c478bd9Sstevel@tonic-gate extern	char	**environ;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate int
547c478bd9Sstevel@tonic-gate main(int argc, char **argv)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	char	**p;
577c478bd9Sstevel@tonic-gate 	int	opt;
587c478bd9Sstevel@tonic-gate 	int	i;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #if	!defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
647c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
657c478bd9Sstevel@tonic-gate #endif
667c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	/* check for non-standard "-" option */
697c478bd9Sstevel@tonic-gate 	if ((argc > 1) && (strcmp(argv[1], "-")) == 0) {
70*23a1cceaSRoger A. Faulkner 		(void) clearenv();
717c478bd9Sstevel@tonic-gate 		for (i = 1; i < argc; i++)
727c478bd9Sstevel@tonic-gate 			argv[i] = argv[i+1];
737c478bd9Sstevel@tonic-gate 		argc--;
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/* get options */
777c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "i")) != EOF) {
787c478bd9Sstevel@tonic-gate 		switch (opt) {
797c478bd9Sstevel@tonic-gate 		case 'i':
80*23a1cceaSRoger A. Faulkner 			(void) clearenv();
817c478bd9Sstevel@tonic-gate 			break;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 		default:
847c478bd9Sstevel@tonic-gate 			Usage();
857c478bd9Sstevel@tonic-gate 		}
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/* get environment strings */
897c478bd9Sstevel@tonic-gate 	while (argv[optind] != NULL && strchr(argv[optind], '=') != NULL) {
907c478bd9Sstevel@tonic-gate 		if (putenv(argv[optind])) {
917c478bd9Sstevel@tonic-gate 			(void) perror(argv[optind]);
927c478bd9Sstevel@tonic-gate 			exit(1);
937c478bd9Sstevel@tonic-gate 		}
947c478bd9Sstevel@tonic-gate 		optind++;
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/* if no utility, output environment strings */
987c478bd9Sstevel@tonic-gate 	if (argv[optind] == NULL) {
997c478bd9Sstevel@tonic-gate 		p = environ;
1007c478bd9Sstevel@tonic-gate 		while (*p != NULL)
1017c478bd9Sstevel@tonic-gate 			(void) puts(*p++);
1027c478bd9Sstevel@tonic-gate 	} else {
1037c478bd9Sstevel@tonic-gate 		(void) execvp(argv[optind],  &argv[optind]);
1047c478bd9Sstevel@tonic-gate 		(void) perror(argv[0]);
1057c478bd9Sstevel@tonic-gate 		exit(((errno == ENOENT) || (errno == ENOTDIR)) ? 127 : 126);
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 	return (0);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static	void
1127c478bd9Sstevel@tonic-gate Usage()
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
1157c478bd9Sstevel@tonic-gate 	    "Usage: env [-i] [name=value ...] [utility [argument ...]]\n"
1167c478bd9Sstevel@tonic-gate 	    "       env [-] [name=value ...] [utility [argument ...]]\n"));
1177c478bd9Sstevel@tonic-gate 	exit(1);
1187c478bd9Sstevel@tonic-gate }
119