tests: more automated quote adjustment
[coreutils/ericb.git] / src / basename.c
bloba8e59c2e11b3491ecf8e113f6e50eb29a1852642
1 /* basename -- strip directory and suffix from file names
2 Copyright (C) 1990-1997, 1999-2012 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 3 of the License, or
7 (at your option) 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, see <http://www.gnu.org/licenses/>. */
17 /* Usage: basename name [suffix]
18 NAME is a file name; SUFFIX is a suffix to strip from it.
20 basename /usr/foo/lossage/functions.l
21 => functions.l
22 basename /usr/foo/lossage/functions.l .l
23 => functions
24 basename functions.lisp p
25 => functions.lis */
27 #include <config.h>
28 #include <getopt.h>
29 #include <stdio.h>
30 #include <sys/types.h>
32 #include "system.h"
33 #include "long-options.h"
34 #include "error.h"
35 #include "quote.h"
37 /* The official name of this program (e.g., no `g' prefix). */
38 #define PROGRAM_NAME "basename"
40 #define AUTHORS proper_name ("David MacKenzie")
42 void
43 usage (int status)
45 if (status != EXIT_SUCCESS)
46 emit_try_help ();
47 else
49 printf (_("\
50 Usage: %s NAME [SUFFIX]\n\
51 or: %s OPTION\n\
52 "),
53 program_name, program_name);
54 fputs (_("\
55 Print NAME with any leading directory components removed.\n\
56 If specified, also remove a trailing SUFFIX.\n\
57 \n\
58 "), stdout);
59 fputs (HELP_OPTION_DESCRIPTION, stdout);
60 fputs (VERSION_OPTION_DESCRIPTION, stdout);
61 printf (_("\
62 \n\
63 Examples:\n\
64 %s /usr/bin/sort Output \"sort\".\n\
65 %s include/stdio.h .h Output \"stdio\".\n\
66 "),
67 program_name, program_name);
68 emit_ancillary_info ();
70 exit (status);
73 /* Remove SUFFIX from the end of NAME if it is there, unless NAME
74 consists entirely of SUFFIX. */
76 static void
77 remove_suffix (char *name, const char *suffix)
79 char *np;
80 const char *sp;
82 np = name + strlen (name);
83 sp = suffix + strlen (suffix);
85 while (np > name && sp > suffix)
86 if (*--np != *--sp)
87 return;
88 if (np > name)
89 *np = '\0';
92 int
93 main (int argc, char **argv)
95 char *name;
97 initialize_main (&argc, &argv);
98 set_program_name (argv[0]);
99 setlocale (LC_ALL, "");
100 bindtextdomain (PACKAGE, LOCALEDIR);
101 textdomain (PACKAGE);
103 atexit (close_stdout);
105 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
106 usage, AUTHORS, (char const *) NULL);
107 if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
108 usage (EXIT_FAILURE);
110 if (argc < optind + 1)
112 error (0, 0, _("missing operand"));
113 usage (EXIT_FAILURE);
116 if (optind + 2 < argc)
118 error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));
119 usage (EXIT_FAILURE);
122 name = base_name (argv[optind]);
123 strip_trailing_slashes (name);
125 /* Per POSIX, `basename // /' must return `//' on platforms with
126 distinct //. On platforms with drive letters, this generalizes
127 to making `basename c: :' return `c:'. This rule is captured by
128 skipping suffix stripping if base_name returned an absolute path
129 or a drive letter (only possible if name is a file-system
130 root). */
131 if (argc == optind + 2 && IS_RELATIVE_FILE_NAME (name)
132 && ! FILE_SYSTEM_PREFIX_LEN (name))
133 remove_suffix (name, argv[optind + 1]);
135 puts (name);
136 free (name);
138 exit (EXIT_SUCCESS);