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 <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 34 | #include <stdlib.h> |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 35 | #include <stddef.h> |
Eric Andersen | eba8ed7 | 2001-03-09 14:36:42 +0000 | [diff] [blame] | 36 | #include <string.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 37 | #include <unistd.h> |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 38 | #include <sys/types.h> |
| 39 | #include <sys/utsname.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 40 | #include "busybox.h" |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 41 | |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 42 | typedef struct { |
| 43 | struct utsname name; |
| 44 | char processor[8]; /* for "unknown" */ |
| 45 | } uname_info_t; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 46 | |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 47 | static const char options[] = "snrvmpa"; |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 48 | static const unsigned short int utsname_offset[] = { |
| 49 | offsetof(uname_info_t,name.sysname), |
| 50 | offsetof(uname_info_t,name.nodename), |
| 51 | offsetof(uname_info_t,name.release), |
| 52 | offsetof(uname_info_t,name.version), |
| 53 | offsetof(uname_info_t,name.machine), |
| 54 | offsetof(uname_info_t,processor) |
| 55 | }; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 56 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 57 | int uname_main(int argc, char **argv); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 58 | int uname_main(int argc, char **argv) |
| 59 | { |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 60 | uname_info_t uname_info; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 61 | #if defined(__sparc__) && defined(__linux__) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 62 | char *fake_sparc = getenv("FAKE_SPARC"); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 63 | #endif |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 64 | const unsigned short int *delta; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 65 | char toprint; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 67 | toprint = getopt32(argc, argv, options); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 68 | |
| 69 | if (argc != optind) { |
| 70 | bb_show_usage(); |
| 71 | } |
| 72 | |
| 73 | if (toprint & (1 << 6)) { |
| 74 | toprint = 0x3f; |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | if (toprint == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 78 | toprint = 1; /* sysname */ |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | if (uname(&uname_info.name) == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 82 | bb_error_msg_and_die("cannot get system name"); |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | #if defined(__sparc__) && defined(__linux__) |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 86 | if ((fake_sparc != NULL) |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 87 | && ((fake_sparc[0] == 'y') |
| 88 | || (fake_sparc[0] == 'Y'))) { |
| 89 | strcpy(uname_info.name.machine, "sparc"); |
| 90 | } |
| 91 | #endif |
| 92 | |
| 93 | strcpy(uname_info.processor, "unknown"); |
| 94 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 95 | delta = utsname_offset; |
Manuel Novoa III | fa15f70 | 2002-03-25 02:37:20 +0000 | [diff] [blame] | 96 | do { |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 97 | if (toprint & 1) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 98 | printf(((char *)(&uname_info)) + *delta); |
Manuel Novoa III | fa15f70 | 2002-03-25 02:37:20 +0000 | [diff] [blame] | 99 | if (toprint > 1) { |
| 100 | putchar(' '); |
| 101 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 102 | } |
Manuel Novoa III | fa15f70 | 2002-03-25 02:37:20 +0000 | [diff] [blame] | 103 | ++delta; |
| 104 | } while (toprint >>= 1); |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 105 | putchar('\n'); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 106 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 107 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 108 | } |