.
[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 "long-options.h"
44
45 char *basename ();
46 void strip_trailing_slashes ();
47
48 static void remove_suffix ();
49
50 /* The name this program was run with. */
51 char *program_name;
52
53 static void
54 usage (status)
55      int status;
56 {
57   if (status != 0)
58     fprintf (stderr, "Try `%s --help' for more information.\n",
59              program_name);
60   else
61     {
62       printf ("\
63 Usage: %s PATH [SUFFIX]\n\
64   or:  %s OPTION\n\
65 ",
66               program_name, program_name);
67       printf ("\
68 \n\
69   --help      display this help and exit\n\
70   --version   output version information and exit\n\
71 ");
72     }
73   exit (status);
74 }
75
76 void
77 main (argc, argv)
78      int argc;
79      char **argv;
80 {
81   char *name;
82
83   program_name = argv[0];
84
85   parse_long_options (argc, argv, usage);
86
87   if (argc == 1 || argc > 3)
88     usage (1);
89
90   strip_trailing_slashes (argv[1]);
91
92   name = basename (argv[1]);
93
94   if (argc == 3)
95     remove_suffix (name, argv[2]);
96
97   puts (name);
98
99   exit (0);
100 }
101
102 /* Remove SUFFIX from the end of NAME if it is there, unless NAME
103    consists entirely of SUFFIX. */
104
105 static void
106 remove_suffix (name, suffix)
107      register char *name, *suffix;
108 {
109   register char *np, *sp;
110
111   np = name + strlen (name);
112   sp = suffix + strlen (suffix);
113
114   while (np > name && sp > suffix)
115     if (*--np != *--sp)
116       return;
117   if (np > name)
118     *np = '\0';
119 }