micro-utils/coreutils/uname.c

76 lines
1.2 KiB
C
Raw Permalink Normal View History

#include <stdio.h>
#include <errno.h>
#include <string.h>
2023-11-07 16:56:14 +03:00
#include <unistd.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,
2023-11-07 16:56:14 +03:00
O_MACHINE,
O_ALL
};
2023-11-07 16:56:14 +03:00
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "ashrvm")) != -1) {
switch (opt) {
case 'a':
for (int i = 0; i < O_ALL; i++)
flags[i] = 1;
break;
2023-11-07 16:56:14 +03:00
case 's':
flags[O_SYS] = 1;
break;
2023-11-07 16:56:14 +03:00
case 'n':
flags[O_NODE] = 1;
break;
2023-11-07 16:56:14 +03:00
case 'r':
flags[O_RELEASE] = 1;
break;
2023-11-07 16:56:14 +03:00
case 'v':
flags[O_VERSION] = 1;
break;
2023-11-07 16:56:14 +03:00
case 'm':
flags[O_MACHINE] = 1;
break;
2023-11-07 16:56:14 +03:00
default:
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');
}