From 264021699cb9a460ebcdd3d080e88d55c269cc61 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 5 Jun 2007 23:29:32 +0200 Subject: New program: arch * NEWS: Mention arch. * README: Add arch to the list of programs. * AUTHORS: Add arch. * src/uname.c: Include "uname.h". (PROGRAM_NAME): Handle arch, too. (ARCH_AUTHORS): Define. (uname_long_options, arch_long_options): Renamed and new globals. (usage): Handle arch-mode as well as uname-mode. (decode_switches): New function, extracted from main, to handle arch-mode as well as uname-mode. (main): Handle both modes. * src/uname-arch.c: New program, alias for "uname -m". * src/uname-uname.c: New file, default uname mode. * src/uname.h: New file, uname modes. * src/Makefile.am (EXTRA_PROGRAMS): Add arch. (uname_SOURCES, arch_SOURCES): Define. * man/arch.x: New file. * man/Makefile.am (dist_man_MANS): Add arch.1. (arch.1): New dependency. * tests/misc/arch: New test, compare "arch" with "uname -m" * configure.ac (OPTIONAL_BIN_PROGS): Add arch. (MAN): Add arch.1. * .x-sc_require_config_h: Exempt uname-arch.c and uname-uname.c from the always-include- rule. Signed-off-by: Karel Zak Signed-off-by: Jim Meyering --- src/Makefile.am | 9 +++- src/uname-arch.c | 2 + src/uname-uname.c | 2 + src/uname.c | 158 ++++++++++++++++++++++++++++++++++++------------------ src/uname.h | 7 +++ 5 files changed, 125 insertions(+), 53 deletions(-) create mode 100644 src/uname-arch.c create mode 100644 src/uname-uname.c create mode 100644 src/uname.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 2dc990445..5a71bb3f6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -16,7 +16,8 @@ ## along with this program; if not, write to the Free Software Foundation, ## Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -EXTRA_PROGRAMS = chroot df hostid nice pinky stty su uname uptime users who +EXTRA_PROGRAMS = chroot df hostid nice pinky stty su uname uptime users who \ + arch bin_SCRIPTS = groups bin_PROGRAMS = [ chcon chgrp chown chmod cp dd dircolors du \ @@ -44,7 +45,8 @@ noinst_HEADERS = \ remove.h \ system.h \ wheel-size.h \ - wheel.h + wheel.h \ + uname.h EXTRA_DIST = dcgen dircolors.hin tac-pipe.c \ groups.sh wheel-gen.pl extract-magic c99-to-c89.diff @@ -203,6 +205,9 @@ chgrp_SOURCES = chgrp.c chown-core.c mv_SOURCES = mv.c copy.c cp-hash.c remove.c rm_SOURCES = rm.c remove.c +uname_SOURCES = uname.c uname-uname.c +arch_SOURCES = uname.c uname-arch.c + md5sum_SOURCES = md5sum.c md5sum_CPPFLAGS = -DHASH_ALGO_MD5=1 $(AM_CPPFLAGS) sha1sum_SOURCES = md5sum.c diff --git a/src/uname-arch.c b/src/uname-arch.c new file mode 100644 index 000000000..eb429424e --- /dev/null +++ b/src/uname-arch.c @@ -0,0 +1,2 @@ +#include "uname.h" +int uname_mode = UNAME_ARCH; diff --git a/src/uname-uname.c b/src/uname-uname.c new file mode 100644 index 000000000..450245dcc --- /dev/null +++ b/src/uname-uname.c @@ -0,0 +1,2 @@ +#include "uname.h" +int uname_mode = UNAME_UNAME; diff --git a/src/uname.c b/src/uname.c index c7ae92676..ebd68da05 100644 --- a/src/uname.c +++ b/src/uname.c @@ -1,7 +1,7 @@ /* 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, 1992, 1993, 1996, 1997, 1999-2005, 2007 + Free Software Foundation, Inc. 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 @@ -54,11 +54,13 @@ #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" +#define PROGRAM_NAME (uname_mode == UNAME_UNAME ? "uname" : "arch") #define AUTHORS "David MacKenzie" +#define ARCH_AUTHORS "David MacKenzie", "Karel Zak" /* Values that are bitwise or'd into `toprint'. */ /* Kernel name. */ @@ -88,7 +90,7 @@ /* 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,6 +108,13 @@ 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) { @@ -115,7 +124,10 @@ usage (int status) 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,13 +136,22 @@ 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\ -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); emit_bug_reporting_address (); @@ -151,69 +172,83 @@ print_element (char const *element) fputs (element, stdout); } -int -main (int argc, char **argv) + +/* 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; - static char const unknown[] = "unknown"; - - /* Mask indicating which elements to print. */ unsigned int toprint = 0; - initialize_main (&argc, &argv); - program_name = argv[0]; - setlocale (LC_ALL, ""); - bindtextdomain (PACKAGE, LOCALEDIR); - textdomain (PACKAGE); + if (uname_mode == UNAME_ARCH) + { + while ((c = getopt_long (argc, argv, "", + arch_long_options, NULL)) != -1) + { + switch (c) + { + case_GETOPT_HELP_CHAR; - atexit (close_stdout); + case_GETOPT_VERSION_CHAR (PROGRAM_NAME, ARCH_AUTHORS); - while ((c = getopt_long (argc, argv, "asnrvmpio", long_options, NULL)) != -1) + default: + usage (EXIT_FAILURE); + } + } + toprint = PRINT_MACHINE; + } + else { - switch (c) - { - case 'a': - toprint = UINT_MAX; - break; + 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 's': + toprint |= PRINT_KERNEL_NAME; + break; - case 'n': - toprint |= PRINT_NODENAME; - break; + case 'n': + toprint |= PRINT_NODENAME; + break; - case 'r': - toprint |= PRINT_KERNEL_RELEASE; - break; + case 'r': + toprint |= PRINT_KERNEL_RELEASE; + break; - case 'v': - toprint |= PRINT_KERNEL_VERSION; - break; + case 'v': + toprint |= PRINT_KERNEL_VERSION; + break; - case 'm': - toprint |= PRINT_MACHINE; - break; + case 'm': + toprint |= PRINT_MACHINE; + break; - case 'p': - toprint |= PRINT_PROCESSOR; - break; + case 'p': + toprint |= PRINT_PROCESSOR; + break; - case 'i': - toprint |= PRINT_HARDWARE_PLATFORM; - break; + case 'i': + toprint |= PRINT_HARDWARE_PLATFORM; + break; - case 'o': - toprint |= PRINT_OPERATING_SYSTEM; - break; + case 'o': + toprint |= PRINT_OPERATING_SYSTEM; + break; - case_GETOPT_HELP_CHAR; + case_GETOPT_HELP_CHAR; - case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); + case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); - default: - usage (EXIT_FAILURE); + default: + usage (EXIT_FAILURE); + } } } @@ -223,6 +258,27 @@ main (int argc, char **argv) usage (EXIT_FAILURE); } + return toprint; +} + +int +main (int argc, char **argv) +{ + static char const unknown[] = "unknown"; + + /* Mask indicating which elements to print. */ + unsigned int toprint = 0; + + initialize_main (&argc, &argv); + program_name = argv[0]; + setlocale (LC_ALL, ""); + bindtextdomain (PACKAGE, LOCALEDIR); + textdomain (PACKAGE); + + atexit (close_stdout); + + toprint = decode_switches (argc, argv); + if (toprint == 0) toprint = PRINT_KERNEL_NAME; diff --git a/src/uname.h b/src/uname.h new file mode 100644 index 000000000..2f4a87242 --- /dev/null +++ b/src/uname.h @@ -0,0 +1,7 @@ +/* This is for the `uname' program. */ +#define UNAME_UNAME 1 + +/* This is for the `arch' program. */ +#define UNAME_ARCH 2 + +extern int uname_mode; -- cgit v1.2.3-54-g00ecf