1 /* basename -- strip directory and suffix from filenames
2 Copyright (C) 1990-1997, 1999 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Usage: basename name [suffix]
19 NAME is a pathname; 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
30 #include <sys/types.h>
33 #include "long-options.h"
37 void strip_trailing_slashes ();
39 /* The name this program was run with. */
46 fprintf (stderr, _("Try `%s --help' for more information.\n"),
51 Usage: %s NAME [SUFFIX]\n\
54 program_name, program_name);
56 Print NAME with any leading directory components removed.\n\
57 If specified, also remove a trailing SUFFIX.\n\
59 --help display this help and exit\n\
60 --version output version information and exit\n\
62 puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
67 /* Remove SUFFIX from the end of NAME if it is there, unless NAME
68 consists entirely of SUFFIX. */
71 remove_suffix (char *name, const char *suffix)
76 np = name + strlen (name);
77 sp = suffix + strlen (suffix);
79 while (np > name && sp > suffix)
87 main (int argc, char **argv)
91 program_name = argv[0];
92 setlocale (LC_ALL, "");
93 bindtextdomain (PACKAGE, LOCALEDIR);
96 parse_long_options (argc, argv, "basename", GNU_PACKAGE, VERSION,
97 "FIXME unknown", usage);
99 if (argc == 1 || argc > 3)
101 error (0, 0, (argc == 1 ? _("too few arguments")
102 : _("too many arguments")));
106 strip_trailing_slashes (argv[1]);
108 name = base_name (argv[1]);
111 remove_suffix (name, argv[2]);