1 /* basename -- strip directory and suffix from filenames
2 Copyright (C) 1990-1997, 1999-2004 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)
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
23 basename /usr/foo/lossage/functions.l .l
25 basename functions.lisp p
31 #include <sys/types.h>
34 #include "long-options.h"
39 /* The official name of this program (e.g., no `g' prefix). */
40 #define PROGRAM_NAME "basename"
42 #define AUTHORS "FIXME unknown"
44 /* The name this program was run with. */
50 if (status
!= EXIT_SUCCESS
)
51 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
56 Usage: %s NAME [SUFFIX]\n\
59 program_name
, program_name
);
61 Print NAME with any leading directory components removed.\n\
62 If specified, also remove a trailing SUFFIX.\n\
65 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
66 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
67 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
72 /* Remove SUFFIX from the end of NAME if it is there, unless NAME
73 consists entirely of SUFFIX. */
76 remove_suffix (char *name
, const char *suffix
)
81 np
= name
+ strlen (name
);
82 sp
= suffix
+ strlen (suffix
);
84 while (np
> name
&& sp
> suffix
)
92 main (int argc
, char **argv
)
96 initialize_main (&argc
, &argv
);
97 program_name
= argv
[0];
98 setlocale (LC_ALL
, "");
99 bindtextdomain (PACKAGE
, LOCALEDIR
);
100 textdomain (PACKAGE
);
102 atexit (close_stdout
);
104 parse_long_options (argc
, argv
, PROGRAM_NAME
, GNU_PACKAGE
, VERSION
,
105 usage
, AUTHORS
, (char const *) NULL
);
106 if (getopt_long (argc
, argv
, "+", NULL
, NULL
) != -1)
107 usage (EXIT_FAILURE
);
109 if (argc
< optind
+ 1)
111 error (0, 0, _("missing operand"));
112 usage (EXIT_FAILURE
);
115 if (optind
+ 2 < argc
)
117 error (0, 0, _("extra operand %s"), quote (argv
[optind
+ 2]));
118 usage (EXIT_FAILURE
);
121 name
= base_name (argv
[optind
]);
122 name
[base_len (name
)] = '\0';
124 if (argc
== optind
+ 2)
125 remove_suffix (name
, argv
[optind
+ 1]);