coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

coreutils/src/uname.c (81 lines, 2.1 KiB) -rw-r--r-- blame download

01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
// uname.c, version 1.0.1
// OMKOV coreutils implementation of POSIX uname
// Copyright (C) 2020, Jakob Wakeling
// MIT Licence

#include "util/optget.h"

#include <sys/utsname.h>
#include <unistd.h>

#include <stdbool.h>
#include <stdio.h>

#define VERSION "1.0.1"

static struct lop lops[] = {
	{ "help",    ARG_NUL, 256 },
	{ "version", ARG_NUL, 257 },
	{ NULL, 0, 0 }
};

static int mode;

static inline void print(char *string);

static void hlp(void);
static void ver(void);

int main(int ac, char *av[]) { (void)(ac);
	struct opt opt = OPTGET_INIT; opt.str = "amnrsv"; opt.lops = lops;
	for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) {
	case 'a': { mode |= 31; break; }
	case 'm': { mode |=  1; break; }
	case 'n': { mode |=  2; break; }
	case 'r': { mode |=  4; break; }
	case 's': { mode |=  8; break; }
	case 'v': { mode |= 16; break; }
	case 256: { hlp(); return 0; }
	case 257: { ver(); return 0; }
	default: { return 1; }
	}
	
	if (mode == 0) { mode |= 8; }
	struct utsname name; uname(&name);
	
	if (mode &  8) { print(name.sysname); }
	if (mode &  2) { print(name.nodename); }
	if (mode &  4) { print(name.release); }
	if (mode & 16) { print(name.version); }
	if (mode &  1) { print(name.machine); }
	fputc('\n', stdout);
	
	return 0;
}

static inline void print(char *string) {
	static bool spc = false;
	if (spc) { fputc(' ', stdout); } else { spc = true; }
	fputs(string, stdout); return;
}

static void hlp(void) {
	puts("uname - return system name\n");
	puts("usage: uname [-amnrsv]\n");
	puts("options:");
	puts("  -a         Print all system characteristics");
	puts("  -m         Print the system hardware type");
	puts("  -n         Print the system network node name");
	puts("  -r         Print the current release level of the OS");
	puts("  -s         Print the name of the OS");
	puts("  -v         Print the current version of the OS");
	puts("  --help     Display help information");
	puts("  --version  Display version information");
}

static void ver(void) {
	puts("OMKOV coreutils uname, version " VERSION);
	puts("Copyright (C) 2020, Jakob Wakeling");
	puts("MIT Licence (https://opensource.org/licenses/MIT)");
}