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"
39 /* The official name of this program (e.g., no `g' prefix). */
40 #define PROGRAM_NAME "basename"
42 #define AUTHORS "FIXME unknown"
44 /* The name this program was run with. */
50 if (status != EXIT_SUCCESS)
51 fprintf (stderr, _("Try `%s --help' for more information.\n"),
56 Usage: %s NAME [SUFFIX]\n\
59 program_name, program_name);
61 Print NAME with any leading directory components removed.\n\
62 If specified, also remove a trailing SUFFIX.\n\
65 fputs (HELP_OPTION_DESCRIPTION, stdout);
66 fputs (VERSION_OPTION_DESCRIPTION, stdout);
70 %s /usr/bin/sort Output \"sort\".\n\
71 %s include/stdio.h .h Output \"stdio\".\n\
73 program_name, program_name);
74 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
79 /* Remove SUFFIX from the end of NAME if it is there, unless NAME
80 consists entirely of SUFFIX. */
83 remove_suffix (char *name, const char *suffix)
88 np = name + strlen (name);
89 sp = suffix + strlen (suffix);
91 while (np > name && sp > suffix)
99 main (int argc, char **argv)
103 initialize_main (&argc, &argv);
104 program_name = argv[0];
105 setlocale (LC_ALL, "");
106 bindtextdomain (PACKAGE, LOCALEDIR);
107 textdomain (PACKAGE);
109 atexit (close_stdout);
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);
116 if (argc < optind + 1)
118 error (0, 0, _("missing operand"));
119 usage (EXIT_FAILURE);
122 if (optind + 2 < argc)
124 error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
125 usage (EXIT_FAILURE);
128 name = base_name (argv[optind]);
129 strip_trailing_slashes (name);
131 /* Per POSIX, `basename // /' must return `//' on platforms with
132 distinct //. On platforms with drive letters, this generalizes
133 to making `basename c: :' return `c:'. This rule is captured by
134 skipping suffix stripping if base_name returned an absolute path
135 or a drive letter (only possible if name is a file-system
137 if (argc == optind + 2 && IS_RELATIVE_FILE_NAME (name)
138 && ! FILE_SYSTEM_PREFIX_LEN (name))
139 remove_suffix (name, argv[optind + 1]);