xref: /haiku/src/system/libroot/posix/sys/uname.c (revision 4a850ca730d8282b5b924e49e09b4ba4d6db7f54)
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 				/* The minimal ARM version emulated by QEMU
73 				 * XXX: use armv6 (raspberry Pi)?
74 				 * XXX: should we really use B_HOST_IS_LENDIAN here?
75 				 * XXX: use real cpu version as on Linux?
76 				 *	cf. http://git.qemu.org/?p=qemu.git;a=blob;f=linux-user/uname.c
77 				 */
78 #if B_HOST_IS_LENDIAN
79 				platform = "armv5tel";
80 #else
81 				platform = "armv5teb";
82 #endif
83 				break;
84 			case B_CPU_ARM_64:
85 				platform = "aarch64";
86 				break;
87 			case B_CPU_ALPHA:
88 				platform = "alpha";
89 				break;
90 			case B_CPU_MIPS:
91 				platform = "mips";
92 				break;
93 			case B_CPU_SH:
94 				platform = "sh4";
95 				break;
96 			case B_CPU_RISC_V:
97 				platform = "riscv64";
98 				break;
99 			case B_CPU_UNKNOWN:
100 			default:
101 				platform = "unknown";
102 				break;
103 		}
104 	}
105 
106 	strlcpy(info->machine, platform, sizeof(info->machine));
107 
108 	if (gethostname(info->nodename, sizeof(info->nodename)) != 0)
109 		strlcpy(info->nodename, "unknown", sizeof(info->nodename));
110 
111 	return 0;
112 }
113 
114