xref: /original-bsd/usr.bin/env/env.c (revision e58c8952)
1 /*
2  * Copyright (c) 1988, 1993, 1994
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, 1994\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.3 (Berkeley) 04/02/94";
16 #endif /* not lint */
17 
18 #include <err.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 
24 extern char **environ;
25 
26 int
27 main(argc, argv)
28 	int argc;
29 	char **argv;
30 {
31 	char **ep, *p;
32 	char *cleanenv[1];
33 	int ch;
34 
35 	while ((ch = getopt(argc, argv, "-")) != EOF)
36 		switch(ch) {
37 		case '-':
38 			environ = cleanenv;
39 			cleanenv[0] = NULL;
40 			break;
41 		case '?':
42 		default:
43 			(void)fprintf(stderr,
44 			    "usage: env [-] [name=value ...] [command]\n");
45 			exit(1);
46 		}
47 	for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv)
48 		(void)setenv(*argv, ++p, 1);
49 	if (*argv) {
50 		execvp(*argv, argv);
51 		err(1, "%s", *argv);
52 	}
53 	for (ep = environ; *ep; ep++)
54 		(void)printf("%s\n", *ep);
55 	exit(0);
56 }
57