1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-07-28 19:39:52 +02:00
Files
coreutils/src/basename.c
T

138 lines
3.4 KiB
C
Raw Normal View History

2005-06-02 05:17:24 +00:00
/* basename -- strip directory and suffix from file names
2005-04-05 22:21:50 +00:00
Copyright (C) 1990-1997, 1999-2005 Free Software Foundation, Inc.
1992-11-01 05:44:29 +00:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
1996-03-24 18:33:12 +00:00
along with this program; if not, write to the Free Software Foundation,
2005-05-14 07:58:31 +00:00
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
1992-11-01 05:44:29 +00:00
/* Usage: basename name [suffix]
2005-06-02 05:17:24 +00:00
NAME is a file name; SUFFIX is a suffix to strip from it.
1992-11-01 05:44:29 +00:00
basename /usr/foo/lossage/functions.l
=> functions.l
basename /usr/foo/lossage/functions.l .l
=> functions
basename functions.lisp p
=> functions.lis */
1993-10-12 14:49:11 +00:00
#include <config.h>
2004-09-21 22:01:28 +00:00
#include <getopt.h>
1992-11-01 05:44:29 +00:00
#include <stdio.h>
#include <sys/types.h>
1992-11-01 05:44:29 +00:00
#include "system.h"
1994-02-13 20:27:08 +00:00
#include "long-options.h"
2001-05-12 16:05:12 +00:00
#include "dirname.h"
#include "error.h"
#include "quote.h"
1992-11-01 05:44:29 +00:00
1999-03-31 04:11:35 +00:00
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "basename"
#define AUTHORS "FIXME unknown"
1999-03-31 04:11:35 +00:00
/* The name this program was run with. */
char *program_name;
1999-01-25 14:33:38 +00:00
void
1996-01-06 11:44:05 +00:00
usage (int status)
{
if (status != EXIT_SUCCESS)
1996-04-30 03:26:34 +00:00
fprintf (stderr, _("Try `%s --help' for more information.\n"),
1993-10-17 03:57:04 +00:00
program_name);
else
1993-10-26 00:11:14 +00:00
{
1996-04-30 03:26:34 +00:00
printf (_("\
Usage: %s NAME [SUFFIX]\n\
1994-02-14 21:19:12 +00:00
or: %s OPTION\n\
1996-04-30 03:26:34 +00:00
"),
1994-02-14 21:19:12 +00:00
program_name, program_name);
fputs (_("\
Print NAME with any leading directory components removed.\n\
If specified, also remove a trailing SUFFIX.\n\
1993-10-17 03:57:04 +00:00
\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
2005-04-05 22:21:50 +00:00
printf (_("\
\n\
Examples:\n\
%s /usr/bin/sort Output \"sort\".\n\
%s include/stdio.h .h Output \"stdio\".\n\
"),
program_name, program_name);
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
1993-10-26 00:11:14 +00:00
}
1993-10-17 03:57:04 +00:00
exit (status);
}
/* Remove SUFFIX from the end of NAME if it is there, unless NAME
consists entirely of SUFFIX. */
static void
remove_suffix (char *name, const char *suffix)
{
char *np;
const char *sp;
np = name + strlen (name);
sp = suffix + strlen (suffix);
while (np > name && sp > suffix)
if (*--np != *--sp)
return;
if (np > name)
*np = '\0';
}
int
1996-01-06 11:44:05 +00:00
main (int argc, char **argv)
1992-11-01 05:44:29 +00:00
{
char *name;
2003-06-17 18:13:23 +00:00
initialize_main (&argc, &argv);
program_name = argv[0];
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
2000-05-07 14:49:06 +00:00
atexit (close_stdout);
1999-03-31 04:11:35 +00:00
parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
usage, AUTHORS, (char const *) NULL);
2004-11-17 00:56:25 +00:00
if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
2004-09-21 22:01:28 +00:00
usage (EXIT_FAILURE);
2004-09-21 22:01:28 +00:00
if (argc < optind + 1)
1994-10-21 05:25:03 +00:00
{
error (0, 0, _("missing operand"));
usage (EXIT_FAILURE);
}
2004-09-21 22:01:28 +00:00
if (optind + 2 < argc)
{
2004-09-21 22:01:28 +00:00
error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
2002-08-31 08:52:10 +00:00
usage (EXIT_FAILURE);
1994-10-21 05:25:03 +00:00
}
2004-09-21 22:01:28 +00:00
name = base_name (argv[optind]);
2001-05-12 16:05:12 +00:00
name[base_len (name)] = '\0';
1992-11-01 05:44:29 +00:00
2004-09-21 22:01:28 +00:00
if (argc == optind + 2)
remove_suffix (name, argv[optind + 1]);
1992-11-01 05:44:29 +00:00
puts (name);
2002-08-31 08:52:10 +00:00
exit (EXIT_SUCCESS);
1992-11-01 05:44:29 +00:00
}