Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/String.h>
28#include <AK/StringBuilder.h>
29#include <AK/Vector.h>
30#include <stdio.h>
31#include <sys/utsname.h>
32#include <unistd.h>
33
34int main(int argc, char** argv)
35{
36 if (pledge("stdio", nullptr) < 0) {
37 perror("pledge");
38 return 1;
39 }
40
41 utsname uts;
42 int rc = uname(&uts);
43 if (rc < 0) {
44 perror("uname() failed");
45 return 0;
46 }
47 bool flag_s = false;
48 bool flag_n = false;
49 bool flag_r = false;
50 bool flag_m = false;
51 if (argc == 1) {
52 flag_s = true;
53 } else {
54 for (int i = 1; i < argc; ++i) {
55 if (argv[i][0] == '-') {
56 for (const char* o = &argv[i][1]; *o; ++o) {
57 switch (*o) {
58 case 's':
59 flag_s = true;
60 break;
61 case 'n':
62 flag_n = true;
63 break;
64 case 'r':
65 flag_r = true;
66 break;
67 case 'm':
68 flag_m = true;
69 break;
70 case 'a':
71 flag_s = flag_n = flag_r = flag_m = true;
72 break;
73 }
74 }
75 }
76 }
77 }
78 if (!flag_s && !flag_n && !flag_r && !flag_m)
79 flag_s = true;
80 Vector<String> parts;
81 if (flag_s)
82 parts.append(uts.sysname);
83 if (flag_n)
84 parts.append(uts.nodename);
85 if (flag_r)
86 parts.append(uts.release);
87 if (flag_m)
88 parts.append(uts.machine);
89 StringBuilder builder;
90 builder.join(' ', parts);
91 puts(builder.to_string().characters());
92 return 0;
93}