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 |
| 3 | Copyright (C) 1989-1999 Free Software Foundation, Inc. |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2, or (at your option) |
| 8 | any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software Foundation, |
| 17 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 18 | |
| 19 | /* Option Example |
| 20 | |
| 21 | -s, --sysname SunOS |
| 22 | -n, --nodename rocky8 |
| 23 | -r, --release 4.0 |
| 24 | -v, --version |
| 25 | -m, --machine sun |
| 26 | -a, --all SunOS rocky8 4.0 sun |
| 27 | |
| 28 | The default behavior is equivalent to `-s'. |
| 29 | |
| 30 | David MacKenzie <djm@gnu.ai.mit.edu> */ |
| 31 | |
| 32 | /* Busyboxed by Erik Andersen */ |
| 33 | |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 34 | /* Further size reductions by Glenn McGrath and Manuel Novoa III. */ |
| 35 | |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 36 | #include <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 37 | #include <stdlib.h> |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 38 | #include <stddef.h> |
Eric Andersen | eba8ed7 | 2001-03-09 14:36:42 +0000 | [diff] [blame] | 39 | #include <string.h> |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 40 | #include <sys/types.h> |
| 41 | #include <sys/utsname.h> |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 42 | #include <getopt.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 43 | #include "busybox.h" |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 44 | |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 45 | typedef struct { |
| 46 | struct utsname name; |
| 47 | char processor[8]; /* for "unknown" */ |
| 48 | } uname_info_t; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 49 | |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 50 | static const char options[] = "snrvmpa"; |
| 51 | static const char flags[] = "\x01\x02\x04\x08\x10\x20\x3f"; |
| 52 | static const unsigned short int utsname_offset[] = { |
| 53 | offsetof(uname_info_t,name.sysname), |
| 54 | offsetof(uname_info_t,name.nodename), |
| 55 | offsetof(uname_info_t,name.release), |
| 56 | offsetof(uname_info_t,name.version), |
| 57 | offsetof(uname_info_t,name.machine), |
| 58 | offsetof(uname_info_t,processor) |
| 59 | }; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 60 | |
| 61 | int uname_main(int argc, char **argv) |
| 62 | { |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 63 | uname_info_t uname_info; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 64 | |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 65 | #if defined(__sparc__) && defined(__linux__) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | char *fake_sparc = getenv("FAKE_SPARC"); |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 67 | #endif |
| 68 | |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 69 | const unsigned short int *delta; |
| 70 | int opt; |
| 71 | char toprint = 0; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 72 | |
Manuel Novoa III | 6509f92 | 2001-12-05 04:21:30 +0000 | [diff] [blame] | 73 | while ((opt = getopt(argc, argv, options)) != -1) { |
| 74 | const char *p = strchr(options,opt); |
| 75 | if (p == NULL) { |
| 76 | show_usage(); |
| 77 | } |
| 78 | toprint |= flags[(int)(p-options)]; |
| 79 | } |
| 80 | |
| 81 | if (toprint == 0) { |
| 82 | toprint = flags[0]; /* sysname */ |
| 83 | } |
| 84 | |
| 85 | if (uname(&uname_info.name) == -1) { |
| 86 | error_msg_and_die("cannot get system name"); |
| 87 | } |
| 88 | |
| 89 | #if defined(__sparc__) && defined(__linux__) |
| 90 | if ((fake_sparc != NULL) |
| 91 | && ((fake_sparc[0] == 'y') |
| 92 | || (fake_sparc[0] == 'Y'))) { |
| 93 | strcpy(uname_info.name.machine, "sparc"); |
| 94 | } |
| 95 | #endif |
| 96 | |
| 97 | strcpy(uname_info.processor, "unknown"); |
| 98 | |
| 99 | for (delta=utsname_offset ; toprint ; delta++, toprint >>= 1) { |
| 100 | if (toprint & 1) { |
| 101 | printf("%s ", ((char *)(&uname_info)) + *delta ); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 102 | } |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 103 | } |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 104 | |
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 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 107 | return EXIT_SUCCESS; |
Eric Andersen | c8fdb56 | 1999-10-26 05:21:02 +0000 | [diff] [blame] | 108 | } |