46 lines
805 B
C
46 lines
805 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.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], "--help")) {
|
|
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;
|
|
}
|