1*0b823812Sjoerg /* $NetBSD: printenv.c,v 1.12 2011/09/06 18:26:55 joerg Exp $ */
2b8715698Stls
361f28255Scgd /*
4b8715698Stls * Copyright (c) 1987, 1993
51f7c7963Ssimonb * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
1589aaa1bbSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
32cb7e4f16Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3498e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1987, 1993\
3598e5374cSlukem The Regents of the University of California. All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd
3861f28255Scgd #ifndef lint
39b8715698Stls /*static char sccsid[] = "from: @(#)printenv.c 8.2 (Berkeley) 5/4/95";*/
40*0b823812Sjoerg __RCSID("$NetBSD: printenv.c,v 1.12 2011/09/06 18:26:55 joerg Exp $");
4161f28255Scgd #endif /* not lint */
4261f28255Scgd
43b8715698Stls #include <sys/types.h>
44b8715698Stls
45b8715698Stls #include <stdlib.h>
46f7c6bf57Sjtc #include <stdio.h>
47f7c6bf57Sjtc #include <string.h>
48b8715698Stls #include <unistd.h>
496263c6eaSdholland #include <err.h>
50b8715698Stls
51*0b823812Sjoerg __dead static void usage(void);
52f7c6bf57Sjtc
5361f28255Scgd /*
5461f28255Scgd * printenv
5561f28255Scgd *
5661f28255Scgd * Bill Joy, UCB
5761f28255Scgd * February, 1979
5861f28255Scgd */
59f7c6bf57Sjtc int
main(int argc,char * argv[])606d977e7bSdholland main(int argc, char *argv[])
6161f28255Scgd {
6261f28255Scgd extern char **environ;
63b8715698Stls char *cp, **ep;
64b8715698Stls size_t len;
65b8715698Stls int ch;
6661f28255Scgd
67cb7e4f16Slukem while ((ch = getopt(argc, argv, "")) != -1)
68b8715698Stls switch(ch) {
69b8715698Stls case '?':
70b8715698Stls default:
71b8715698Stls usage();
72b8715698Stls }
73b8715698Stls argc -= optind;
74b8715698Stls argv += optind;
75b8715698Stls
76b8715698Stls if (argc == 0) {
7761f28255Scgd for (ep = environ; *ep; ep++)
78b8715698Stls (void)printf("%s\n", *ep);
7961f28255Scgd exit(0);
8061f28255Scgd }
816263c6eaSdholland if (argc != 1)
826263c6eaSdholland usage();
836263c6eaSdholland if (strchr(*argv, '=') != NULL)
846263c6eaSdholland errx(1, "Invalid environment variable %s", *argv);
85b8715698Stls len = strlen(*argv);
8661f28255Scgd for (ep = environ; *ep; ep++)
87b8715698Stls if (!memcmp(*ep, *argv, len)) {
8861f28255Scgd cp = *ep + len;
8961f28255Scgd if (!*cp || *cp == '=') {
90b8715698Stls (void)printf("%s\n", *cp ? cp + 1 : cp);
9161f28255Scgd exit(0);
9261f28255Scgd }
9361f28255Scgd }
9461f28255Scgd exit(1);
9561f28255Scgd }
96b8715698Stls
97*0b823812Sjoerg static void
usage(void)986d977e7bSdholland usage(void)
99b8715698Stls {
1006263c6eaSdholland (void)fprintf(stderr, "Usage: printenv [name]\n");
101b8715698Stls exit(1);
102b8715698Stls }
103