micro-utils/coreutils/uname.c
2023-10-21 15:05:49 +03:00

67 lines
1.3 KiB
C

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/utsname.h>
/* sys node release version machine */
int flags[] = {0, 0, 0, 0, 0};
enum {
O_SYS,
O_NODE,
O_RELEASE,
O_VERSION,
O_MACHINE
};
int main(const int argc, const char **argv) {
for (int i = 0; i < argc; i++) {
if (!strcmp("-a", argv[i])) {
for (size_t i = 0; i < sizeof(flags) / sizeof(flags[0]); i++)
flags[i] = 1;
}
else if (!strcmp("-s", argv[i]))
flags[O_SYS] = 1;
else if (!strcmp("-n", argv[i]))
flags[O_NODE] = 1;
else if (!strcmp("-r", argv[i]))
flags[O_RELEASE] = 1;
else if (!strcmp("-v", argv[i]))
flags[O_VERSION] = 1;
else if (!strcmp("-m", argv[i]))
flags[O_MACHINE] = 1;
else if (!strcmp("--help", argv[i])) {
printf("uname [-a (All)] [-s (Sys)] [-n (Nodename)] [-r (Release)] [-v (Version)] [-m (Machine)]\n");
return 0;
}
}
struct utsname uts;
if (uname(&uts)) {
fprintf(stderr, "uname: %s", strerror(errno));
return 1;
}
if (flags[O_SYS] || argc == 1)
printf("%s ", uts.sysname);
if (flags[O_NODE])
printf("%s ", uts.nodename);
if (flags[O_RELEASE])
printf("%s ", uts.release);
if (flags[O_VERSION])
printf("%s ", uts.version);
if (flags[O_MACHINE])
printf("%s ", uts.machine);
putchar('\n');
}