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-- file download

637e3a1 Jamozed 2020-07-06 23:53:34
0
// uname.c, version 1.0.1
35a6103 Jamozed 2020-06-27 00:36:30
1
// OMKOV coreutils implementation of POSIX uname
35a6103 Jamozed 2020-06-27 00:36:30
2
// Copyright (C) 2020, Jakob Wakeling
e2140ec Jamozed 2022-03-06 15:27:45
3
// MIT Licence
35a6103 Jamozed 2020-06-27 00:36:30
4
b181413 Jamozed 2022-02-05 22:32:00
5
#include "util/optget.h"
35a6103 Jamozed 2020-06-27 00:36:30
6
35a6103 Jamozed 2020-06-27 00:36:30
7
#include <sys/utsname.h>
35a6103 Jamozed 2020-06-27 00:36:30
8
#include <unistd.h>
35a6103 Jamozed 2020-06-27 00:36:30
9
35a6103 Jamozed 2020-06-27 00:36:30
10
#include <stdbool.h>
35a6103 Jamozed 2020-06-27 00:36:30
11
#include <stdio.h>
35a6103 Jamozed 2020-06-27 00:36:30
12
637e3a1 Jamozed 2020-07-06 23:53:34
13
#define VERSION "1.0.1"
637e3a1 Jamozed 2020-07-06 23:53:34
14
30cf081 Jamozed 2020-08-28 23:01:02
15
static struct lop lops[] = {
30cf081 Jamozed 2020-08-28 23:01:02
16
	{ "help",    ARG_NUL, 256 },
30cf081 Jamozed 2020-08-28 23:01:02
17
	{ "version", ARG_NUL, 257 },
30cf081 Jamozed 2020-08-28 23:01:02
18
	{ NULL, 0, 0 }
30cf081 Jamozed 2020-08-28 23:01:02
19
};
30cf081 Jamozed 2020-08-28 23:01:02
20
35a6103 Jamozed 2020-06-27 00:36:30
21
static int mode;
35a6103 Jamozed 2020-06-27 00:36:30
22
35a6103 Jamozed 2020-06-27 00:36:30
23
static inline void print(char *string);
35a6103 Jamozed 2020-06-27 00:36:30
24
30cf081 Jamozed 2020-08-28 23:01:02
25
static void hlp(void);
30cf081 Jamozed 2020-08-28 23:01:02
26
static void ver(void);
35a6103 Jamozed 2020-06-27 00:36:30
27
637e3a1 Jamozed 2020-07-06 23:53:34
28
int main(int ac, char *av[]) { (void)(ac);
30cf081 Jamozed 2020-08-28 23:01:02
29
	struct opt opt = OPTGET_INIT; opt.str = "amnrsv"; opt.lops = lops;
30cf081 Jamozed 2020-08-28 23:01:02
30
	for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) {
35a6103 Jamozed 2020-06-27 00:36:30
31
	case 'a': { mode |= 31; break; }
35a6103 Jamozed 2020-06-27 00:36:30
32
	case 'm': { mode |=  1; break; }
35a6103 Jamozed 2020-06-27 00:36:30
33
	case 'n': { mode |=  2; break; }
35a6103 Jamozed 2020-06-27 00:36:30
34
	case 'r': { mode |=  4; break; }
35a6103 Jamozed 2020-06-27 00:36:30
35
	case 's': { mode |=  8; break; }
35a6103 Jamozed 2020-06-27 00:36:30
36
	case 'v': { mode |= 16; break; }
30cf081 Jamozed 2020-08-28 23:01:02
37
	case 256: { hlp(); return 0; }
30cf081 Jamozed 2020-08-28 23:01:02
38
	case 257: { ver(); return 0; }
35a6103 Jamozed 2020-06-27 00:36:30
39
	default: { return 1; }
35a6103 Jamozed 2020-06-27 00:36:30
40
	}
35a6103 Jamozed 2020-06-27 00:36:30
41
	
35a6103 Jamozed 2020-06-27 00:36:30
42
	if (mode == 0) { mode |= 8; }
35a6103 Jamozed 2020-06-27 00:36:30
43
	struct utsname name; uname(&name);
35a6103 Jamozed 2020-06-27 00:36:30
44
	
35a6103 Jamozed 2020-06-27 00:36:30
45
	if (mode &  8) { print(name.sysname); }
35a6103 Jamozed 2020-06-27 00:36:30
46
	if (mode &  2) { print(name.nodename); }
35a6103 Jamozed 2020-06-27 00:36:30
47
	if (mode &  4) { print(name.release); }
35a6103 Jamozed 2020-06-27 00:36:30
48
	if (mode & 16) { print(name.version); }
35a6103 Jamozed 2020-06-27 00:36:30
49
	if (mode &  1) { print(name.machine); }
35a6103 Jamozed 2020-06-27 00:36:30
50
	fputc('\n', stdout);
35a6103 Jamozed 2020-06-27 00:36:30
51
	
35a6103 Jamozed 2020-06-27 00:36:30
52
	return 0;
35a6103 Jamozed 2020-06-27 00:36:30
53
}
35a6103 Jamozed 2020-06-27 00:36:30
54
35a6103 Jamozed 2020-06-27 00:36:30
55
static inline void print(char *string) {
35a6103 Jamozed 2020-06-27 00:36:30
56
	static bool spc = false;
35a6103 Jamozed 2020-06-27 00:36:30
57
	if (spc) { fputc(' ', stdout); } else { spc = true; }
35a6103 Jamozed 2020-06-27 00:36:30
58
	fputs(string, stdout); return;
35a6103 Jamozed 2020-06-27 00:36:30
59
}
35a6103 Jamozed 2020-06-27 00:36:30
60
30cf081 Jamozed 2020-08-28 23:01:02
61
static void hlp(void) {
35a6103 Jamozed 2020-06-27 00:36:30
62
	puts("uname - return system name\n");
35a6103 Jamozed 2020-06-27 00:36:30
63
	puts("usage: uname [-amnrsv]\n");
35a6103 Jamozed 2020-06-27 00:36:30
64
	puts("options:");
35a6103 Jamozed 2020-06-27 00:36:30
65
	puts("  -a         Print all system characteristics");
35a6103 Jamozed 2020-06-27 00:36:30
66
	puts("  -m         Print the system hardware type");
35a6103 Jamozed 2020-06-27 00:36:30
67
	puts("  -n         Print the system network node name");
35a6103 Jamozed 2020-06-27 00:36:30
68
	puts("  -r         Print the current release level of the OS");
35a6103 Jamozed 2020-06-27 00:36:30
69
	puts("  -s         Print the name of the OS");
35a6103 Jamozed 2020-06-27 00:36:30
70
	puts("  -v         Print the current version of the OS");
35a6103 Jamozed 2020-06-27 00:36:30
71
	puts("  --help     Display help information");
35a6103 Jamozed 2020-06-27 00:36:30
72
	puts("  --version  Display version information");
35a6103 Jamozed 2020-06-27 00:36:30
73
}
35a6103 Jamozed 2020-06-27 00:36:30
74
30cf081 Jamozed 2020-08-28 23:01:02
75
static void ver(void) {
637e3a1 Jamozed 2020-07-06 23:53:34
76
	puts("OMKOV coreutils uname, version " VERSION);
35a6103 Jamozed 2020-06-27 00:36:30
77
	puts("Copyright (C) 2020, Jakob Wakeling");
e2140ec Jamozed 2022-03-06 15:27:45
78
	puts("MIT Licence (https://opensource.org/licenses/MIT)");
35a6103 Jamozed 2020-06-27 00:36:30
79
}
80