xref: /illumos-gate/usr/src/cmd/env/env.c (revision 8c8c34cd)
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
523a1cceaSRoger A. Faulkner  * Common Development and Distribution License (the "License").
623a1cceaSRoger 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  */
2123a1cceaSRoger A. Faulkner 
22*8c8c34cdSRobert Mustacchi /*
23*8c8c34cdSRobert Mustacchi  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
24*8c8c34cdSRobert Mustacchi  */
257c478bd9Sstevel@tonic-gate /*
2623a1cceaSRoger A. Faulkner  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
3023a1cceaSRoger A. Faulkner  * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
3123a1cceaSRoger A. Faulkner  *	  All Rights Reserved
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  *	env [ - ] [ name=value ]... [command arg...]
367c478bd9Sstevel@tonic-gate  *	set environment, then execute command (or print environment)
377c478bd9Sstevel@tonic-gate  *	- says start fresh, otherwise merge with inherited environment
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <limits.h>
467c478bd9Sstevel@tonic-gate #include <ctype.h>
477c478bd9Sstevel@tonic-gate #include <locale.h>
487c478bd9Sstevel@tonic-gate #include <string.h>
497c478bd9Sstevel@tonic-gate #include <unistd.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static	void	Usage();
537c478bd9Sstevel@tonic-gate extern	char	**environ;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)577c478bd9Sstevel@tonic-gate main(int argc, char **argv)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	char	**p;
607c478bd9Sstevel@tonic-gate 	int	opt;
617c478bd9Sstevel@tonic-gate 	int	i;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #if	!defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
677c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
687c478bd9Sstevel@tonic-gate #endif
697c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	/* check for non-standard "-" option */
727c478bd9Sstevel@tonic-gate 	if ((argc > 1) && (strcmp(argv[1], "-")) == 0) {
7323a1cceaSRoger A. Faulkner 		(void) clearenv();
747c478bd9Sstevel@tonic-gate 		for (i = 1; i < argc; i++)
757c478bd9Sstevel@tonic-gate 			argv[i] = argv[i+1];
767c478bd9Sstevel@tonic-gate 		argc--;
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/* get options */
807c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "i")) != EOF) {
817c478bd9Sstevel@tonic-gate 		switch (opt) {
827c478bd9Sstevel@tonic-gate 		case 'i':
8323a1cceaSRoger A. Faulkner 			(void) clearenv();
847c478bd9Sstevel@tonic-gate 			break;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 		default:
877c478bd9Sstevel@tonic-gate 			Usage();
887c478bd9Sstevel@tonic-gate 		}
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	/* get environment strings */
927c478bd9Sstevel@tonic-gate 	while (argv[optind] != NULL && strchr(argv[optind], '=') != NULL) {
937c478bd9Sstevel@tonic-gate 		if (putenv(argv[optind])) {
947c478bd9Sstevel@tonic-gate 			(void) perror(argv[optind]);
957c478bd9Sstevel@tonic-gate 			exit(1);
967c478bd9Sstevel@tonic-gate 		}
977c478bd9Sstevel@tonic-gate 		optind++;
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/* if no utility, output environment strings */
1017c478bd9Sstevel@tonic-gate 	if (argv[optind] == NULL) {
1027c478bd9Sstevel@tonic-gate 		p = environ;
1037c478bd9Sstevel@tonic-gate 		while (*p != NULL)
1047c478bd9Sstevel@tonic-gate 			(void) puts(*p++);
1057c478bd9Sstevel@tonic-gate 	} else {
1067c478bd9Sstevel@tonic-gate 		(void) execvp(argv[optind],  &argv[optind]);
107*8c8c34cdSRobert Mustacchi 		(void) fprintf(stderr, "%s: %s: %s\n", argv[0], argv[optind],
108*8c8c34cdSRobert Mustacchi 		    strerror(errno));
1097c478bd9Sstevel@tonic-gate 		exit(((errno == ENOENT) || (errno == ENOTDIR)) ? 127 : 126);
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 	return (0);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate static	void
Usage()1167c478bd9Sstevel@tonic-gate Usage()
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
1197c478bd9Sstevel@tonic-gate 	    "Usage: env [-i] [name=value ...] [utility [argument ...]]\n"
1207c478bd9Sstevel@tonic-gate 	    "       env [-] [name=value ...] [utility [argument ...]]\n"));
1217c478bd9Sstevel@tonic-gate 	exit(1);
1227c478bd9Sstevel@tonic-gate }
123