diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2016-01-20 10:55:18 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2016-01-20 10:55:18 +0000 |
commit | 70e9163c9c18e995515598085cb824e554eb7ae7 (patch) | |
tree | a42dc8b2a6c031354bf31472de888bfc8a060132 /src/dirname.c | |
parent | cbf5993c43f49281173f185863577d86bfac6eae (diff) | |
download | coreutils-tarball-70e9163c9c18e995515598085cb824e554eb7ae7.tar.gz |
coreutils-8.25HEADcoreutils-8.25master
Diffstat (limited to 'src/dirname.c')
-rw-r--r-- | src/dirname.c | 105 |
1 files changed, 62 insertions, 43 deletions
diff --git a/src/dirname.c b/src/dirname.c index 2253391..d3d130d 100644 --- a/src/dirname.c +++ b/src/dirname.c @@ -1,12 +1,11 @@ /* dirname -- strip suffix from file name - Copyright (C) 1990-1997, 1999-2002, 2004, 2005, 2006 Free Software - Foundation, Inc. + Copyright (C) 1990-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 and Jim Meyering. */ @@ -25,46 +23,53 @@ #include <sys/types.h> #include "system.h" -#include "long-options.h" #include "error.h" -#include "quote.h" -/* The official name of this program (e.g., no `g' prefix). */ +/* The official name of this program (e.g., no 'g' prefix). */ #define PROGRAM_NAME "dirname" -#define AUTHORS "David MacKenzie", "Jim Meyering" +#define AUTHORS \ + proper_name ("David MacKenzie"), \ + proper_name ("Jim Meyering") -/* The name this program was run with. */ -char *program_name; +static struct option const longopts[] = +{ + {"zero", no_argument, NULL, 'z'}, + {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 NAME\n\ - or: %s OPTION\n\ +Usage: %s [OPTION] NAME...\n\ "), - program_name, program_name); + program_name); fputs (_("\ -Print NAME with its trailing /component removed; if NAME contains no /'s,\n\ -output `.' (meaning the current directory).\n\ +Output each NAME with its last non-slash component and trailing slashes\n\ +removed; if NAME contains no /'s, output '.' (meaning the current directory).\n\ \n\ "), stdout); + fputs (_("\ + -z, --zero end each output line with NUL, not newline\n\ +"), stdout); fputs (HELP_OPTION_DESCRIPTION, stdout); fputs (VERSION_OPTION_DESCRIPTION, stdout); printf (_("\ \n\ Examples:\n\ - %s /usr/bin/sort Output \"/usr/bin\".\n\ - %s stdio.h Output \".\".\n\ + %s /usr/bin/ -> \"/usr\"\n\ + %s dir1/str dir2/str -> \"dir1\" followed by \"dir2\"\n\ + %s stdio.h -> \".\"\n\ "), - program_name, program_name); - printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT); + program_name, program_name, program_name); + emit_ancillary_info (PROGRAM_NAME); } exit (status); } @@ -73,21 +78,38 @@ int main (int argc, char **argv) { static char const dot = '.'; + bool use_nuls = false; char const *result; size_t len; initialize_main (&argc, &argv); - program_name = argv[0]; + set_program_name (argv[0]); setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION, - usage, AUTHORS, (char const *) NULL); - if (getopt_long (argc, argv, "+", NULL, NULL) != -1) - usage (EXIT_FAILURE); + while (true) + { + int c = getopt_long (argc, argv, "z", longopts, NULL); + + if (c == -1) + break; + + switch (c) + { + case 'z': + use_nuls = true; + break; + + case_GETOPT_HELP_CHAR; + case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); + + default: + usage (EXIT_FAILURE); + } + } if (argc < optind + 1) { @@ -95,23 +117,20 @@ main (int argc, char **argv) usage (EXIT_FAILURE); } - if (optind + 1 < argc) + for (; optind < argc; optind++) { - error (0, 0, _("extra operand %s"), quote (argv[optind + 1])); - usage (EXIT_FAILURE); - } + result = argv[optind]; + len = dir_len (result); - result = argv[optind]; - len = dir_len (result); + if (! len) + { + result = ˙ + len = 1; + } - if (! len) - { - result = ˙ - len = 1; + fwrite (result, 1, len, stdout); + putchar (use_nuls ? '\0' :'\n'); } - fwrite (result, 1, len, stdout); - putchar ('\n'); - - exit (EXIT_SUCCESS); + return EXIT_SUCCESS; } |