1 /* basename -- strip directory and suffix from filenames
2 Copyright (C) 90, 91, 92, 93, 94, 1995 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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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>
34 #include "long-options.h"
37 char *basename __P ((char *));
38 void strip_trailing_slashes ();
40 static void remove_suffix __P ((register char *name, register char *suffix));
42 /* The name this program was run with. */
49 fprintf (stderr, "Try `%s --help' for more information.\n",
54 Usage: %s NAME [SUFFIX]\n\
57 program_name, program_name);
59 Print NAME with any leading directory components removed.\n\
60 If specified, also remove a trailing SUFFIX.\n\
62 --help display this help and exit\n\
63 --version output version information and exit\n\
70 main (int argc, char **argv)
74 program_name = argv[0];
76 parse_long_options (argc, argv, "basename", version_string, usage);
78 if (argc == 1 || argc > 3)
80 error (0, 0, "too %s arguments", argc == 1 ? "few" : "many");
84 strip_trailing_slashes (argv[1]);
86 name = basename (argv[1]);
89 remove_suffix (name, argv[2]);
96 /* Remove SUFFIX from the end of NAME if it is there, unless NAME
97 consists entirely of SUFFIX. */
100 remove_suffix (register char *name, register char *suffix)
102 register char *np, *sp;
104 np = name + strlen (name);
105 sp = suffix + strlen (suffix);
107 while (np > name && sp > suffix)