k9core/src/uname.c

87 lines
1.5 KiB
C
Raw Normal View History

#include <getopt.h>
#include <stdio.h>
2020-08-22 09:53:59 +00:00
#include <string.h>
#include <sys/utsname.h>
2020-08-22 09:53:59 +00:00
const char*
get_operating_system()
2020-08-22 09:53:59 +00:00
{
#ifdef __gnu_linux__
return "GNU/Linux";
2020-08-22 09:53:59 +00:00
#endif
#ifdef __FreeBSD__
return "FreeBSD";
2020-08-22 09:53:59 +00:00
#endif
#ifdef __OpenBSD__
return "OpenBSD";
2020-08-22 09:53:59 +00:00
#endif
#ifdef _WIN32
return "Here's a USB stick kid, get a real operating system";
2020-08-22 09:53:59 +00:00
#endif
return "Unknown operating system";
2020-08-22 09:53:59 +00:00
}
int
main(int argc, char* argv[])
{
int c;
int all = 0;
int machine = 0;
int node_name = 0;
int kernel_release = 0;
int kernel_name = 0;
int operating_system = 0;
int nothing = 0;
struct utsname kernel_info;
2020-08-21 12:06:28 +00:00
if (argc == 1)
nothing = 1;
while ((c = getopt(argc, argv, "amnrsv")) != -1) {
switch (c) {
case 'a':
all = 1;
break;
case 'm':
machine = 1;
break;
case 'n':
node_name = 1;
break;
case 'r':
kernel_release = 1;
break;
case 's':
kernel_name = 1;
break;
case 'v':
operating_system = 1;
break;
}
}
2020-08-21 12:06:28 +00:00
uname(&kernel_info);
2020-08-21 12:06:28 +00:00
if (all) {
printf("%s %s %s %s %s %s\n",
kernel_info.sysname,
kernel_info.nodename,
kernel_info.release,
kernel_info.version,
kernel_info.machine,
get_operating_system());
} else {
if (machine)
printf("%s ", kernel_info.machine);
if (node_name)
printf("%s ", kernel_info.nodename);
if (kernel_release)
printf("%s ", kernel_info.release);
if (kernel_name || nothing)
printf("%s ", kernel_info.sysname);
if (operating_system)
printf("%s", get_operating_system());
printf("\n");
}
return 0;
}