~nabijaczleweli/voreutils

ref: cb91cf21c628ab3d5e42d89694d99fcca6c8efc8 voreutils/cmd/uname.cpp -rw-r--r-- 4.5 KiB
cb91cf21наб Add uname (and arch). Centralise spaced print a month ago
                                                                                
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// SPDX-License-Identifier: 0BSD


#include <climits>
#include <cstdio>
#include <string_view>
#include <sys/utsname.h>
#include <unistd.h>
#include <vore-getopt>
#include <vore-path>
#include <vore-print>
#if __OpenBSD__
#include <sys/param.h>
#elif __NetBSD__ || __FreeBSD__ || __DragonFly__
#include <sys/param.h>
#include <sys/sysctl.h>
#endif

using namespace std::literals;


#define UNAME_USAGE "usage: %s [-asnrvmpio]\n"
#define ARCH_USAGE "usage: %s\n"

#define SETFLAG(whichc, which, where)                       \
	case whichc:                                              \
		where.which = static_cast<decltype(where.which)>(true); \
		break;


enum class defaultable : std::uint8_t { no, yes, ifknown };

struct fields {
	bool s;
	bool n;
	bool r;
	bool v;
	bool m;
	defaultable p;
	defaultable i;
	bool o;

	constexpr bool operator==(const fields & rhs) const noexcept {
		return this->s == rhs.s && this->n == rhs.n && this->r == rhs.r && this->v == rhs.v && this->m == rhs.m && this->p == rhs.p && this->i == rhs.i &&
		       this->o == rhs.o;
	}
};


int main(int argc, char * const * argv) {
	fields def{}, sel{};

	auto self = vore::basename(argv[0] ?: "(?)");
	if(self == "arch") {  // 4.3BSD-Reno calls this "machine"
		if(argv[1]) {
			std::fprintf(stderr, ARCH_USAGE, argv[0]);
			return 1;
		}

		def.m = true;
	} else if(self == "uname") {
		def.s = true;

		for(auto && [arg, _] : vore::opt::get{argc,
		                                      argv,
		                                      "asnrvmpio",
		                                      {{"all", no_argument, nullptr, 'a'},
		                                       {"kernel-name", no_argument, nullptr, 's'},
		                                       {"nodename", no_argument, nullptr, 'n'},
		                                       {"kernel-release", no_argument, nullptr, 'r'},
		                                       {"kernel-version", no_argument, nullptr, 'v'},
		                                       {"machine", no_argument, nullptr, 'm'},
		                                       {"processor", no_argument, nullptr, 'p'},
		                                       {"hardware-platform", no_argument, nullptr, 'i'},
		                                       {"operating-system", no_argument, nullptr, 'o'}}})
			switch(arg) {
				SETFLAG('s', s, sel);
				SETFLAG('n', n, sel);
				SETFLAG('r', r, sel);
				SETFLAG('v', v, sel);
				SETFLAG('m', m, sel);
				SETFLAG('p', p, sel);
				SETFLAG('i', i, sel);
				SETFLAG('o', o, sel);
				case 'a':
					// sel = {true, true, true, true, true, defaultable::no, defaultable::no, false};
					sel = {true, true, true, true, true, defaultable::ifknown, defaultable::ifknown, true};
					break;
				default:
					std::fprintf(stderr, UNAME_USAGE, argv[0]);
					return 1;
			}

		if(argv[optind]) {
			std::fprintf(stderr, UNAME_USAGE, argv[0]);
			return 1;
		}
	} else {
		std::fprintf(stderr, UNAME_USAGE, "uname");
		std::fprintf(stderr, ARCH_USAGE, "arch");
		return 2;
	}

	fields flds = (sel == fields{}) ? def : sel;


	struct utsname ut {};
	if(flds.s || flds.n || flds.r || flds.v || flds.m || flds.o)
		(void)uname(&ut);

	const char * processor{};
	char proc[SYS_NMLN] __attribute__((unused));
	if(flds.p != defaultable::no) {
#if __OpenBSD__
		processor = MACHINE_ARCH;
#elif __NetBSD__ || __FreeBSD__ || __DragonFly__
		int mib[]    = {CTL_HW, HW_MACHINE_ARCH};
		size_t procl = sizeof(proc);
		if(sysctl(mib, sizeof(mib) / sizeof(*mib), proc, &procl, nullptr, 0) == -1)
			std::fprintf(stderr, "%s: sysctl(hw.machine_arch): %s", argv[0], std::strerror(errno));
		processor = proc;
#else
		processor = "unknown";
#endif
	}

	const char * platform{};
	if(flds.i != defaultable::no)
		platform = "unknown";

	if(flds.p == defaultable::ifknown && processor == "unknown"sv)
		processor = nullptr;
	if(flds.i == defaultable::ifknown && platform == "unknown"sv)
		platform = nullptr;

	return vore::print_spaced(argv[0], std::initializer_list<const char *>{
	                                       flds.s ? ut.sysname : nullptr,
	                                       flds.n ? ut.nodename : nullptr,
	                                       flds.r ? ut.release : nullptr,
	                                       flds.v ? ut.version : nullptr,
	                                       flds.m ? ut.machine : nullptr,
	                                       processor,
	                                       platform,
	                                       flds.o ? ut.sysname : nullptr,
	                                   });
}