xref: /titanic_51/usr/src/cmd/env/env.c (revision bbaa8b60dd95d714741fc474adad3cf710ef4efd)
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) 2012, Joyent, Inc. All rights reserved.
24  */
25 /*
26  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
27  */
28 
29 /*
30  * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
31  *	  All Rights Reserved
32  */
33 
34 /*
35  *	env [ - ] [ name=value ]... [command arg...]
36  *	set environment, then execute command (or print environment)
37  *	- says start fresh, otherwise merge with inherited environment
38  */
39 
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <errno.h>
44 #include <unistd.h>
45 #include <limits.h>
46 #include <ctype.h>
47 #include <locale.h>
48 #include <string.h>
49 #include <unistd.h>
50 
51 
52 static	void	Usage();
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 		(void) clearenv();
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 			(void) clearenv();
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) fprintf(stderr, "%s: %s: %s\n", argv[0], argv[optind],
108 		    strerror(errno));
109 		exit(((errno == ENOENT) || (errno == ENOTDIR)) ? 127 : 126);
110 	}
111 	return (0);
112 }
113 
114 
115 static	void
116 Usage()
117 {
118 	(void) fprintf(stderr, gettext(
119 	    "Usage: env [-i] [name=value ...] [utility [argument ...]]\n"
120 	    "       env [-] [name=value ...] [utility [argument ...]]\n"));
121 	exit(1);
122 }
123