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