.
[platform/upstream/coreutils.git] / src / basename.c
1 /* basename -- strip directory and suffix from filenames
2    Copyright (C) 90, 91, 92, 93, 1994 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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.  */
17
18 /* Usage: basename name [suffix]
19    NAME is a pathname; SUFFIX is a suffix to strip from it.
20
21    basename /usr/foo/lossage/functions.l
22    => functions.l
23    basename /usr/foo/lossage/functions.l .l
24    => functions
25    basename functions.lisp p
26    => functions.lis */
27
28 #ifdef HAVE_CONFIG_H
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).  */
33 #include <config.h>
34 #else
35 #include "config.h"
36 #endif
37 #endif
38
39 #include <stdio.h>
40 #include <sys/types.h>
41
42 #include "system.h"
43 #include "version.h"
44 #include "long-options.h"
45
46 char *basename ();
47 void strip_trailing_slashes ();
48
49 static void remove_suffix ();
50
51 /* The name this program was run with. */
52 char *program_name;
53
54 static void
55 usage (status)
56      int status;
57 {
58   if (status != 0)
59     fprintf (stderr, "Try `%s --help' for more information.\n",
60              program_name);
61   else
62     {
63       printf ("\
64 Usage: %s PATH [SUFFIX]\n\
65   or:  %s OPTION\n\
66 ",
67               program_name, program_name);
68       printf ("\
69 \n\
70   --help      display this help and exit\n\
71   --version   output version information and exit\n\
72 ");
73     }
74   exit (status);
75 }
76
77 void
78 main (argc, argv)
79      int argc;
80      char **argv;
81 {
82   char *name;
83
84   program_name = argv[0];
85
86   parse_long_options (argc, argv, "basename", version_string, usage);
87
88   if (argc == 1 || argc > 3)
89     usage (1);
90
91   strip_trailing_slashes (argv[1]);
92
93   name = basename (argv[1]);
94
95   if (argc == 3)
96     remove_suffix (name, argv[2]);
97
98   puts (name);
99
100   exit (0);
101 }
102
103 /* Remove SUFFIX from the end of NAME if it is there, unless NAME
104    consists entirely of SUFFIX. */
105
106 static void
107 remove_suffix (name, suffix)
108      register char *name, *suffix;
109 {
110   register char *np, *sp;
111
112   np = name + strlen (name);
113   sp = suffix + strlen (suffix);
114
115   while (np > name && sp > suffix)
116     if (*--np != *--sp)
117       return;
118   if (np > name)
119     *np = '\0';
120 }