merge with 1.8.1h
[platform/upstream/coreutils.git] / src / basename.c
1 /* basename -- strip directory and suffix from filenames
2    Copyright (C) 1990, 1991 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 #include <getopt.h>
42
43 #include "version.h"
44 #include "system.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 /* If non-zero, display usage information and exit.  */
55 static int show_help;
56
57 /* If non-zero, print the version on standard output and exit.  */
58 static int show_version;
59
60 static struct option const long_options[] =
61 {
62   {"help", no_argument, &show_help, 1},
63   {"version", no_argument, &show_version, 1},
64   {0, 0, 0, 0}
65 };
66
67 static void
68 usage (status)
69      int status;
70 {
71   fprintf (status == 0 ? stdout : stderr, "\
72 Usage: %s [OPTION]... PATH [SUFFIX]\n\
73 ",
74            program_name);
75
76   if (status != 0)
77     fprintf (stderr, "Try `%s --help' for more information.\n",
78              program_name);
79   else
80
81     printf ("\
82 \n\
83   --help      display this help and exit\n\
84   --version   output version information and exit\n\
85 ");
86
87   exit (status);
88 }
89
90 void
91 main (argc, argv)
92      int argc;
93      char **argv;
94 {
95   char *name;
96   int c;
97
98   program_name = argv[0];
99
100   while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF)
101     {
102       switch (c)
103         {
104         case 0:
105           break;
106
107         default:
108           usage (1);
109         }
110     }
111
112   if (show_version)
113     {
114       printf ("%s\n", version_string);
115       exit (0);
116     }
117
118   if (show_help)
119     usage (0);
120
121   if (argc - optind == 0 || argc - optind > 2)
122     usage (1);
123
124   strip_trailing_slashes (argv[optind]);
125
126   name = basename (argv[optind]);
127
128   if (argc == 3)
129     remove_suffix (name, argv[optind + 1]);
130
131   puts (name);
132
133   exit (0);
134 }
135
136 /* Remove SUFFIX from the end of NAME if it is there, unless NAME
137    consists entirely of SUFFIX. */
138
139 static void
140 remove_suffix (name, suffix)
141      register char *name, *suffix;
142 {
143   register char *np, *sp;
144
145   np = name + strlen (name);
146   sp = suffix + strlen (suffix);
147
148   while (np > name && sp > suffix)
149     if (*--np != *--sp)
150       return;
151   if (np > name)
152     *np = '\0';
153 }