* src/dd.c (flags): noatime and nofollow now depend on
[coreutils/bo.git] / src / basename.c
blobf2617b1ea3bc8e312e4d160ce4f4a77ae8c8f062
1 /* basename -- strip directory and suffix from file names
2 Copyright (C) 1990-1997, 1999-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Usage: basename name [suffix]
19 NAME is a file name; 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 <getopt.h>
30 #include <stdio.h>
31 #include <sys/types.h>
33 #include "system.h"
34 #include "long-options.h"
35 #include "error.h"
36 #include "quote.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 != EXIT_SUCCESS)
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 (_("\
67 \n\
68 Examples:\n\
69 %s /usr/bin/sort Output \"sort\".\n\
70 %s include/stdio.h .h Output \"stdio\".\n\
71 "),
72 program_name, program_name);
73 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
75 exit (status);
78 /* Remove SUFFIX from the end of NAME if it is there, unless NAME
79 consists entirely of SUFFIX. */
81 static void
82 remove_suffix (char *name, const char *suffix)
84 char *np;
85 const char *sp;
87 np = name + strlen (name);
88 sp = suffix + strlen (suffix);
90 while (np > name && sp > suffix)
91 if (*--np != *--sp)
92 return;
93 if (np > name)
94 *np = '\0';
97 int
98 main (int argc, char **argv)
100 char *name;
102 initialize_main (&argc, &argv);
103 program_name = argv[0];
104 setlocale (LC_ALL, "");
105 bindtextdomain (PACKAGE, LOCALEDIR);
106 textdomain (PACKAGE);
108 atexit (close_stdout);
110 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
111 usage, AUTHORS, (char const *) NULL);
112 if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
113 usage (EXIT_FAILURE);
115 if (argc < optind + 1)
117 error (0, 0, _("missing operand"));
118 usage (EXIT_FAILURE);
121 if (optind + 2 < argc)
123 error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
124 usage (EXIT_FAILURE);
127 name = base_name (argv[optind]);
128 strip_trailing_slashes (name);
130 /* Per POSIX, `basename // /' must return `//' on platforms with
131 distinct //. On platforms with drive letters, this generalizes
132 to making `basename c: :' return `c:'. This rule is captured by
133 skipping suffix stripping if base_name returned an absolute path
134 or a drive letter (only possible if name is a file-system
135 root). */
136 if (argc == optind + 2 && IS_RELATIVE_FILE_NAME (name)
137 && ! FILE_SYSTEM_PREFIX_LEN (name))
138 remove_suffix (name, argv[optind + 1]);
140 puts (name);
141 free (name);
143 exit (EXIT_SUCCESS);