| 79 | } |
| 80 | |
| 81 | int |
| 82 | main (int argc, char **argv) |
| 83 | { |
| 84 | bool opt_nul_terminate_output = false; |
| 85 | |
| 86 | initialize_main (&argc, &argv); |
| 87 | set_program_name (argv[0]); |
| 88 | setlocale (LC_ALL, ""); |
| 89 | bindtextdomain (PACKAGE, LOCALEDIR); |
| 90 | textdomain (PACKAGE); |
| 91 | |
| 92 | initialize_exit_failure (PRINTENV_FAILURE); |
| 93 | atexit (close_stdout); |
| 94 | |
| 95 | int optc; |
| 96 | while ((optc = getopt_long (argc, argv, "+iu:0", longopts, NULL)) != -1) |
| 97 | { |
| 98 | switch (optc) |
| 99 | { |
| 100 | case '0': |
| 101 | opt_nul_terminate_output = true; |
| 102 | break; |
| 103 | case_GETOPT_HELP_CHAR; |
| 104 | case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); |
| 105 | default: |
| 106 | usage (PRINTENV_FAILURE); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | bool ok; |
| 111 | if (optind >= argc) |
| 112 | { |
| 113 | for (char **env = environ; *env != NULL; ++env) |
| 114 | { |
| 115 | fputs (*env, stdout); |
| 116 | putchar (opt_nul_terminate_output ? '\0' : '\n'); |
| 117 | } |
| 118 | ok = true; |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | int matches = 0; |
| 123 | |
| 124 | for (int i = optind; i < argc; ++i) |
| 125 | { |
| 126 | bool matched = false; |
| 127 | |
| 128 | /* 'printenv a=b' is silent, even if 'a=b=c' is in environ. */ |
| 129 | if (strchr (argv[i], '=')) |
| 130 | continue; |
| 131 | |
| 132 | for (char **env = environ; *env; ++env) |
| 133 | { |
| 134 | char const *ep = *env; |
| 135 | char const *ap = argv[i]; |
| 136 | while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++) |
| 137 | { |
| 138 | if (*ep == '=' && *ap == '\0') |
nothing calls this directly
no test coverage detected