45 lines
783 B
C
45 lines
783 B
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
unsigned int nullline;
|
||
|
|
||
|
int printvars(const int len, const char **names) {
|
||
|
int ret = 0;
|
||
|
|
||
|
for (int i = 0; i < len; i++) {
|
||
|
char *val = getenv(names[i]);
|
||
|
if (!val) {
|
||
|
ret = 1;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
printf("%s%c", val, (nullline == 1) ? 0 : '\n');
|
||
|
}
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
int main(const int argc, const char **argv, const char **envp) {
|
||
|
int i;
|
||
|
for (i = 1; i < argc; i++) {
|
||
|
if (argv[i][0] != '-')
|
||
|
break;
|
||
|
|
||
|
else if (!strcmp(argv[i], "-0"))
|
||
|
nullline = 1;
|
||
|
|
||
|
else if (!strcmp(argv[i], "-h")) {
|
||
|
printf("printenv [-n (end each output line with NUL, not newline)]\n");
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (i == argc)
|
||
|
while (*envp)
|
||
|
printf("%s%c", *envp++, (nullline == 1) ? 0 : '\n');
|
||
|
|
||
|
else
|
||
|
return printvars(argc - i, argv + i);
|
||
|
|
||
|
return 0;
|
||
|
}
|