(PROGRAM_NAME, AUTHORS): Define and use.
[platform/upstream/coreutils.git] / src / basename.c
1 /* basename -- strip directory and suffix from filenames
2    Copyright (C) 1990-1997, 1999 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 Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 <config.h>
29 #include <stdio.h>
30 #include <sys/types.h>
31
32 #include "system.h"
33 #include "long-options.h"
34 #include "error.h"
35
36 /* The official name of this program (e.g., no `g' prefix).  */
37 #define PROGRAM_NAME "basename"
38
39 #define AUTHORS "FIXME unknown"
40
41 char *base_name ();
42 void strip_trailing_slashes ();
43
44 /* The name this program was run with. */
45 char *program_name;
46
47 void
48 usage (int status)
49 {
50   if (status != 0)
51     fprintf (stderr, _("Try `%s --help' for more information.\n"),
52              program_name);
53   else
54     {
55       printf (_("\
56 Usage: %s NAME [SUFFIX]\n\
57   or:  %s OPTION\n\
58 "),
59               program_name, program_name);
60       printf (_("\
61 Print NAME with any leading directory components removed.\n\
62 If specified, also remove a trailing SUFFIX.\n\
63 \n\
64   --help      display this help and exit\n\
65   --version   output version information and exit\n\
66 "));
67       puts (_("\nReport bugs to <bug-sh-utils@gnu.org>."));
68     }
69   exit (status);
70 }
71
72 /* Remove SUFFIX from the end of NAME if it is there, unless NAME
73    consists entirely of SUFFIX. */
74
75 static void
76 remove_suffix (char *name, const char *suffix)
77 {
78   char *np;
79   const char *sp;
80
81   np = name + strlen (name);
82   sp = suffix + strlen (suffix);
83
84   while (np > name && sp > suffix)
85     if (*--np != *--sp)
86       return;
87   if (np > name)
88     *np = '\0';
89 }
90
91 int
92 main (int argc, char **argv)
93 {
94   char *name;
95
96   program_name = argv[0];
97   setlocale (LC_ALL, "");
98   bindtextdomain (PACKAGE, LOCALEDIR);
99   textdomain (PACKAGE);
100
101   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
102                       AUTHORS, usage);
103
104   if (argc == 1 || argc > 3)
105     {
106       error (0, 0, (argc == 1 ? _("too few arguments")
107                     : _("too many arguments")));
108       usage (1);
109     }
110
111   strip_trailing_slashes (argv[1]);
112
113   name = base_name (argv[1]);
114
115   if (argc == 3)
116     remove_suffix (name, argv[2]);
117
118   puts (name);
119
120   exit (0);
121 }