micro-utils/coreutils/printenv.c
2023-11-07 16:56:14 +03:00

49 lines
798 B
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
unsigned int nullline;
int printvars(int len, 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(int argc, char **argv, const char **envp) {
int opt;
while ((opt = getopt(argc, argv, "0")) != -1) {
switch (opt) {
case '0':
nullline = 1;
break;
default:
printf("printenv [var1 var2...]\n\t[-n end each output line with NUL]\n");
return 0;
}
}
argv += optind;
argc -= optind;
if (argc == 0)
while (*envp)
printf("%s%c", *envp++, (nullline == 1) ? 0 : '\n');
else
return printvars(argc, argv);
return 0;
}