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 | |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 20 | The default behavior is equivalent to '-s'. |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 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"; |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 42 | static 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 | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 49 | }; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 51 | int uname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 52 | int uname_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 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 |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 58 | const unsigned short *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 | |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 63 | if (argv[optind]) { /* coreutils-6.9 compat */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 64 | bb_show_usage(); |
| 65 | } |
| 66 | |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 67 | if (toprint & (1 << 6)) { /* -a => all opts on */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 68 | toprint = 0x3f; |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 71 | if (toprint == 0) { /* no opts => -s (sysname) */ |
| 72 | toprint = 1; |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 75 | uname(&uname_info.name); /* never fails */ |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 76 | |
| 77 | #if defined(__sparc__) && defined(__linux__) |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 78 | if (fake_sparc && (fake_sparc[0] | 0x20) == 'y') { |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 79 | strcpy(uname_info.name.machine, "sparc"); |
| 80 | } |
| 81 | #endif |
| 82 | |
| 83 | strcpy(uname_info.processor, "unknown"); |
| 84 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 85 | delta = utsname_offset; |
Manuel Novoa III | fa15f70 | 2002-03-25 02:37:20 +0000 | [diff] [blame] | 86 | do { |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 87 | if (toprint & 1) { |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 88 | /* printf would not be safe here */ |
| 89 | fputs((char *)(&uname_info) + *delta, stdout); |
Manuel Novoa III | fa15f70 | 2002-03-25 02:37:20 +0000 | [diff] [blame] | 90 | if (toprint > 1) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 91 | bb_putchar(' '); |
Manuel Novoa III | fa15f70 | 2002-03-25 02:37:20 +0000 | [diff] [blame] | 92 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 93 | } |
Manuel Novoa III | fa15f70 | 2002-03-25 02:37:20 +0000 | [diff] [blame] | 94 | ++delta; |
| 95 | } while (toprint >>= 1); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 96 | bb_putchar('\n'); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 97 | |
Denis Vlasenko | e0a7fc5 | 2008-07-02 11:14:59 +0000 | [diff] [blame] | 98 | fflush_stdout_and_exit(EXIT_SUCCESS); /* coreutils-6.9 compat */ |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 99 | } |