merge with 1.8.1b
[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 #include <stdio.h>
29 #include <sys/types.h>
30 #include <getopt.h>
31
32 #include "version.h"
33 #include "system.h"
34
35 char *basename ();
36 void strip_trailing_slashes ();
37
38 static void remove_suffix ();
39
40 /* The name this program was run with. */
41 char *program_name;
42
43 /* If non-zero, display usage information and exit.  */
44 static int show_help;
45
46 /* If non-zero, print the version on standard output and exit.  */
47 static int show_version;
48
49 static struct option const long_options[] =
50 {
51   {"help", no_argument, &show_help, 1},
52   {"version", no_argument, &show_version, 1},
53   {0, 0, 0, 0}
54 };
55
56 static void
57 usage ()
58 {
59   fprintf (stderr, "Usage: %s [{--help,--version}] name [suffix]\n",
60            program_name);
61   exit (1);
62 }
63
64 void
65 main (argc, argv)
66      int argc;
67      char **argv;
68 {
69   char *name;
70   int c;
71
72   program_name = argv[0];
73
74   while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF)
75     {
76       switch (c)
77         {
78         case 0:
79           break;
80
81         default:
82           usage ();
83         }
84     }
85
86   if (show_version)
87     {
88       printf ("%s\n", version_string);
89       exit (0);
90     }
91
92   if (show_help)
93     usage ();
94
95   if (argc - optind == 0 || argc - optind > 2)
96     usage ();
97
98   strip_trailing_slashes (argv[optind]);
99
100   name = basename (argv[optind]);
101
102   if (argc == 3)
103     remove_suffix (name, argv[optind + 1]);
104
105   puts (name);
106
107   exit (0);
108 }
109
110 /* Remove SUFFIX from the end of NAME if it is there, unless NAME
111    consists entirely of SUFFIX. */
112
113 static void
114 remove_suffix (name, suffix)
115      register char *name, *suffix;
116 {
117   register char *np, *sp;
118
119   np = name + strlen (name);
120   sp = suffix + strlen (suffix);
121
122   while (np > name && sp > suffix)
123     if (*--np != *--sp)
124       return;
125   if (np > name)
126     *np = '\0';
127 }