*** empty log message ***
[platform/upstream/coreutils.git] / src / dirname.c
1 /* dirname -- strip suffix from file name
2
3    Copyright (C) 1990-1997, 1999-2002, 2004, 2005 Free Software
4    Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* Written by David MacKenzie and Jim Meyering. */
21
22 #include <config.h>
23 #include <getopt.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26
27 #include "system.h"
28 #include "long-options.h"
29 #include "error.h"
30 #include "dirname.h"
31 #include "quote.h"
32
33 /* The official name of this program (e.g., no `g' prefix).  */
34 #define PROGRAM_NAME "dirname"
35
36 #define AUTHORS "David MacKenzie", "Jim Meyering"
37
38 /* The name this program was run with. */
39 char *program_name;
40
41 void
42 usage (int status)
43 {
44   if (status != EXIT_SUCCESS)
45     fprintf (stderr, _("Try `%s --help' for more information.\n"),
46              program_name);
47   else
48     {
49       printf (_("\
50 Usage: %s NAME\n\
51   or:  %s OPTION\n\
52 "),
53               program_name, program_name);
54       fputs (_("\
55 Print NAME with its trailing /component removed; if NAME contains no /'s,\n\
56 output `.' (meaning the current directory).\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 \"/usr/bin\".\n\
65   %s stdio.h        Output \".\".\n\
66 "),
67               program_name, program_name);
68       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
69     }
70   exit (status);
71 }
72
73 int
74 main (int argc, char **argv)
75 {
76   static char const dot = '.';
77   char const *result;
78   size_t len;
79
80   initialize_main (&argc, &argv);
81   program_name = argv[0];
82   setlocale (LC_ALL, "");
83   bindtextdomain (PACKAGE, LOCALEDIR);
84   textdomain (PACKAGE);
85
86   atexit (close_stdout);
87
88   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
89                       usage, AUTHORS, (char const *) NULL);
90   if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
91     usage (EXIT_FAILURE);
92
93   if (argc < optind + 1)
94     {
95       error (0, 0, _("missing operand"));
96       usage (EXIT_FAILURE);
97     }
98
99   if (optind + 1 < argc)
100     {
101       error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
102       usage (EXIT_FAILURE);
103     }
104
105   result = argv[optind];
106   len = dir_len (result);
107
108   if (! len)
109     {
110       result = &dot;
111       len = 1;
112     }
113
114   fwrite (result, 1, len, stdout);
115   putchar ('\n');
116
117   exit (EXIT_SUCCESS);
118 }