xref: /titanic_52/usr/src/cmd/env/env.c (revision 1eee170a5f6cf875d905524fea524c7c5c870aa0)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*
27  * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
28  *	  All Rights Reserved
29  */
30 
31 /*
32  *	env [ - ] [ name=value ]... [command arg...]
33  *	set environment, then execute command (or print environment)
34  *	- says start fresh, otherwise merge with inherited environment
35  */
36 
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <errno.h>
41 #include <unistd.h>
42 #include <limits.h>
43 #include <ctype.h>
44 #include <locale.h>
45 #include <string.h>
46 #include <unistd.h>
47 
48 
49 static	void	Usage();
50 extern	char	**environ;
51 
52 
53 int
54 main(int argc, char **argv)
55 {
56 	char	**p;
57 	int	opt;
58 	int	i;
59 
60 
61 	(void) setlocale(LC_ALL, "");
62 
63 #if	!defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
64 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
65 #endif
66 	(void) textdomain(TEXT_DOMAIN);
67 
68 	/* check for non-standard "-" option */
69 	if ((argc > 1) && (strcmp(argv[1], "-")) == 0) {
70 		(void) clearenv();
71 		for (i = 1; i < argc; i++)
72 			argv[i] = argv[i+1];
73 		argc--;
74 	}
75 
76 	/* get options */
77 	while ((opt = getopt(argc, argv, "i")) != EOF) {
78 		switch (opt) {
79 		case 'i':
80 			(void) clearenv();
81 			break;
82 
83 		default:
84 			Usage();
85 		}
86 	}
87 
88 	/* get environment strings */
89 	while (argv[optind] != NULL && strchr(argv[optind], '=') != NULL) {
90 		if (putenv(argv[optind])) {
91 			(void) perror(argv[optind]);
92 			exit(1);
93 		}
94 		optind++;
95 	}
96 
97 	/* if no utility, output environment strings */
98 	if (argv[optind] == NULL) {
99 		p = environ;
100 		while (*p != NULL)
101 			(void) puts(*p++);
102 	} else {
103 		(void) execvp(argv[optind],  &argv[optind]);
104 		(void) perror(argv[0]);
105 		exit(((errno == ENOENT) || (errno == ENOTDIR)) ? 127 : 126);
106 	}
107 	return (0);
108 }
109 
110 
111 static	void
112 Usage()
113 {
114 	(void) fprintf(stderr, gettext(
115 	    "Usage: env [-i] [name=value ...] [utility [argument ...]]\n"
116 	    "       env [-] [name=value ...] [utility [argument ...]]\n"));
117 	exit(1);
118 }
119