blob: 2eecb5d6d1b40b4d0c038b6ebb0294a0c1800fda [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc8fdb561999-10-26 05:21:02 +00002/* uname -- print system information
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00003 * Copyright (C) 1989-1999 Free Software Foundation, Inc.
4 *
5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6 */
Eric Andersenc8fdb561999-10-26 05:21:02 +00007
Manuel Novoa III cad53642003-03-19 09:13:01 +00008/* BB_AUDIT SUSv3 compliant */
9/* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */
10
Eric Andersenc8fdb561999-10-26 05:21:02 +000011/* Option Example
12
13 -s, --sysname SunOS
14 -n, --nodename rocky8
15 -r, --release 4.0
16 -v, --version
17 -m, --machine sun
18 -a, --all SunOS rocky8 4.0 sun
19
20 The default behavior is equivalent to `-s'.
21
22 David MacKenzie <djm@gnu.ai.mit.edu> */
23
24/* Busyboxed by Erik Andersen */
25
Manuel Novoa III 6509f922001-12-05 04:21:30 +000026/* Further size reductions by Glenn McGrath and Manuel Novoa III. */
27
Manuel Novoa III cad53642003-03-19 09:13:01 +000028/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
29 *
30 * Now does proper error checking on i/o. Plus some further space savings.
31 */
32
Eric Andersenc8fdb561999-10-26 05:21:02 +000033#include <sys/utsname.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000034#include "libbb.h"
Eric Andersenc8fdb561999-10-26 05:21:02 +000035
Manuel Novoa III 6509f922001-12-05 04:21:30 +000036typedef struct {
37 struct utsname name;
38 char processor[8]; /* for "unknown" */
39} uname_info_t;
Eric Andersenc8fdb561999-10-26 05:21:02 +000040
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000041static const char options[] ALIGN1 = "snrvmpa";
42static const unsigned short utsname_offset[] ALIGN2 = {
Manuel Novoa III 6509f922001-12-05 04:21:30 +000043 offsetof(uname_info_t,name.sysname),
44 offsetof(uname_info_t,name.nodename),
45 offsetof(uname_info_t,name.release),
46 offsetof(uname_info_t,name.version),
47 offsetof(uname_info_t,name.machine),
48 offsetof(uname_info_t,processor)
49};
Eric Andersenc8fdb561999-10-26 05:21:02 +000050
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000051int uname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Eric Andersenc8fdb561999-10-26 05:21:02 +000052int uname_main(int argc, char **argv)
53{
Manuel Novoa III 6509f922001-12-05 04:21:30 +000054 uname_info_t uname_info;
Eric Andersenc8fdb561999-10-26 05:21:02 +000055#if defined(__sparc__) && defined(__linux__)
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 char *fake_sparc = getenv("FAKE_SPARC");
Eric Andersenc8fdb561999-10-26 05:21:02 +000057#endif
Manuel Novoa III 6509f922001-12-05 04:21:30 +000058 const unsigned short int *delta;
Manuel Novoa III cad53642003-03-19 09:13:01 +000059 char toprint;
Eric Andersenc8fdb561999-10-26 05:21:02 +000060
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000061 toprint = getopt32(argv, options);
Manuel Novoa III cad53642003-03-19 09:13:01 +000062
63 if (argc != optind) {
64 bb_show_usage();
65 }
66
67 if (toprint & (1 << 6)) {
68 toprint = 0x3f;
Manuel Novoa III 6509f922001-12-05 04:21:30 +000069 }
70
71 if (toprint == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 toprint = 1; /* sysname */
Manuel Novoa III 6509f922001-12-05 04:21:30 +000073 }
74
75 if (uname(&uname_info.name) == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000076 bb_error_msg_and_die("cannot get system name");
Manuel Novoa III 6509f922001-12-05 04:21:30 +000077 }
78
79#if defined(__sparc__) && defined(__linux__)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000080 if ((fake_sparc != NULL)
Manuel Novoa III 6509f922001-12-05 04:21:30 +000081 && ((fake_sparc[0] == 'y')
82 || (fake_sparc[0] == 'Y'))) {
83 strcpy(uname_info.name.machine, "sparc");
84 }
85#endif
86
87 strcpy(uname_info.processor, "unknown");
88
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000089 delta = utsname_offset;
Manuel Novoa III fa15f702002-03-25 02:37:20 +000090 do {
Manuel Novoa III 6509f922001-12-05 04:21:30 +000091 if (toprint & 1) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000092 printf(((char *)(&uname_info)) + *delta);
Manuel Novoa III fa15f702002-03-25 02:37:20 +000093 if (toprint > 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +000094 bb_putchar(' ');
Manuel Novoa III fa15f702002-03-25 02:37:20 +000095 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 }
Manuel Novoa III fa15f702002-03-25 02:37:20 +000097 ++delta;
98 } while (toprint >>= 1);
Denis Vlasenko4daad902007-09-27 10:20:47 +000099 bb_putchar('\n');
Eric Andersenc8fdb561999-10-26 05:21:02 +0000100
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000101 fflush_stdout_and_exit(EXIT_SUCCESS);
Eric Andersenc8fdb561999-10-26 05:21:02 +0000102}