Annotation of solidite/utils/uname.cpp, revision 1.1.1.1
1.1 user 1: /* Copyright (c) 2026 The Solidite Developers
2: *
3: * This program is free software: you can redistribute it and/or modify it under
4: * the terms of the GNU Affero General Public License as published by the Free
5: * Software Foundation, either version 3 of the License, or (at your option)
6: * any later version.
7: *
8: * This program is distributed in the hope that it will be useful, but WITHOUT
9: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
11: * details.
12: *
13: * You should have received a copy of the GNU Affero General Public License
14: * along with this program. If not, see <https://www.gnu.org/licenses/>. */
15:
16: #include <bitset>
17: #include <iostream>
18: #include <locale>
19: #include <string>
20:
21: #include <cstddef>
22: #include <cstdint>
23: #include <cstring>
24:
25: #include <sys/utsname.h>
26:
27: int main(int argc, char* argv[]) {
28: // locale work
29: std::locale::global(std::locale(""));
30: std::cerr.imbue(std::locale());
31:
32: std::bitset<6> options = 0b000000;
33: /* options[0]=print system hardware type (-m)
34: * options[1]=write node (-n)
35: * options[2]=write OS release level (-r)
36: * options[3]=write the name of the operating system (-s)
37: * options[4]=write OS version level of the OS release (-v)
38: * options[5]=error occurred, return code 1*/
39:
40: for (int i = 1; i < argc; ++i) {
41: if (argv[i][0] != '-')
42: continue;
43: for (std::size_t j = 1; j < strlen(argv[i]); ++j) {
44: switch (argv[i][j]) {
45: case 'a':
46: for (int i = 0; i < 5; ++i)
47: options.set(i);
48: break;
49: case 'm':
50: options.set(0);
51: break;
52: case 'n':
53: options.set(1);
54: break;
55: case 'r':
56: options.set(2);
57: break;
58: case 's':
59: options.set(3);
60: break;
61: case 'v':
62: options.set(4);
63: break;
64: default:
65: std::cerr << "bad option\n";
66: options.set(5);
67: break;
68: }
69: }
70: }
71:
72: if (options == 0)
73: options.set(3);
74:
75: struct utsname uts_name;
76: if (uname(&uts_name) != 0) {
77: std::cerr << "getting uname failed\n";
78: return 1;
79: }
80:
81: std::string output_buffer {};
82: if (options.test(0) == 1) {
83: output_buffer+=uts_name.machine;
84: output_buffer+=' ';
85: }
86: if (options.test(1) == 1) {
87: output_buffer+=uts_name.nodename;
88: output_buffer+=' ';
89: }
90: if (options.test(2) == 1) {
91: output_buffer+=uts_name.release;
92: output_buffer+=' ';
93: }
94: if (options.test(3) == 1) {
95: output_buffer+=uts_name.sysname;
96: output_buffer+=' ';
97: }
98: if (options.test(4) == 1) {
99: output_buffer+=uts_name.version;
100: output_buffer+=' ';
101: }
102: output_buffer.erase(output_buffer.length() - 1, 1);
103: std::cout << output_buffer + '\n';
104: return options.test(5);
105: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>