1 /* basename -- strip directory and suffix from file names
2 Copyright (C) 1990-1997, 1999-2006 Free Software Foundation, Inc.
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)
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.
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. */
18 /* Usage: basename name [suffix]
19 NAME is a file name; SUFFIX is a suffix to strip from it.
21 basename /usr/foo/lossage/functions.l
23 basename /usr/foo/lossage/functions.l .l
25 basename functions.lisp p
31 #include <sys/types.h>
34 #include "long-options.h"
38 /* The official name of this program (e.g., no `g' prefix). */
39 #define PROGRAM_NAME "basename"
41 #define AUTHORS "FIXME unknown"
43 /* The name this program was run with. */
49 if (status != EXIT_SUCCESS)
50 fprintf (stderr, _("Try `%s --help' for more information.\n"),
55 Usage: %s NAME [SUFFIX]\n\
58 program_name, program_name);
60 Print NAME with any leading directory components removed.\n\
61 If specified, also remove a trailing SUFFIX.\n\
64 fputs (HELP_OPTION_DESCRIPTION, stdout);
65 fputs (VERSION_OPTION_DESCRIPTION, stdout);
69 %s /usr/bin/sort Output \"sort\".\n\
70 %s include/stdio.h .h Output \"stdio\".\n\
72 program_name, program_name);
73 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
78 /* Remove SUFFIX from the end of NAME if it is there, unless NAME
79 consists entirely of SUFFIX. */
82 remove_suffix (char *name, const char *suffix)
87 np = name + strlen (name);
88 sp = suffix + strlen (suffix);
90 while (np > name && sp > suffix)
98 main (int argc, char **argv)
102 initialize_main (&argc, &argv);
103 program_name = argv[0];
104 setlocale (LC_ALL, "");
105 bindtextdomain (PACKAGE, LOCALEDIR);
106 textdomain (PACKAGE);
108 atexit (close_stdout);
110 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
111 usage, AUTHORS, (char const *) NULL);
112 if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
113 usage (EXIT_FAILURE);
115 if (argc < optind + 1)
117 error (0, 0, _("missing operand"));
118 usage (EXIT_FAILURE);
121 if (optind + 2 < argc)
123 error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
124 usage (EXIT_FAILURE);
127 name = base_name (argv[optind]);
128 strip_trailing_slashes (name);
130 /* Per POSIX, `basename // /' must return `//' on platforms with
131 distinct //. On platforms with drive letters, this generalizes
132 to making `basename c: :' return `c:'. This rule is captured by
133 skipping suffix stripping if base_name returned an absolute path
134 or a drive letter (only possible if name is a file-system
136 if (argc == optind + 2 && IS_RELATIVE_FILE_NAME (name)
137 && ! FILE_SYSTEM_PREFIX_LEN (name))
138 remove_suffix (name, argv[optind + 1]);