merge with 1.8.1d
[coreutils.git] / src / dirname.c
blobb6b46d73850b6c0f365e09f331b0d88e9f5f0e28
1 /* dirname -- strip filename suffix from pathname
2 Copyright (C) 1990, 1991 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 2, or (at your option)
7 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, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by David MacKenzie and Jim Meyering. */
20 #ifdef HAVE_CONFIG_H
21 #if defined (CONFIG_BROKETS)
22 /* We use <config.h> instead of "config.h" so that a compilation
23 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
24 (which it would do because it found this file in $srcdir). */
25 #include <config.h>
26 #else
27 #include "config.h"
28 #endif
29 #endif
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <getopt.h>
35 #include "version.h"
36 #include "system.h"
38 void strip_trailing_slashes ();
40 /* The name this program was run with. */
41 char *program_name;
43 /* If non-zero, display usage information and exit. */
44 static int show_help;
46 /* If non-zero, print the version on standard output and exit. */
47 static int show_version;
49 static struct option const long_options[] =
51 {"help", no_argument, &show_help, 1},
52 {"version", no_argument, &show_version, 1},
53 {0, 0, 0, 0}
56 static void
57 usage ()
59 fprintf (stderr, "Usage: %s [{--help,--version}] path\n", program_name);
60 exit (1);
63 void
64 main (argc, argv)
65 int argc;
66 char **argv;
68 register char *path;
69 register char *slash;
70 int c;
72 program_name = argv[0];
74 while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF)
76 switch (c)
78 case 0:
79 break;
81 default:
82 usage ();
86 if (show_version)
88 printf ("%s\n", version_string);
89 exit (0);
92 if (show_help)
93 usage ();
95 if (argc - optind != 1)
96 usage ();
98 path = argv[optind];
99 strip_trailing_slashes (path);
101 slash = rindex (path, '/');
102 if (slash == NULL)
103 path = ".";
104 else
106 /* Remove any trailing slashes and final element. */
107 while (slash > path && *slash == '/')
108 --slash;
109 slash[1] = 0;
111 puts (path);
113 exit (0);