initial commit
[glibc.git] / sysdeps / unix / bsd / bsd4.4 / kfreebsd / uname.c
1 /* Copyright (C) 2002, 2006 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Bruno Haible <bruno@clisp.org>.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20 #include <sys/utsname.h>
21 #include <sys/sysctl.h>
22 #include <errno.h>
23 #include <string.h>
24
25 #define SYSNAME "GNU/kFreeBSD"
26 #define SYSNAME_LEN 13
27
28 /* Check for bounds in pre-processor */
29 #if SYSNAME_LEN > _UTSNAME_SYSNAME_LENGTH
30 #error
31 #endif
32
33 /* Put information about the system in NAME. */
34 int
35 __uname (struct utsname *name)
36 {
37 int request[2] = { CTL_KERN };
38 size_t len;
39
40 /* Fill sysname: "uname -s". */
41 strcpy (name->sysname, SYSNAME);
42
43 /* Fill nodename: "uname -n". Fetch sysctl "kern.hostname". */
44 request[1] = KERN_HOSTNAME;
45 len = sizeof (name->nodename);
46 if (__sysctl (request, 2, name->nodename, &len, NULL, 0) >= 0)
47 {
48 if (len < sizeof (name->nodename))
49 name->nodename[len] = '\0';
50 }
51
52 /* Fill release: "uname -r". Fetch sysctl "kern.osrelease". */
53 request[1] = KERN_OSRELEASE;
54 len = sizeof (name->release);
55 if (__sysctl (request, 2, name->release, &len, NULL, 0) >= 0)
56 {
57 if (len < sizeof (name->release))
58 name->release[len] = '\0';
59 }
60
61 /* Fill version: "uname -v". Fetch sysctl "kern.version". */
62 request[1] = KERN_VERSION;
63 len = sizeof (name->version);
64 if (__sysctl (request, 2, name->version, &len, NULL, 0) >= 0)
65 {
66 if (len < sizeof (name->version))
67 name->version[len] = '\0';
68 }
69
70 /* Remove trailing whitespace. Turn non-trailing whitespace to
71 spaces. */
72 {
73 char *p0 = name->version;
74 char *p = p0 + __strnlen (p0, sizeof (name->version));
75
76 while (p > p0 && (p[-1] == '\t' || p[-1] == '\n' || p[-1] == ' '))
77 *--p = '\0';
78
79 while (p > p0)
80 {
81 --p;
82 if (*p == '\t' || *p == '\n')
83 *p = ' ';
84 }
85 }
86
87 #ifdef __x86_64__
88 /* Check for bounds in pre-processor */
89 # if 7 > _UTSNAME_MACHINE_LENGTH
90 # error
91 # endif
92 /* Pristine FreeBSD kernel would return "amd64". Avoid that. */
93 strcpy (name->machine, "x86_64");
94 #else
95 /* Fill machine: "uname -m". Fetch sysctl "hw.machine". */
96 request[0] = CTL_HW;
97 request[1] = HW_MACHINE;
98 len = sizeof (name->machine);
99 if (__sysctl (request, 2, name->machine, &len, NULL, 0) >= 0)
100 {
101 if (len < sizeof (name->machine))
102 name->machine[len] = '\0';
103 }
104 #endif
105
106 return 0;
107 }
108 libc_hidden_def (__uname)
109 weak_alias (__uname, uname)
110 libc_hidden_def (uname)