1 /* basename -- strip directory and suffix from filenames
2 Copyright (C) 1990, 1991 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
29 #if defined (CONFIG_BROKETS)
30 /* We use <config.h> instead of "config.h" so that a compilation
31 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
32 (which it would do because it found this file in $srcdir). */
40 #include <sys/types.h>
47 void strip_trailing_slashes ();
49 static void remove_suffix ();
51 /* The name this program was run with. */
54 /* If non-zero, display usage information and exit. */
57 /* If non-zero, print the version on standard output and exit. */
58 static int show_version;
60 static struct option const long_options[] =
62 {"help", no_argument, &show_help, 1},
63 {"version", no_argument, &show_version, 1},
71 fprintf (status == 0 ? stdout : stderr, "\
72 Usage: %s [OPTION]... PATH [SUFFIX]\n\
77 fprintf (stderr, "Try `%s --help' for more information.\n",
83 --help display this help and exit\n\
84 --version output version information and exit\n\
98 program_name = argv[0];
100 while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF)
114 printf ("%s\n", version_string);
121 if (argc - optind == 0 || argc - optind > 2)
124 strip_trailing_slashes (argv[optind]);
126 name = basename (argv[optind]);
129 remove_suffix (name, argv[optind + 1]);
136 /* Remove SUFFIX from the end of NAME if it is there, unless NAME
137 consists entirely of SUFFIX. */
140 remove_suffix (name, suffix)
141 register char *name, *suffix;
143 register char *np, *sp;
145 np = name + strlen (name);
146 sp = suffix + strlen (suffix);
148 while (np > name && sp > suffix)