Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 2 | /* uname -- print system information |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 3 | * 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 Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 7 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 8 | /* BB_AUDIT SUSv3 compliant */ |
| 9 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */ |
| 10 | |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 11 | /* 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 | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 26 | /* Further size reductions by Glenn McGrath and Manuel Novoa III. */ |
| 27 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 28 | /* 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 Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 33 | #include <sys/utsname.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 34 | #include "libbb.h" |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 35 | |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 36 | typedef struct { |
| 37 | struct utsname name; |
| 38 | char processor[8]; /* for "unknown" */ |
| 39 | } uname_info_t; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 40 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 41 | static const char options[] ALIGN1 = "snrvmpa"; |
| 42 | static const unsigned short utsname_offset[] ALIGN2 = { |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 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) |
| 49 | }; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 51 | int uname_main(int argc, char **argv); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 52 | int uname_main(int argc, char **argv) |
| 53 | { |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 54 | uname_info_t uname_info; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 55 | #if defined(__sparc__) && defined(__linux__) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | char *fake_sparc = getenv("FAKE_SPARC"); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 57 | #endif |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 58 | const unsigned short int *delta; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 59 | char toprint; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 60 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame^] | 61 | toprint = getopt32(argv, options); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 62 | |
| 63 | if (argc != optind) { |
| 64 | bb_show_usage(); |
| 65 | } |
| 66 | |
| 67 | if (toprint & (1 << 6)) { |
| 68 | toprint = 0x3f; |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | if (toprint == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | toprint = 1; /* sysname */ |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | if (uname(&uname_info.name) == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 76 | bb_error_msg_and_die("cannot get system name"); |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | #if defined(__sparc__) && defined(__linux__) |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 80 | if ((fake_sparc != NULL) |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 81 | && ((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 Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 89 | delta = utsname_offset; |
Manuel Novoa III | fa15f70 | 2002-03-25 02:37:20 +0000 | [diff] [blame] | 90 | do { |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 91 | if (toprint & 1) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 92 | printf(((char *)(&uname_info)) + *delta); |
Manuel Novoa III | fa15f70 | 2002-03-25 02:37:20 +0000 | [diff] [blame] | 93 | if (toprint > 1) { |
| 94 | putchar(' '); |
| 95 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 96 | } |
Manuel Novoa III | fa15f70 | 2002-03-25 02:37:20 +0000 | [diff] [blame] | 97 | ++delta; |
| 98 | } while (toprint >>= 1); |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 99 | putchar('\n'); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 100 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 101 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 102 | } |