micro-utils/coreutils/uname.c
2023-11-19 12:09:42 +03:00

64 lines
1.0 KiB
C

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/utsname.h>
int main(int argc, char **argv) {
struct utsname uts;
if (uname(&uts)) {
fprintf(stderr, "uname: %s", strerror(errno));
return 1;
}
if (argc == 1) {
printf("%s\n", uts.sysname);
return 0;
}
unsigned int a_flag = 0;
int opt;
while ((opt = getopt(argc, argv, "asnrvm")) != -1) {
switch (opt) {
case 'a':
a_flag = 1;
/* fallthrough */
case 's':
printf("%s ", uts.sysname);
if (!a_flag)
break;
/* fallthrough */
case 'n':
printf("%s ", uts.nodename);
if (!a_flag)
break;
/* fallthrough */
case 'r':
printf("%s ", uts.release);
if (!a_flag)
break;
/* fallthrough */
case 'v':
printf("%s ", uts.version);
if (!a_flag)
break;
/* fallthrough */
case 'm':
printf("%s ", uts.machine);
break;
default:
printf("uname\n\t[-a All] [-s Sys]\n\t[-n Nodename] [-r Release]\n\t[-v Version] [-m Machine]\n");
return 0;
}
}
putchar('\n');
}