*** empty log message ***
[coreutils.git] / src / basename.c
blob139b9e90c39d02550fcbc9edded5522b2b8835b4
1 /* basename -- strip directory and suffix from filenames
2 Copyright (C) 1990-1997, 1999, 2000, 2001 Free Software Foundation, Inc.
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.
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.
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. */
18 /* Usage: basename name [suffix]
19 NAME is a pathname; SUFFIX is a suffix to strip from it.
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 */
28 #include <config.h>
29 #include <stdio.h>
30 #include <sys/types.h>
32 #include "system.h"
33 #include "long-options.h"
34 #include "dirname.h"
35 #include "error.h"
36 #include "closeout.h"
38 /* The official name of this program (e.g., no `g' prefix). */
39 #define PROGRAM_NAME "basename"
41 #define AUTHORS "FIXME unknown"
43 /* The name this program was run with. */
44 char *program_name;
46 void
47 usage (int status)
49 if (status != 0)
50 fprintf (stderr, _("Try `%s --help' for more information.\n"),
51 program_name);
52 else
54 printf (_("\
55 Usage: %s NAME [SUFFIX]\n\
56 or: %s OPTION\n\
57 "),
58 program_name, program_name);
59 fputs (_("\
60 Print NAME with any leading directory components removed.\n\
61 If specified, also remove a trailing SUFFIX.\n\
62 \n\
63 "), stdout);
64 fputs (HELP_OPTION_DESCRIPTION, stdout);
65 fputs (VERSION_OPTION_DESCRIPTION, stdout);
66 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
68 exit (status);
71 /* Remove SUFFIX from the end of NAME if it is there, unless NAME
72 consists entirely of SUFFIX. */
74 static void
75 remove_suffix (char *name, const char *suffix)
77 char *np;
78 const char *sp;
80 np = name + strlen (name);
81 sp = suffix + strlen (suffix);
83 while (np > name && sp > suffix)
84 if (*--np != *--sp)
85 return;
86 if (np > name)
87 *np = '\0';
90 int
91 main (int argc, char **argv)
93 char *name;
95 program_name = argv[0];
96 setlocale (LC_ALL, "");
97 bindtextdomain (PACKAGE, LOCALEDIR);
98 textdomain (PACKAGE);
100 atexit (close_stdout);
102 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
103 AUTHORS, 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], "--"))
108 --argc;
109 ++argv;
112 if (argc == 1 || argc > 3)
114 error (0, 0, (argc == 1 ? _("too few arguments")
115 : _("too many arguments")));
116 usage (1);
119 name = base_name (argv[1]);
120 name[base_len (name)] = '\0';
122 if (argc == 3)
123 remove_suffix (name, argv[2]);
125 puts (name);
127 exit (0);