diff options
Diffstat (limited to 'src/uname.c')
-rw-r--r-- | src/uname.c | 302 |
1 files changed, 176 insertions, 126 deletions
diff --git a/src/uname.c b/src/uname.c index 0715e07..962a6cf 100644 --- a/src/uname.c +++ b/src/uname.c @@ -1,12 +1,11 @@ /* uname -- print system information - Copyright (C) 1989, 1992, 1993, 1996, 1997, 1999, 2000, 2001, 2002, - 2003, 2004, 2005 Free Software Foundation, Inc. + Copyright (C) 1989-2016 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or modify + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -14,8 +13,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */ @@ -54,13 +52,15 @@ #include "system.h" #include "error.h" #include "quote.h" +#include "uname.h" -/* The official name of this program (e.g., no `g' prefix). */ -#define PROGRAM_NAME "uname" +/* The official name of this program (e.g., no 'g' prefix). */ +#define PROGRAM_NAME (uname_mode == UNAME_UNAME ? "uname" : "arch") -#define AUTHORS "David MacKenzie" +#define AUTHORS proper_name ("David MacKenzie") +#define ARCH_AUTHORS "David MacKenzie", "Karel Zak" -/* Values that are bitwise or'd into `toprint'. */ +/* Values that are bitwise or'd into 'toprint'. */ /* Kernel name. */ #define PRINT_KERNEL_NAME 1 @@ -85,10 +85,7 @@ /* Operating system. */ #define PRINT_OPERATING_SYSTEM 128 -/* The name this program was run with, for error messages. */ -char *program_name; - -static struct option const long_options[] = +static struct option const uname_long_options[] = { {"all", no_argument, NULL, 'a'}, {"kernel-name", no_argument, NULL, 's'}, @@ -106,16 +103,25 @@ static struct option const long_options[] = {NULL, 0, NULL, 0} }; +static struct option const arch_long_options[] = +{ + {GETOPT_HELP_OPTION_DECL}, + {GETOPT_VERSION_OPTION_DECL}, + {NULL, 0, NULL, 0} +}; + void usage (int status) { if (status != EXIT_SUCCESS) - fprintf (stderr, _("Try `%s --help' for more information.\n"), - program_name); + emit_try_help (); else { printf (_("Usage: %s [OPTION]...\n"), program_name); - fputs (_("\ + + if (uname_mode == UNAME_UNAME) + { + fputs (_("\ Print certain system information. With no OPTION, same as -s.\n\ \n\ -a, --all print all information, in the following order,\n\ @@ -124,16 +130,25 @@ Print certain system information. With no OPTION, same as -s.\n\ -n, --nodename print the network node hostname\n\ -r, --kernel-release print the kernel release\n\ "), stdout); - fputs (_("\ + fputs (_("\ -v, --kernel-version print the kernel version\n\ -m, --machine print the machine hardware name\n\ - -p, --processor print the processor type or \"unknown\"\n\ - -i, --hardware-platform print the hardware platform or \"unknown\"\n\ + -p, --processor print the processor type (non-portable)\n\ + -i, --hardware-platform print the hardware platform (non-portable)\n\ -o, --operating-system print the operating system\n\ "), stdout); + } + else + { + fputs (_("\ +Print machine architecture.\n\ +\n\ +"), stdout); + } + fputs (HELP_OPTION_DESCRIPTION, stdout); fputs (VERSION_OPTION_DESCRIPTION, stdout); - printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT); + emit_ancillary_info (PROGRAM_NAME); } exit (status); } @@ -151,100 +166,135 @@ print_element (char const *element) fputs (element, stdout); } + +/* Set all the option flags according to the switches specified. + Return the mask indicating which elements to print. */ + +static int +decode_switches (int argc, char **argv) +{ + int c; + unsigned int toprint = 0; + + if (uname_mode == UNAME_ARCH) + { + while ((c = getopt_long (argc, argv, "", + arch_long_options, NULL)) != -1) + { + switch (c) + { + case_GETOPT_HELP_CHAR; + + case_GETOPT_VERSION_CHAR (PROGRAM_NAME, ARCH_AUTHORS); + + default: + usage (EXIT_FAILURE); + } + } + toprint = PRINT_MACHINE; + } + else + { + while ((c = getopt_long (argc, argv, "asnrvmpio", + uname_long_options, NULL)) != -1) + { + switch (c) + { + case 'a': + toprint = UINT_MAX; + break; + + case 's': + toprint |= PRINT_KERNEL_NAME; + break; + + case 'n': + toprint |= PRINT_NODENAME; + break; + + case 'r': + toprint |= PRINT_KERNEL_RELEASE; + break; + + case 'v': + toprint |= PRINT_KERNEL_VERSION; + break; + + case 'm': + toprint |= PRINT_MACHINE; + break; + + case 'p': + toprint |= PRINT_PROCESSOR; + break; + + case 'i': + toprint |= PRINT_HARDWARE_PLATFORM; + break; + + case 'o': + toprint |= PRINT_OPERATING_SYSTEM; + break; + + case_GETOPT_HELP_CHAR; + + case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); + + default: + usage (EXIT_FAILURE); + } + } + } + + if (argc != optind) + { + error (0, 0, _("extra operand %s"), quote (argv[optind])); + usage (EXIT_FAILURE); + } + + return toprint; +} + int main (int argc, char **argv) { - int c; static char const unknown[] = "unknown"; /* Mask indicating which elements to print. */ unsigned int toprint = 0; initialize_main (&argc, &argv); - program_name = argv[0]; + set_program_name (argv[0]); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); atexit (close_stdout); - while ((c = getopt_long (argc, argv, "asnrvmpio", long_options, NULL)) != -1) - { - switch (c) - { - case 'a': - toprint = UINT_MAX; - break; - - case 's': - toprint |= PRINT_KERNEL_NAME; - break; - - case 'n': - toprint |= PRINT_NODENAME; - break; - - case 'r': - toprint |= PRINT_KERNEL_RELEASE; - break; - - case 'v': - toprint |= PRINT_KERNEL_VERSION; - break; - - case 'm': - toprint |= PRINT_MACHINE; - break; - - case 'p': - toprint |= PRINT_PROCESSOR; - break; - - case 'i': - toprint |= PRINT_HARDWARE_PLATFORM; - break; - - case 'o': - toprint |= PRINT_OPERATING_SYSTEM; - break; - - case_GETOPT_HELP_CHAR; - - case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); - - default: - usage (EXIT_FAILURE); - } - } - - if (argc != optind) - { - error (0, 0, _("extra operand %s"), quote (argv[optind])); - usage (EXIT_FAILURE); - } + toprint = decode_switches (argc, argv); if (toprint == 0) toprint = PRINT_KERNEL_NAME; if (toprint & (PRINT_KERNEL_NAME | PRINT_NODENAME | PRINT_KERNEL_RELEASE - | PRINT_KERNEL_VERSION | PRINT_MACHINE)) + | PRINT_KERNEL_VERSION | PRINT_MACHINE)) { struct utsname name; if (uname (&name) == -1) - error (EXIT_FAILURE, errno, _("cannot get system name")); + error (EXIT_FAILURE, errno, _("cannot get system name")); if (toprint & PRINT_KERNEL_NAME) - print_element (name.sysname); + print_element (name.sysname); if (toprint & PRINT_NODENAME) - print_element (name.nodename); + print_element (name.nodename); if (toprint & PRINT_KERNEL_RELEASE) - print_element (name.release); + print_element (name.release); if (toprint & PRINT_KERNEL_VERSION) - print_element (name.version); + print_element (name.version); if (toprint & PRINT_MACHINE) - print_element (name.machine); + print_element (name.machine); } if (toprint & PRINT_PROCESSOR) @@ -252,43 +302,43 @@ main (int argc, char **argv) char const *element = unknown; #if HAVE_SYSINFO && defined SI_ARCHITECTURE { - static char processor[257]; - if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor)) - element = processor; + static char processor[257]; + if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor)) + element = processor; } #endif #ifdef UNAME_PROCESSOR if (element == unknown) - { - static char processor[257]; - size_t s = sizeof processor; - static int mib[] = { CTL_HW, UNAME_PROCESSOR }; - if (sysctl (mib, 2, processor, &s, 0, 0) >= 0) - element = processor; + { + static char processor[257]; + size_t s = sizeof processor; + static int mib[] = { CTL_HW, UNAME_PROCESSOR }; + if (sysctl (mib, 2, processor, &s, 0, 0) >= 0) + element = processor; # ifdef __APPLE__ - /* This kludge works around a bug in Mac OS X. */ - if (element == unknown) - { - cpu_type_t cputype; - size_t s = sizeof cputype; - NXArchInfo const *ai; - if (sysctlbyname ("hw.cputype", &cputype, &s, NULL, 0) == 0 - && (ai = NXGetArchInfoFromCpuType (cputype, - CPU_SUBTYPE_MULTIPLE)) - != NULL) - element = ai->name; - - /* Hack "safely" around the ppc vs. powerpc return value. */ - if (cputype == CPU_TYPE_POWERPC - && strncmp (element, "ppc", 3) == 0) - element = "powerpc"; - } + /* This kludge works around a bug in Mac OS X. */ + if (element == unknown) + { + cpu_type_t cputype; + size_t cs = sizeof cputype; + NXArchInfo const *ai; + if (sysctlbyname ("hw.cputype", &cputype, &cs, NULL, 0) == 0 + && (ai = NXGetArchInfoFromCpuType (cputype, + CPU_SUBTYPE_MULTIPLE)) + != NULL) + element = ai->name; + + /* Hack "safely" around the ppc vs. powerpc return value. */ + if (cputype == CPU_TYPE_POWERPC + && STRNCMP_LIT (element, "ppc") == 0) + element = "powerpc"; + } # endif - } + } #endif if (! (toprint == UINT_MAX && element == unknown)) - print_element (element); + print_element (element); } if (toprint & PRINT_HARDWARE_PLATFORM) @@ -296,24 +346,24 @@ main (int argc, char **argv) char const *element = unknown; #if HAVE_SYSINFO && defined SI_PLATFORM { - static char hardware_platform[257]; - if (0 <= sysinfo (SI_PLATFORM, - hardware_platform, sizeof hardware_platform)) - element = hardware_platform; + static char hardware_platform[257]; + if (0 <= sysinfo (SI_PLATFORM, + hardware_platform, sizeof hardware_platform)) + element = hardware_platform; } #endif #ifdef UNAME_HARDWARE_PLATFORM if (element == unknown) - { - static char hardware_platform[257]; - size_t s = sizeof hardware_platform; - static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM }; - if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0) - element = hardware_platform; - } + { + static char hardware_platform[257]; + size_t s = sizeof hardware_platform; + static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM }; + if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0) + element = hardware_platform; + } #endif if (! (toprint == UINT_MAX && element == unknown)) - print_element (element); + print_element (element); } if (toprint & PRINT_OPERATING_SYSTEM) @@ -321,5 +371,5 @@ main (int argc, char **argv) putchar ('\n'); - exit (EXIT_SUCCESS); + return EXIT_SUCCESS; } |