summaryrefslogtreecommitdiff
path: root/src/printenv.c
blob: ac4511cc5f11b386dc43d992cd0053a9fad01943 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* printenv -- print all or part of environment
   Copyright (C) 1989, 1991 Free Software Foundation.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* Usage: printenv [variable...]

   If no arguments are given, print the entire environment.
   If one or more variable names are given, print the value of
   each one that is set, and nothing for ones that are not set.

   Exit status:
   0 if all variables specified were found
   1 if not

   David MacKenzie and Richard Mlynarik */

#include <stdio.h>
#include <sys/types.h>
#include "system.h"

extern char **environ;

void
main (argc, argv)
     int argc;
     char **argv;
{
  char **env;
  char *ep, *ap;
  int i;
  int matches = 0;

  if (argc == 1)
    {
      for (env = environ; *env; ++env)
	puts (*env);
      exit (0);
    }

  for (i = 1; i < argc; ++i)
    {
      for (env = environ; *env; ++env)
	{
	  ep = *env;
	  ap = argv[i];
	  while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++)
	    if (*ep == '=' && *ap == '\0')
	      {
		puts (ep + 1);
		++matches;
		break;
	      }
	}
    }
  exit (matches != argc - 1);
}