Remove __P-protected prototype for basename.
[platform/upstream/coreutils.git] / src / basename.c
1 /* basename -- strip directory and suffix from filenames
2    Copyright (C) 90, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
3
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.
8
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.
13
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
16    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 /* Usage: basename name [suffix]
19    NAME is a pathname; SUFFIX is a suffix to strip from it.
20
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 */
27
28 #include <config.h>
29 #include <stdio.h>
30 #include <sys/types.h>
31
32 #include "system.h"
33 #include "version.h"
34 #include "long-options.h"
35 #include "error.h"
36
37 extern char *basename ();
38 extern void strip_trailing_slashes ();
39
40 /* The name this program was run with. */
41 char *program_name;
42
43 static void
44 usage (int status)
45 {
46   if (status != 0)
47     fprintf (stderr, "Try `%s --help' for more information.\n",
48              program_name);
49   else
50     {
51       printf ("\
52 Usage: %s NAME [SUFFIX]\n\
53   or:  %s OPTION\n\
54 ",
55               program_name, program_name);
56       printf ("\
57 Print NAME with any leading directory components removed.\n\
58 If specified, also remove a trailing SUFFIX.\n\
59 \n\
60   --help      display this help and exit\n\
61   --version   output version information and exit\n\
62 ");
63     }
64   exit (status);
65 }
66
67 /* Remove SUFFIX from the end of NAME if it is there, unless NAME
68    consists entirely of SUFFIX. */
69
70 static void
71 remove_suffix (char *name, const char *suffix)
72 {
73   char *np;
74   const char *sp;
75
76   np = name + strlen (name);
77   sp = suffix + strlen (suffix);
78
79   while (np > name && sp > suffix)
80     if (*--np != *--sp)
81       return;
82   if (np > name)
83     *np = '\0';
84 }
85
86 void
87 main (int argc, char **argv)
88 {
89   char *name;
90
91   program_name = argv[0];
92   setlocale (LC_ALL, "");
93   bindtextdomain (PACKAGE, LOCALEDIR);
94   textdomain (PACKAGE);
95
96   parse_long_options (argc, argv, "basename", version_string, usage);
97
98   if (argc == 1 || argc > 3)
99     {
100       error (0, 0, "too %s arguments", argc == 1 ? "few" : "many");
101       usage (1);
102     }
103
104   strip_trailing_slashes (argv[1]);
105
106   name = basename (argv[1]);
107
108   if (argc == 3)
109     remove_suffix (name, argv[2]);
110
111   puts (name);
112
113   exit (0);
114 }