blob: e28285c441c6ca027b7b88bd95d7ff0f6708763d [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
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +000020 The default behavior is equivalent to '-s'.
Eric Andersenc8fdb561999-10-26 05:21:02 +000021
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";
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +000042static const unsigned short utsname_offset[] = {
43 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)
Manuel Novoa III 6509f922001-12-05 04:21:30 +000049};
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;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000052int uname_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenc8fdb561999-10-26 05:21:02 +000053{
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
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +000058 const unsigned short *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
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +000063 if (argv[optind]) { /* coreutils-6.9 compat */
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 bb_show_usage();
65 }
66
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +000067 if (toprint & (1 << 6)) { /* -a => all opts on */
Manuel Novoa III cad53642003-03-19 09:13:01 +000068 toprint = 0x3f;
Manuel Novoa III 6509f922001-12-05 04:21:30 +000069 }
70
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +000071 if (toprint == 0) { /* no opts => -s (sysname) */
72 toprint = 1;
Manuel Novoa III 6509f922001-12-05 04:21:30 +000073 }
74
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +000075 uname(&uname_info.name); /* never fails */
Manuel Novoa III 6509f922001-12-05 04:21:30 +000076
77#if defined(__sparc__) && defined(__linux__)
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +000078 if (fake_sparc && (fake_sparc[0] | 0x20) == 'y') {
Manuel Novoa III 6509f922001-12-05 04:21:30 +000079 strcpy(uname_info.name.machine, "sparc");
80 }
81#endif
82
83 strcpy(uname_info.processor, "unknown");
84
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000085 delta = utsname_offset;
Manuel Novoa III fa15f702002-03-25 02:37:20 +000086 do {
Manuel Novoa III 6509f922001-12-05 04:21:30 +000087 if (toprint & 1) {
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +000088 /* printf would not be safe here */
89 fputs((char *)(&uname_info) + *delta, stdout);
Manuel Novoa III fa15f702002-03-25 02:37:20 +000090 if (toprint > 1) {
Denis Vlasenko4daad902007-09-27 10:20:47 +000091 bb_putchar(' ');
Manuel Novoa III fa15f702002-03-25 02:37:20 +000092 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000093 }
Manuel Novoa III fa15f702002-03-25 02:37:20 +000094 ++delta;
95 } while (toprint >>= 1);
Denis Vlasenko4daad902007-09-27 10:20:47 +000096 bb_putchar('\n');
Eric Andersenc8fdb561999-10-26 05:21:02 +000097
Denis Vlasenkoe0a7fc52008-07-02 11:14:59 +000098 fflush_stdout_and_exit(EXIT_SUCCESS); /* coreutils-6.9 compat */
Eric Andersenc8fdb561999-10-26 05:21:02 +000099}