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