xref: /original-bsd/usr.bin/printenv/printenv.c (revision 0f81f0ee)
1 /*
2  * Copyright (c) 1987, 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) 1987, 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[] = "@(#)printenv.c	8.2 (Berkeley) 05/04/95";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 
25 void	usage __P((void));
26 
27 /*
28  * printenv
29  *
30  * Bill Joy, UCB
31  * February, 1979
32  */
33 int
34 main(argc, argv)
35 	int argc;
36 	char *argv[];
37 {
38 	extern char **environ;
39 	register char *cp, **ep;
40 	register size_t len;
41 	int ch;
42 
43 	while ((ch = getopt(argc, argv, "")) != EOF)
44 		switch(ch) {
45 		case '?':
46 		default:
47 			usage();
48 		}
49 	argc -= optind;
50 	argv += optind;
51 
52 	if (argc == 0) {
53 		for (ep = environ; *ep; ep++)
54 			(void)printf("%s\n", *ep);
55 		exit(0);
56 	}
57 	len = strlen(*argv);
58 	for (ep = environ; *ep; ep++)
59 		if (!memcmp(*ep, *argv, len)) {
60 			cp = *ep + len;
61 			if (!*cp || *cp == '=') {
62 				(void)printf("%s\n", *cp ? cp + 1 : cp);
63 				exit(0);
64 			}
65 		}
66 	exit(1);
67 }
68 
69 void
70 usage()
71 {
72 	(void)fprintf(stderr, "usage: printenv [name]\n");
73 	exit(1);
74 }
75