.
[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 #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   if (status != 0)
72     fprintf (stderr, "Try `%s --help' for more information.\n",
73              program_name);
74   else
75     {
76       printf ("Usage: %s [OPTION]... PATH [SUFFIX]\n", program_name);
77       printf ("\
78 \n\
79   --help      display this help and exit\n\
80   --version   output version information and exit\n\
81 ");
82     }
83   exit (status);
84 }
85
86 void
87 main (argc, argv)
88      int argc;
89      char **argv;
90 {
91   char *name;
92   int c;
93
94   program_name = argv[0];
95
96   while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF)
97     {
98       switch (c)
99         {
100         case 0:
101           break;
102
103         default:
104           usage (1);
105         }
106     }
107
108   if (show_version)
109     {
110       printf ("%s\n", version_string);
111       exit (0);
112     }
113
114   if (show_help)
115     usage (0);
116
117   if (argc - optind == 0 || argc - optind > 2)
118     usage (1);
119
120   strip_trailing_slashes (argv[optind]);
121
122   name = basename (argv[optind]);
123
124   if (argc == 3)
125     remove_suffix (name, argv[optind + 1]);
126
127   puts (name);
128
129   exit (0);
130 }
131
132 /* Remove SUFFIX from the end of NAME if it is there, unless NAME
133    consists entirely of SUFFIX. */
134
135 static void
136 remove_suffix (name, suffix)
137      register char *name, *suffix;
138 {
139   register char *np, *sp;
140
141   np = name + strlen (name);
142   sp = suffix + strlen (suffix);
143
144   while (np > name && sp > suffix)
145     if (*--np != *--sp)
146       return;
147   if (np > name)
148     *np = '\0';
149 }