k9core

Coreutils for *nix operating systems [fork]
git clone git://bvnf.space/k9core.git
Log | Files | Refs | LICENSE

uname.c (1635B)


      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <sys/utsname.h>

const char *
get_operating_system()
{
#ifdef __gnu_linux__
	return "GNU/Linux";
#endif
#ifdef __FreeBSD__
	return "FreeBSD";
#endif
#ifdef __OpenBSD__
	return "OpenBSD";
#endif
#ifdef _WIN32
	return "Here's a USB stick kid, get a real operating system";
#endif
	return "Unknown operating system";
}

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;

	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;
		}
	}

	if(uname(&kernel_info) == -1) {
		fprintf(stderr, "uname: %s\n", strerror(errno));
		return 1;
	}

	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;
}