"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 6d1d2e5 | 2005-04-21 23:23:13 +0000 | [diff] [blame] | 2 | /* |
| 3 | * printenv implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2005 by Erik Andersen <andersen@codepoet.org> |
| 6 | * Copyright (C) 2005 by Mike Frysinger <vapier@gentoo.org> |
| 7 | * |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Mike Frysinger | 6d1d2e5 | 2005-04-21 23:23:13 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <string.h> |
| 13 | #include <stdlib.h> |
Mike Frysinger | 5cfa5ef | 2005-04-23 01:41:51 +0000 | [diff] [blame] | 14 | #include "busybox.h" |
Mike Frysinger | 6d1d2e5 | 2005-04-21 23:23:13 +0000 | [diff] [blame] | 15 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 16 | int printenv_main(int argc, char **argv); |
Mike Frysinger | 6d1d2e5 | 2005-04-21 23:23:13 +0000 | [diff] [blame] | 17 | int printenv_main(int argc, char **argv) |
| 18 | { |
| 19 | extern char **environ; |
| 20 | int e = 0; |
| 21 | |
| 22 | /* no variables specified, show whole env */ |
| 23 | if (argc == 1) |
| 24 | while (environ[e]) |
| 25 | puts(environ[e++]); |
| 26 | |
| 27 | /* search for specified variables and print them out if found */ |
| 28 | else { |
| 29 | int i; |
| 30 | size_t l; |
| 31 | char *arg, *env; |
| 32 | |
| 33 | for (i=1; (arg = argv[i]); ++i) |
| 34 | for (; (env = environ[e]); ++e) { |
| 35 | l = strlen(arg); |
| 36 | if (!strncmp(env, arg, l) && env[l] == '=') |
| 37 | puts(env + l + 1); |
| 38 | } |
| 39 | } |
| 40 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 41 | fflush_stdout_and_exit(0); |
Mike Frysinger | 6d1d2e5 | 2005-04-21 23:23:13 +0000 | [diff] [blame] | 42 | } |