xref: /original-bsd/usr.bin/printenv/printenv.c (revision ea3a8ee8)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1987 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)printenv.c	5.5 (Berkeley) 10/23/92";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 
24 void	usage __P((void));
25 
26 /*
27  * printenv
28  *
29  * Bill Joy, UCB
30  * February, 1979
31  */
32 int
33 main(argc, argv)
34 	int argc;
35 	char *argv[];
36 {
37 	extern char **environ;
38 	register char *cp, **ep;
39 	register size_t len;
40 	int ch;
41 
42 	while ((ch = getopt(argc, argv, "")) != EOF)
43 		switch(ch) {
44 		case '?':
45 		default:
46 			usage();
47 		}
48 	argc -= optind;
49 	argv += optind;
50 
51 	if (argc == 0) {
52 		for (ep = environ; *ep; ep++)
53 			(void)printf("%s\n", *ep);
54 		exit(0);
55 	}
56 	len = strlen(*argv);
57 	for (ep = environ; *ep; ep++)
58 		if (!memcmp(*ep, *argv, len)) {
59 			cp = *ep + len;
60 			if (!*cp || *cp == '=') {
61 				(void)printf("%s\n", *cp ? cp + 1 : cp);
62 				exit(0);
63 			}
64 		}
65 	exit(1);
66 }
67 
68 void
69 usage()
70 {
71 	(void)fprintf(stderr, "usage: printenv [name]\n");
72 	exit(1);
73 }
74