| 1 | /* basename -- strip directory and suffix from file names
|
|---|
| 2 | Copyright (C) 1990-1997, 1999-2005 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This program is free software; you can redistribute it and/or modify
|
|---|
| 5 | it under the terms of the GNU General Public License as published by
|
|---|
| 6 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 7 | any later version.
|
|---|
| 8 |
|
|---|
| 9 | This program is distributed in the hope that it will be useful,
|
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | GNU General Public License for more details.
|
|---|
| 13 |
|
|---|
| 14 | You should have received a copy of the GNU General Public License
|
|---|
| 15 | along with this program; if not, write to the Free Software Foundation,
|
|---|
| 16 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
|---|
| 17 |
|
|---|
| 18 | /* Usage: basename name [suffix]
|
|---|
| 19 | NAME is a file name; SUFFIX is a suffix to strip from it.
|
|---|
| 20 |
|
|---|
| 21 | basename /usr/foo/lossage/functions.l
|
|---|
| 22 | => functions.l
|
|---|
| 23 | basename /usr/foo/lossage/functions.l .l
|
|---|
| 24 | => functions
|
|---|
| 25 | basename functions.lisp p
|
|---|
| 26 | => functions.lis */
|
|---|
| 27 |
|
|---|
| 28 | #include <config.h>
|
|---|
| 29 | #include <getopt.h>
|
|---|
| 30 | #include <stdio.h>
|
|---|
| 31 | #include <sys/types.h>
|
|---|
| 32 |
|
|---|
| 33 | #include "system.h"
|
|---|
| 34 | #include "long-options.h"
|
|---|
| 35 | #include "dirname.h"
|
|---|
| 36 | #include "error.h"
|
|---|
| 37 | #include "quote.h"
|
|---|
| 38 |
|
|---|
| 39 | /* The official name of this program (e.g., no `g' prefix). */
|
|---|
| 40 | #define PROGRAM_NAME "basename"
|
|---|
| 41 |
|
|---|
| 42 | #define AUTHORS "FIXME unknown"
|
|---|
| 43 |
|
|---|
| 44 | /* The name this program was run with. */
|
|---|
| 45 | char *program_name;
|
|---|
| 46 |
|
|---|
| 47 | void
|
|---|
| 48 | usage (int status)
|
|---|
| 49 | {
|
|---|
| 50 | if (status != EXIT_SUCCESS)
|
|---|
| 51 | fprintf (stderr, _("Try `%s --help' for more information.\n"),
|
|---|
| 52 | program_name);
|
|---|
| 53 | else
|
|---|
| 54 | {
|
|---|
| 55 | printf (_("\
|
|---|
| 56 | Usage: %s NAME [SUFFIX]\n\
|
|---|
| 57 | or: %s OPTION\n\
|
|---|
| 58 | "),
|
|---|
| 59 | program_name, program_name);
|
|---|
| 60 | fputs (_("\
|
|---|
| 61 | Print NAME with any leading directory components removed.\n\
|
|---|
| 62 | If specified, also remove a trailing SUFFIX.\n\
|
|---|
| 63 | \n\
|
|---|
| 64 | "), stdout);
|
|---|
| 65 | fputs (HELP_OPTION_DESCRIPTION, stdout);
|
|---|
| 66 | fputs (VERSION_OPTION_DESCRIPTION, stdout);
|
|---|
| 67 | printf (_("\
|
|---|
| 68 | \n\
|
|---|
| 69 | Examples:\n\
|
|---|
| 70 | %s /usr/bin/sort Output \"sort\".\n\
|
|---|
| 71 | %s include/stdio.h .h Output \"stdio\".\n\
|
|---|
| 72 | "),
|
|---|
| 73 | program_name, program_name);
|
|---|
| 74 | printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
|
|---|
| 75 | }
|
|---|
| 76 | exit (status);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /* Remove SUFFIX from the end of NAME if it is there, unless NAME
|
|---|
| 80 | consists entirely of SUFFIX. */
|
|---|
| 81 |
|
|---|
| 82 | static void
|
|---|
| 83 | remove_suffix (char *name, const char *suffix)
|
|---|
| 84 | {
|
|---|
| 85 | char *np;
|
|---|
| 86 | const char *sp;
|
|---|
| 87 |
|
|---|
| 88 | np = name + strlen (name);
|
|---|
| 89 | sp = suffix + strlen (suffix);
|
|---|
| 90 |
|
|---|
| 91 | while (np > name && sp > suffix)
|
|---|
| 92 | if (*--np != *--sp)
|
|---|
| 93 | return;
|
|---|
| 94 | if (np > name)
|
|---|
| 95 | *np = '\0';
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | int
|
|---|
| 99 | main (int argc, char **argv)
|
|---|
| 100 | {
|
|---|
| 101 | char *name;
|
|---|
| 102 |
|
|---|
| 103 | initialize_main (&argc, &argv);
|
|---|
| 104 | program_name = argv[0];
|
|---|
| 105 | setlocale (LC_ALL, "");
|
|---|
| 106 | bindtextdomain (PACKAGE, LOCALEDIR);
|
|---|
| 107 | textdomain (PACKAGE);
|
|---|
| 108 |
|
|---|
| 109 | atexit (close_stdout);
|
|---|
| 110 |
|
|---|
| 111 | parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
|
|---|
| 112 | usage, AUTHORS, (char const *) NULL);
|
|---|
| 113 | if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
|
|---|
| 114 | usage (EXIT_FAILURE);
|
|---|
| 115 |
|
|---|
| 116 | if (argc < optind + 1)
|
|---|
| 117 | {
|
|---|
| 118 | error (0, 0, _("missing operand"));
|
|---|
| 119 | usage (EXIT_FAILURE);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | if (optind + 2 < argc)
|
|---|
| 123 | {
|
|---|
| 124 | error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
|
|---|
| 125 | usage (EXIT_FAILURE);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | name = base_name (argv[optind]);
|
|---|
| 129 | name[base_len (name)] = '\0';
|
|---|
| 130 |
|
|---|
| 131 | if (argc == optind + 2)
|
|---|
| 132 | remove_suffix (name, argv[optind + 1]);
|
|---|
| 133 |
|
|---|
| 134 | puts (name);
|
|---|
| 135 |
|
|---|
| 136 | exit (EXIT_SUCCESS);
|
|---|
| 137 | }
|
|---|