OpenCoverage

uname.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/uname.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* uname -- print system information-
2-
3 Copyright (C) 1989-2018 Free Software Foundation, Inc.-
4-
5 This program is free software: you can redistribute it and/or modify-
6 it under the terms of the GNU General Public License as published by-
7 the Free Software Foundation, either version 3 of the License, or-
8 (at your option) any later version.-
9-
10 This program 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-
13 GNU General Public License for more details.-
14-
15 You should have received a copy of the GNU General Public License-
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */-
17-
18/* Written by David MacKenzie <djm@gnu.ai.mit.edu> */-
19-
20#include <config.h>-
21#include <stdio.h>-
22#include <sys/types.h>-
23#include <sys/utsname.h>-
24#include <getopt.h>-
25-
26#if HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H-
27# include <sys/systeminfo.h>-
28#endif-
29-
30#if HAVE_SYS_SYSCTL_H-
31# if HAVE_SYS_PARAM_H-
32# include <sys/param.h> /* needed for OpenBSD 3.0 */-
33# endif-
34# include <sys/sysctl.h>-
35# ifdef HW_MODEL-
36# ifdef HW_MACHINE_ARCH-
37/* E.g., FreeBSD 4.5, NetBSD 1.5.2 */-
38# define UNAME_HARDWARE_PLATFORM HW_MODEL-
39# define UNAME_PROCESSOR HW_MACHINE_ARCH-
40# else-
41/* E.g., OpenBSD 3.0 */-
42# define UNAME_PROCESSOR HW_MODEL-
43# endif-
44# endif-
45#endif-
46-
47#ifdef __APPLE__-
48# include <mach/machine.h>-
49# include <mach-o/arch.h>-
50#endif-
51-
52#include "system.h"-
53#include "die.h"-
54#include "error.h"-
55#include "quote.h"-
56#include "uname.h"-
57-
58/* The official name of this program (e.g., no 'g' prefix). */-
59#define PROGRAM_NAME (uname_mode == UNAME_UNAME ? "uname" : "arch")-
60-
61#define AUTHORS proper_name ("David MacKenzie")-
62#define ARCH_AUTHORS "David MacKenzie", "Karel Zak"-
63-
64/* Values that are bitwise or'd into 'toprint'. */-
65/* Kernel name. */-
66#define PRINT_KERNEL_NAME 1-
67-
68/* Node name on a communications network. */-
69#define PRINT_NODENAME 2-
70-
71/* Kernel release. */-
72#define PRINT_KERNEL_RELEASE 4-
73-
74/* Kernel version. */-
75#define PRINT_KERNEL_VERSION 8-
76-
77/* Machine hardware name. */-
78#define PRINT_MACHINE 16-
79-
80/* Processor type. */-
81#define PRINT_PROCESSOR 32-
82-
83/* Hardware platform. */-
84#define PRINT_HARDWARE_PLATFORM 64-
85-
86/* Operating system. */-
87#define PRINT_OPERATING_SYSTEM 128-
88-
89static struct option const uname_long_options[] =-
90{-
91 {"all", no_argument, NULL, 'a'},-
92 {"kernel-name", no_argument, NULL, 's'},-
93 {"sysname", no_argument, NULL, 's'}, /* Obsolescent. */-
94 {"nodename", no_argument, NULL, 'n'},-
95 {"kernel-release", no_argument, NULL, 'r'},-
96 {"release", no_argument, NULL, 'r'}, /* Obsolescent. */-
97 {"kernel-version", no_argument, NULL, 'v'},-
98 {"machine", no_argument, NULL, 'm'},-
99 {"processor", no_argument, NULL, 'p'},-
100 {"hardware-platform", no_argument, NULL, 'i'},-
101 {"operating-system", no_argument, NULL, 'o'},-
102 {GETOPT_HELP_OPTION_DECL},-
103 {GETOPT_VERSION_OPTION_DECL},-
104 {NULL, 0, NULL, 0}-
105};-
106-
107static struct option const arch_long_options[] =-
108{-
109 {GETOPT_HELP_OPTION_DECL},-
110 {GETOPT_VERSION_OPTION_DECL},-
111 {NULL, 0, NULL, 0}-
112};-
113-
114void-
115usage (int status)-
116{-
117 if (status != EXIT_SUCCESS)
status != 0Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • uname
FALSEevaluated 21 times by 1 test
Evaluated by:
  • uname
3-21
118 emit_try_help ();
executed 3 times by 1 test: end of block
Executed by:
  • uname
3
119 else-
120 {-
121 printf (_("Usage: %s [OPTION]...\n"), program_name);-
122-
123 if (uname_mode == UNAME_UNAME)
uname_mode == 1Description
TRUEevaluated 21 times by 1 test
Evaluated by:
  • uname
FALSEnever evaluated
0-21
124 {-
125 fputs (_("\-
126Print certain system information. With no OPTION, same as -s.\n\-
127\n\-
128 -a, --all print all information, in the following order,\n\-
129 except omit -p and -i if unknown:\n\-
130 -s, --kernel-name print the kernel name\n\-
131 -n, --nodename print the network node hostname\n\-
132 -r, --kernel-release print the kernel release\n\-
133"), stdout);-
134 fputs (_("\-
135 -v, --kernel-version print the kernel version\n\-
136 -m, --machine print the machine hardware name\n\-
137 -p, --processor print the processor type (non-portable)\n\-
138 -i, --hardware-platform print the hardware platform (non-portable)\n\-
139 -o, --operating-system print the operating system\n\-
140"), stdout);-
141 }
executed 21 times by 1 test: end of block
Executed by:
  • uname
21
142 else-
143 {-
144 fputs (_("\-
145Print machine architecture.\n\-
146\n\-
147"), stdout);-
148 }
never executed: end of block
0
149-
150 fputs (HELP_OPTION_DESCRIPTION, stdout);-
151 fputs (VERSION_OPTION_DESCRIPTION, stdout);-
152 emit_ancillary_info (PROGRAM_NAME);-
153 }
executed 21 times by 1 test: end of block
Executed by:
  • uname
21
154 exit (status);
executed 24 times by 1 test: exit (status);
Executed by:
  • uname
24
155}-
156-
157/* Print ELEMENT, preceded by a space if something has already been-
158 printed. */-
159-
160static void-
161print_element (char const *element)-
162{-
163 static bool printed;-
164 if (printed)
printedDescription
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • uname
0-2
165 putchar (' ');
never executed: putchar_unlocked (' ');
0
166 printed = true;-
167 fputs (element, stdout);-
168}
executed 2 times by 1 test: end of block
Executed by:
  • uname
2
169-
170-
171/* Set all the option flags according to the switches specified.-
172 Return the mask indicating which elements to print. */-
173-
174static int-
175decode_switches (int argc, char **argv)-
176{-
177 int c;-
178 unsigned int toprint = 0;-
179-
180 if (uname_mode == UNAME_ARCH)
uname_mode == 2Description
TRUEnever evaluated
FALSEevaluated 29 times by 1 test
Evaluated by:
  • uname
0-29
181 {-
182 while ((c = getopt_long (argc, argv, "",
(c = getopt_lo... *)0) )) != -1Description
TRUEnever evaluated
FALSEnever evaluated
0
183 arch_long_options, NULL)) != -1)
(c = getopt_lo... *)0) )) != -1Description
TRUEnever evaluated
FALSEnever evaluated
0
184 {-
185 switch (c)-
186 {-
187 case_GETOPT_HELP_CHAR;
never executed: break;
never executed: case GETOPT_HELP_CHAR:
0
188-
189 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, ARCH_AUTHORS);
never executed: exit ( 0 );
never executed: break;
never executed: case GETOPT_VERSION_CHAR:
0
190-
191 default:
never executed: default:
0
192 usage (EXIT_FAILURE);-
193 }
never executed: end of block
0
194 }-
195 toprint = PRINT_MACHINE;-
196 }
never executed: end of block
0
197 else-
198 {-
199 while ((c = getopt_long (argc, argv, "asnrvmpio",
(c = getopt_lo... *)0) )) != -1Description
TRUEevaluated 45 times by 1 test
Evaluated by:
  • uname
FALSEevaluated 2 times by 1 test
Evaluated by:
  • uname
2-45
200 uname_long_options, NULL)) != -1)
(c = getopt_lo... *)0) )) != -1Description
TRUEevaluated 45 times by 1 test
Evaluated by:
  • uname
FALSEevaluated 2 times by 1 test
Evaluated by:
  • uname
2-45
201 {-
202 switch (c)-
203 {-
204 case 'a':
executed 2 times by 1 test: case 'a':
Executed by:
  • uname
2
205 toprint = UINT_MAX;-
206 break;
executed 2 times by 1 test: break;
Executed by:
  • uname
2
207-
208 case 's':
executed 2 times by 1 test: case 's':
Executed by:
  • uname
2
209 toprint |= PRINT_KERNEL_NAME;-
210 break;
executed 2 times by 1 test: break;
Executed by:
  • uname
2
211-
212 case 'n':
executed 2 times by 1 test: case 'n':
Executed by:
  • uname
2
213 toprint |= PRINT_NODENAME;-
214 break;
executed 2 times by 1 test: break;
Executed by:
  • uname
2
215-
216 case 'r':
executed 2 times by 1 test: case 'r':
Executed by:
  • uname
2
217 toprint |= PRINT_KERNEL_RELEASE;-
218 break;
executed 2 times by 1 test: break;
Executed by:
  • uname
2
219-
220 case 'v':
executed 2 times by 1 test: case 'v':
Executed by:
  • uname
2
221 toprint |= PRINT_KERNEL_VERSION;-
222 break;
executed 2 times by 1 test: break;
Executed by:
  • uname
2
223-
224 case 'm':
executed 2 times by 1 test: case 'm':
Executed by:
  • uname
2
225 toprint |= PRINT_MACHINE;-
226 break;
executed 2 times by 1 test: break;
Executed by:
  • uname
2
227-
228 case 'p':
executed 2 times by 1 test: case 'p':
Executed by:
  • uname
2
229 toprint |= PRINT_PROCESSOR;-
230 break;
executed 2 times by 1 test: break;
Executed by:
  • uname
2
231-
232 case 'i':
executed 2 times by 1 test: case 'i':
Executed by:
  • uname
2
233 toprint |= PRINT_HARDWARE_PLATFORM;-
234 break;
executed 2 times by 1 test: break;
Executed by:
  • uname
2
235-
236 case 'o':
executed 2 times by 1 test: case 'o':
Executed by:
  • uname
2
237 toprint |= PRINT_OPERATING_SYSTEM;-
238 break;
executed 2 times by 1 test: break;
Executed by:
  • uname
2
239-
240 case_GETOPT_HELP_CHAR;
never executed: break;
executed 21 times by 1 test: case GETOPT_HELP_CHAR:
Executed by:
  • uname
0-21
241-
242 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
executed 3 times by 1 test: exit ( 0 );
Executed by:
  • uname
never executed: break;
executed 3 times by 1 test: case GETOPT_VERSION_CHAR:
Executed by:
  • uname
0-3
243-
244 default:
executed 3 times by 1 test: default:
Executed by:
  • uname
3
245 usage (EXIT_FAILURE);-
246 }
never executed: end of block
0
247 }-
248 }
executed 2 times by 1 test: end of block
Executed by:
  • uname
2
249-
250 if (argc != optind)
argc != optindDescription
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • uname
0-2
251 {-
252 error (0, 0, _("extra operand %s"), quote (argv[optind]));-
253 usage (EXIT_FAILURE);-
254 }
never executed: end of block
0
255-
256 return toprint;
executed 2 times by 1 test: return toprint;
Executed by:
  • uname
2
257}-
258-
259int-
260main (int argc, char **argv)-
261{-
262 static char const unknown[] = "unknown";-
263-
264 /* Mask indicating which elements to print. */-
265 unsigned int toprint = 0;-
266-
267 initialize_main (&argc, &argv);-
268 set_program_name (argv[0]);-
269 setlocale (LC_ALL, "");-
270 bindtextdomain (PACKAGE, LOCALEDIR);-
271 textdomain (PACKAGE);-
272-
273 atexit (close_stdout);-
274-
275 toprint = decode_switches (argc, argv);-
276-
277 if (toprint == 0)
toprint == 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • uname
FALSEnever evaluated
0-2
278 toprint = PRINT_KERNEL_NAME;
executed 2 times by 1 test: toprint = 1;
Executed by:
  • uname
2
279-
280 if (toprint
toprint & (1 | 2 | 4 | 8 | 16)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • uname
FALSEnever evaluated
0-2
281 & (PRINT_KERNEL_NAME | PRINT_NODENAME | PRINT_KERNEL_RELEASE
toprint & (1 | 2 | 4 | 8 | 16)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • uname
FALSEnever evaluated
0-2
282 | PRINT_KERNEL_VERSION | PRINT_MACHINE))
toprint & (1 | 2 | 4 | 8 | 16)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • uname
FALSEnever evaluated
0-2
283 {-
284 struct utsname name;-
285-
286 if (uname (&name) == -1)
uname (&name) == -1Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • uname
0-2
287 die (EXIT_FAILURE, errno, _("cannot get system name"));
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), dcgettext (((void *)0), \"cannot get system name\", 5)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "cannot get system name" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "cannot get system name" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
0
288-
289 if (toprint & PRINT_KERNEL_NAME)
toprint & 1Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • uname
FALSEnever evaluated
0-2
290 print_element (name.sysname);
executed 2 times by 1 test: print_element (name.sysname);
Executed by:
  • uname
2
291 if (toprint & PRINT_NODENAME)
toprint & 2Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • uname
0-2
292 print_element (name.nodename);
never executed: print_element (name.nodename);
0
293 if (toprint & PRINT_KERNEL_RELEASE)
toprint & 4Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • uname
0-2
294 print_element (name.release);
never executed: print_element (name.release);
0
295 if (toprint & PRINT_KERNEL_VERSION)
toprint & 8Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • uname
0-2
296 print_element (name.version);
never executed: print_element (name.version);
0
297 if (toprint & PRINT_MACHINE)
toprint & 16Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • uname
0-2
298 print_element (name.machine);
never executed: print_element (name.machine);
0
299 }
executed 2 times by 1 test: end of block
Executed by:
  • uname
2
300-
301 if (toprint & PRINT_PROCESSOR)
toprint & 32Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • uname
0-2
302 {-
303 char const *element = unknown;-
304#if HAVE_SYSINFO && defined SI_ARCHITECTURE-
305 {-
306 static char processor[257];-
307 if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor))-
308 element = processor;-
309 }-
310#endif-
311#ifdef UNAME_PROCESSOR-
312 if (element == unknown)-
313 {-
314 static char processor[257];-
315 size_t s = sizeof processor;-
316 static int mib[] = { CTL_HW, UNAME_PROCESSOR };-
317 if (sysctl (mib, 2, processor, &s, 0, 0) >= 0)-
318 element = processor;-
319-
320# ifdef __APPLE__-
321 /* This kludge works around a bug in Mac OS X. */-
322 if (element == unknown)-
323 {-
324 cpu_type_t cputype;-
325 size_t cs = sizeof cputype;-
326 NXArchInfo const *ai;-
327 if (sysctlbyname ("hw.cputype", &cputype, &cs, NULL, 0) == 0-
328 && (ai = NXGetArchInfoFromCpuType (cputype,-
329 CPU_SUBTYPE_MULTIPLE))-
330 != NULL)-
331 element = ai->name;-
332-
333 /* Hack "safely" around the ppc vs. powerpc return value. */-
334 if (cputype == CPU_TYPE_POWERPC-
335 && STRNCMP_LIT (element, "ppc") == 0)-
336 element = "powerpc";-
337 }-
338# endif-
339 }-
340#endif-
341 if (! (toprint == UINT_MAX && element == unknown))
toprint == (0x...fff * 2U + 1U)Description
TRUEnever evaluated
FALSEnever evaluated
element == unknownDescription
TRUEnever evaluated
FALSEnever evaluated
0
342 print_element (element);
never executed: print_element (element);
0
343 }
never executed: end of block
0
344-
345 if (toprint & PRINT_HARDWARE_PLATFORM)
toprint & 64Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • uname
0-2
346 {-
347 char const *element = unknown;-
348#if HAVE_SYSINFO && defined SI_PLATFORM-
349 {-
350 static char hardware_platform[257];-
351 if (0 <= sysinfo (SI_PLATFORM,-
352 hardware_platform, sizeof hardware_platform))-
353 element = hardware_platform;-
354 }-
355#endif-
356#ifdef UNAME_HARDWARE_PLATFORM-
357 if (element == unknown)-
358 {-
359 static char hardware_platform[257];-
360 size_t s = sizeof hardware_platform;-
361 static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM };-
362 if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0)-
363 element = hardware_platform;-
364 }-
365#endif-
366 if (! (toprint == UINT_MAX && element == unknown))
toprint == (0x...fff * 2U + 1U)Description
TRUEnever evaluated
FALSEnever evaluated
element == unknownDescription
TRUEnever evaluated
FALSEnever evaluated
0
367 print_element (element);
never executed: print_element (element);
0
368 }
never executed: end of block
0
369-
370 if (toprint & PRINT_OPERATING_SYSTEM)
toprint & 128Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • uname
0-2
371 print_element (HOST_OPERATING_SYSTEM);
never executed: print_element ("GNU/Linux");
0
372-
373 putchar ('\n');-
374-
375 return EXIT_SUCCESS;
executed 2 times by 1 test: return 0 ;
Executed by:
  • uname
2
376}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2