17bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes/* $NetBSD: printenv.c,v 1.12 2011/09/06 18:26:55 joerg Exp $ */ 257df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey 37bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes/* 47bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * Copyright (c) 1987, 1993 57bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * The Regents of the University of California. All rights reserved. 657df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * 757df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * Redistribution and use in source and binary forms, with or without 857df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * modification, are permitted provided that the following conditions 957df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * are met: 1057df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * 1. Redistributions of source code must retain the above copyright 1157df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * notice, this list of conditions and the following disclaimer. 1257df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * 2. Redistributions in binary form must reproduce the above copyright 1357df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * notice, this list of conditions and the following disclaimer in the 1457df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * documentation and/or other materials provided with the distribution. 1557df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * 3. Neither the name of the University nor the names of its contributors 1657df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * may be used to endorse or promote products derived from this software 1757df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * without specific prior written permission. 1857df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * 1957df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2057df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2157df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2257df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2357df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2457df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2557df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2657df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2757df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2857df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2957df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey * SUCH DAMAGE. 3057df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey */ 3157df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey 327bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <sys/cdefs.h> 337bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#ifndef lint 347bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes__COPYRIGHT("@(#) Copyright (c) 1987, 1993\ 357bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes The Regents of the University of California. All rights reserved."); 367bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#endif /* not lint */ 3757df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey 387bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#ifndef lint 397bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes/*static char sccsid[] = "from: @(#)printenv.c 8.2 (Berkeley) 5/4/95";*/ 407bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes__RCSID("$NetBSD: printenv.c,v 1.12 2011/09/06 18:26:55 joerg Exp $"); 417bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#endif /* not lint */ 4257df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey 437bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <sys/types.h> 4457df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey 457bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <stdlib.h> 467bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <stdio.h> 477bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <string.h> 487bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <unistd.h> 497bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes#include <err.h> 507bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes 517bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes__dead static void usage(void); 527bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes 537bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes/* 547bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * printenv 557bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * 567bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * Bill Joy, UCB 577bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes * February, 1979 587bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes */ 597bb5660647d0106f96b000c25f5690a45734c38cElliott Hughesint 607bb5660647d0106f96b000c25f5690a45734c38cElliott Hughesmain(int argc, char *argv[]) 617bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes{ 627bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes extern char **environ; 637bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes char *cp, **ep; 647bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes size_t len; 657bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes int ch; 667bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes 677bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes while ((ch = getopt(argc, argv, "")) != -1) 687bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes switch(ch) { 697bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes case '?': 707bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes default: 717bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes usage(); 727bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes } 737bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes argc -= optind; 747bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes argv += optind; 757bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes 767bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes if (argc == 0) { 777bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes for (ep = environ; *ep; ep++) 787bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes (void)printf("%s\n", *ep); 797bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes exit(0); 807bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes } 817bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes if (argc != 1) 827bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes usage(); 837bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes if (strchr(*argv, '=') != NULL) 847bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes errx(1, "Invalid environment variable %s", *argv); 857bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes len = strlen(*argv); 867bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes for (ep = environ; *ep; ep++) 877bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes if (!memcmp(*ep, *argv, len)) { 887bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes cp = *ep + len; 897bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes if (!*cp || *cp == '=') { 907bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes (void)printf("%s\n", *cp ? cp + 1 : cp); 917bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes exit(0); 927bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes } 937bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes } 947bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes exit(1); 957bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes} 9657df14c654a968e5b85d508cbd0871632011cb0eJeff Sharkey 977bb5660647d0106f96b000c25f5690a45734c38cElliott Hughesstatic void 987bb5660647d0106f96b000c25f5690a45734c38cElliott Hughesusage(void) 997bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes{ 1007bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes (void)fprintf(stderr, "Usage: printenv [name]\n"); 1017bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes exit(1); 1027bb5660647d0106f96b000c25f5690a45734c38cElliott Hughes} 103