xref: /haiku/src/system/libroot/posix/sys/uname.c (revision 445d4fd926c569e7b9ae28017da86280aaecbae2)
1 /*
2  * Copyright 2004-2007, Jérôme Duval jerome.duval@free.fr. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <sys/utsname.h>
8 
9 #include <errno.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <unistd.h>
13 
14 #include <OS.h>
15 #include <ByteOrder.h>
16 
17 #include <errno_private.h>
18 #include <system_revision.h>
19 
20 
21 int
22 uname(struct utsname *info)
23 {
24 	cpu_topology_node_info root;
25 	system_info systemInfo;
26 	const char *platform;
27 	const char *haikuRevision;
28 	uint32 count = 1;
29 	status_t error;
30 
31 	if (!info) {
32 		__set_errno(B_BAD_VALUE);
33 		return -1;
34 	}
35 
36 	get_system_info(&systemInfo);
37 
38 	strlcpy(info->sysname, "Haiku", sizeof(info->sysname));
39 
40 	haikuRevision = __get_haiku_revision();
41 	if (haikuRevision[0] != '\0')
42 		snprintf(info->version, sizeof(info->version), "%s ", haikuRevision);
43 	else
44 		info->version[0] = '\0';
45 	strlcat(info->version, systemInfo.kernel_build_date, sizeof(info->version));
46 	strlcat(info->version, " ", sizeof(info->version));
47 	strlcat(info->version, systemInfo.kernel_build_time, sizeof(info->version));
48 	snprintf(info->release, sizeof(info->release), "%" B_PRId64,
49 		systemInfo.kernel_version);
50 
51 	error = get_cpu_topology_info(&root, &count);
52 	if (error != B_OK || count < 1)
53 		platform = "unknown";
54 	else {
55 		switch (root.data.root.platform) {
56 			case B_CPU_x86:
57 				platform = "BePC";
58 				break;
59 			case B_CPU_x86_64:
60 				platform = "x86_64";
61 				break;
62 			case B_CPU_PPC:
63 				platform = "ppc";
64 				break;
65 			case B_CPU_PPC_64:
66 				platform = "ppc64";
67 				break;
68 			case B_CPU_M68K:
69 				platform = "m68k";
70 				break;
71 			case B_CPU_ARM:
72 				platform = "arm";
73 				break;
74 			case B_CPU_ARM_64:
75 				platform = "aarch64";
76 				break;
77 			case B_CPU_RISC_V:
78 				platform = "riscv64";
79 				break;
80 			case B_CPU_UNKNOWN:
81 			default:
82 				platform = "unknown";
83 				break;
84 		}
85 	}
86 
87 	strlcpy(info->machine, platform, sizeof(info->machine));
88 
89 	if (gethostname(info->nodename, sizeof(info->nodename)) != 0)
90 		strlcpy(info->nodename, "unknown", sizeof(info->nodename));
91 
92 	return 0;
93 }
94 
95