Tizen 2.0 Release
[external/tizen-coreutils.git] / src / dirname.c
1 /* dirname -- strip suffix from file name
2
3    Copyright (C) 1990-1997, 1999-2002, 2004, 2005, 2006 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 "quote.h"
31
32 /* The official name of this program (e.g., no `g' prefix).  */
33 #define PROGRAM_NAME "dirname"
34
35 #define AUTHORS "David MacKenzie", "Jim Meyering"
36
37 /* The name this program was run with. */
38 char *program_name;
39
40 void
41 usage (int status)
42 {
43   if (status != EXIT_SUCCESS)
44     fprintf (stderr, _("Try `%s --help' for more information.\n"),
45              program_name);
46   else
47     {
48       printf (_("\
49 Usage: %s NAME\n\
50   or:  %s OPTION\n\
51 "),
52               program_name, program_name);
53       fputs (_("\
54 Print NAME with its trailing /component removed; if NAME contains no /'s,\n\
55 output `.' (meaning the current directory).\n\
56 \n\
57 "), stdout);
58       fputs (HELP_OPTION_DESCRIPTION, stdout);
59       fputs (VERSION_OPTION_DESCRIPTION, stdout);
60       printf (_("\
61 \n\
62 Examples:\n\
63   %s /usr/bin/sort  Output \"/usr/bin\".\n\
64   %s stdio.h        Output \".\".\n\
65 "),
66               program_name, program_name);
67       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
68     }
69   exit (status);
70 }
71
72 int
73 main (int argc, char **argv)
74 {
75   static char const dot = '.';
76   char const *result;
77   size_t len;
78
79   initialize_main (&argc, &argv);
80   program_name = argv[0];
81   setlocale (LC_ALL, "");
82   bindtextdomain (PACKAGE, LOCALEDIR);
83   textdomain (PACKAGE);
84
85   atexit (close_stdout);
86
87   parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
88                       usage, AUTHORS, (char const *) NULL);
89   if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
90     usage (EXIT_FAILURE);
91
92   if (argc < optind + 1)
93     {
94       error (0, 0, _("missing operand"));
95       usage (EXIT_FAILURE);
96     }
97
98   if (optind + 1 < argc)
99     {
100       error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
101       usage (EXIT_FAILURE);
102     }
103
104   result = argv[optind];
105   len = dir_len (result);
106
107   if (! len)
108     {
109       result = &dot;
110       len = 1;
111     }
112
113   fwrite (result, 1, len, stdout);
114   putchar ('\n');
115
116   exit (EXIT_SUCCESS);
117 }