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