(main): Declare to be of type int, not void.
[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 "long-options.h"
34 #include "error.h"
35
36 extern char *basename ();
37 extern void strip_trailing_slashes ();
38
39 /* The name this program was run with. */
40 char *program_name;
41
42 static void
43 usage (int status)
44 {
45   if (status != 0)
46     fprintf (stderr, "Try `%s --help' for more information.\n",
47              program_name);
48   else
49     {
50       printf ("\
51 Usage: %s NAME [SUFFIX]\n\
52   or:  %s OPTION\n\
53 ",
54               program_name, program_name);
55       printf ("\
56 Print NAME with any leading directory components removed.\n\
57 If specified, also remove a trailing SUFFIX.\n\
58 \n\
59   --help      display this help and exit\n\
60   --version   output version information and exit\n\
61 ");
62     }
63   exit (status);
64 }
65
66 /* Remove SUFFIX from the end of NAME if it is there, unless NAME
67    consists entirely of SUFFIX. */
68
69 static void
70 remove_suffix (char *name, const char *suffix)
71 {
72   char *np;
73   const char *sp;
74
75   np = name + strlen (name);
76   sp = suffix + strlen (suffix);
77
78   while (np > name && sp > suffix)
79     if (*--np != *--sp)
80       return;
81   if (np > name)
82     *np = '\0';
83 }
84
85 int
86 main (int argc, char **argv)
87 {
88   char *name;
89
90   program_name = argv[0];
91   setlocale (LC_ALL, "");
92   bindtextdomain (PACKAGE, LOCALEDIR);
93   textdomain (PACKAGE);
94
95   parse_long_options (argc, argv, "basename", PACKAGE_VERSION, usage);
96
97   if (argc == 1 || argc > 3)
98     {
99       error (0, 0, "too %s arguments", argc == 1 ? "few" : "many");
100       usage (1);
101     }
102
103   strip_trailing_slashes (argv[1]);
104
105   name = basename (argv[1]);
106
107   if (argc == 3)
108     remove_suffix (name, argv[2]);
109
110   puts (name);
111
112   exit (0);
113 }