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