xref: /minix3/usr.bin/env/env.c (revision ba6640c5fd858565b70f7aa32d7ba38f6acdbeeb)
1*ba6640c5SThomas Cort /*
2*ba6640c5SThomas Cort  * Copyright (c) 1988, 1993, 1994
3*ba6640c5SThomas Cort  *	The Regents of the University of California.  All rights reserved.
4*ba6640c5SThomas Cort  *
5*ba6640c5SThomas Cort  * Redistribution and use in source and binary forms, with or without
6*ba6640c5SThomas Cort  * modification, are permitted provided that the following conditions
7*ba6640c5SThomas Cort  * are met:
8*ba6640c5SThomas Cort  * 1. Redistributions of source code must retain the above copyright
9*ba6640c5SThomas Cort  *    notice, this list of conditions and the following disclaimer.
10*ba6640c5SThomas Cort  * 2. Redistributions in binary form must reproduce the above copyright
11*ba6640c5SThomas Cort  *    notice, this list of conditions and the following disclaimer in the
12*ba6640c5SThomas Cort  *    documentation and/or other materials provided with the distribution.
13*ba6640c5SThomas Cort  * 3. Neither the name of the University nor the names of its contributors
14*ba6640c5SThomas Cort  *    may be used to endorse or promote products derived from this software
15*ba6640c5SThomas Cort  *    without specific prior written permission.
16*ba6640c5SThomas Cort  *
17*ba6640c5SThomas Cort  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18*ba6640c5SThomas Cort  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*ba6640c5SThomas Cort  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*ba6640c5SThomas Cort  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21*ba6640c5SThomas Cort  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*ba6640c5SThomas Cort  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*ba6640c5SThomas Cort  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*ba6640c5SThomas Cort  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*ba6640c5SThomas Cort  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*ba6640c5SThomas Cort  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*ba6640c5SThomas Cort  * SUCH DAMAGE.
28*ba6640c5SThomas Cort  */
29*ba6640c5SThomas Cort 
30*ba6640c5SThomas Cort #include <sys/cdefs.h>
31*ba6640c5SThomas Cort #ifndef lint
32*ba6640c5SThomas Cort __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\
33*ba6640c5SThomas Cort  The Regents of the University of California.  All rights reserved.");
34*ba6640c5SThomas Cort #endif /* not lint */
35*ba6640c5SThomas Cort 
36*ba6640c5SThomas Cort #ifndef lint
37*ba6640c5SThomas Cort /*static char sccsid[] = "@(#)env.c	8.3 (Berkeley) 4/2/94";*/
38*ba6640c5SThomas Cort __RCSID("$NetBSD: env.c,v 1.20 2010/11/16 02:53:49 christos Exp $");
39*ba6640c5SThomas Cort #endif /* not lint */
40*ba6640c5SThomas Cort 
41*ba6640c5SThomas Cort #include <err.h>
42*ba6640c5SThomas Cort #include <stdio.h>
43*ba6640c5SThomas Cort #include <string.h>
44*ba6640c5SThomas Cort #include <stdlib.h>
45*ba6640c5SThomas Cort #include <unistd.h>
46*ba6640c5SThomas Cort #include <locale.h>
47*ba6640c5SThomas Cort #include <errno.h>
48*ba6640c5SThomas Cort 
49*ba6640c5SThomas Cort static void usage(void) __attribute__((__noreturn__));
50*ba6640c5SThomas Cort 
51*ba6640c5SThomas Cort extern char **environ;
52*ba6640c5SThomas Cort 
53*ba6640c5SThomas Cort int
main(int argc,char ** argv)54*ba6640c5SThomas Cort main(int argc, char **argv)
55*ba6640c5SThomas Cort {
56*ba6640c5SThomas Cort 	char **ep;
57*ba6640c5SThomas Cort 	char *cleanenv[1];
58*ba6640c5SThomas Cort 	int ch;
59*ba6640c5SThomas Cort 
60*ba6640c5SThomas Cort 	setprogname(*argv);
61*ba6640c5SThomas Cort 	(void)setlocale(LC_ALL, "");
62*ba6640c5SThomas Cort 
63*ba6640c5SThomas Cort 	while ((ch = getopt(argc, argv, "-i")) != -1)
64*ba6640c5SThomas Cort 		switch((char)ch) {
65*ba6640c5SThomas Cort 		case '-':			/* obsolete */
66*ba6640c5SThomas Cort 		case 'i':
67*ba6640c5SThomas Cort 			environ = cleanenv;
68*ba6640c5SThomas Cort 			cleanenv[0] = NULL;
69*ba6640c5SThomas Cort 			break;
70*ba6640c5SThomas Cort 		case '?':
71*ba6640c5SThomas Cort 		default:
72*ba6640c5SThomas Cort 			usage();
73*ba6640c5SThomas Cort 		}
74*ba6640c5SThomas Cort 
75*ba6640c5SThomas Cort 	for (argv += optind; *argv && strchr(*argv, '=') != NULL; ++argv)
76*ba6640c5SThomas Cort 		(void)putenv(*argv);
77*ba6640c5SThomas Cort 
78*ba6640c5SThomas Cort 	if (*argv) {
79*ba6640c5SThomas Cort 		/* return 127 if the command to be run could not be found; 126
80*ba6640c5SThomas Cort 		   if the command was found but could not be invoked */
81*ba6640c5SThomas Cort 
82*ba6640c5SThomas Cort 		(void)execvp(*argv, argv);
83*ba6640c5SThomas Cort 		err((errno == ENOENT) ? 127 : 126, "%s", *argv);
84*ba6640c5SThomas Cort 		/* NOTREACHED */
85*ba6640c5SThomas Cort 	}
86*ba6640c5SThomas Cort 
87*ba6640c5SThomas Cort 	for (ep = environ; *ep; ep++)
88*ba6640c5SThomas Cort 		(void)printf("%s\n", *ep);
89*ba6640c5SThomas Cort 
90*ba6640c5SThomas Cort 	exit(0);
91*ba6640c5SThomas Cort }
92*ba6640c5SThomas Cort 
93*ba6640c5SThomas Cort static void
usage(void)94*ba6640c5SThomas Cort usage(void)
95*ba6640c5SThomas Cort {
96*ba6640c5SThomas Cort 	(void)fprintf(stderr, "Usage: %s [-i] [name=value ...] [command]\n",
97*ba6640c5SThomas Cort 	    getprogname());
98*ba6640c5SThomas Cort 	exit(1);
99*ba6640c5SThomas Cort }
100