xref: /original-bsd/usr.bin/env/env.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1988, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)env.c	8.1 (Berkeley) 06/06/93";
16 #endif /* not lint */
17 
18 #include <stdio.h>
19 #include <string.h>
20 
21 main(argc, argv)
22 	int argc;
23 	char **argv;
24 {
25 	extern char **environ;
26 	extern int errno, optind;
27 	register char **ep, *p;
28 	char *cleanenv[1];
29 	int ch;
30 
31 	while ((ch = getopt(argc, argv, "-")) != EOF)
32 		switch((char)ch) {
33 		case '-':
34 			environ = cleanenv;
35 			cleanenv[0] = NULL;
36 			break;
37 		case '?':
38 		default:
39 			(void)fprintf(stderr,
40 			    "usage: env [-] [name=value ...] [command]\n");
41 			exit(1);
42 		}
43 	for (argv += optind; *argv && (p = index(*argv, '=')); ++argv)
44 		(void)setenv(*argv, ++p, 1);
45 	if (*argv) {
46 		execvp(*argv, argv);
47 		(void)fprintf(stderr, "env: %s: %s\n", *argv,
48 		    strerror(errno));
49 		exit(1);
50 	}
51 	for (ep = environ; *ep; ep++)
52 		(void)printf("%s\n", *ep);
53 	exit(0);
54 }
55