a40c8267d6c9a800163773e14e443ac66195f5a8
[platform/upstream/coreutils.git] / src / dirname.c
1 /* dirname -- strip filename suffix from pathname
2    Copyright (C) 1990, 1991 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 /* Written by David MacKenzie and Jim Meyering. */
19
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
30
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <getopt.h>
34
35 #include "version.h"
36 #include "system.h"
37
38 void strip_trailing_slashes ();
39
40 /* The name this program was run with. */
41 char *program_name;
42
43 /* If non-zero, display usage information and exit.  */
44 static int show_help;
45
46 /* If non-zero, print the version on standard output and exit.  */
47 static int show_version;
48
49 static struct option const long_options[] =
50 {
51   {"help", no_argument, &show_help, 1},
52   {"version", no_argument, &show_version, 1},
53   {0, 0, 0, 0}
54 };
55
56 static void
57 usage (status)
58      int status;
59 {
60   fprintf (status == 0 ? stdout : stderr, "\
61 Usage: %s [OPTION]... PATH\n\
62 ",
63            program_name);
64
65   if (status != 0)
66     fprintf (stderr, "Try `%s --help' for more information.\n",
67              program_name);
68   else
69
70     printf ("\
71 \n\
72   --help      display this help and exit\n\
73   --version   output version information and exit\n\
74 ");
75
76   exit (status);
77 }
78
79 void
80 main (argc, argv)
81      int argc;
82      char **argv;
83 {
84   register char *path;
85   register char *slash;
86   int c;
87
88   program_name = argv[0];
89
90   while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF)
91     {
92       switch (c)
93         {
94         case 0:
95           break;
96
97         default:
98           usage (1);
99         }
100     }
101
102   if (show_version)
103     {
104       printf ("%s\n", version_string);
105       exit (0);
106     }
107
108   if (show_help)
109     usage (0);
110
111   if (argc - optind != 1)
112     usage (1);
113
114   path = argv[optind];
115   strip_trailing_slashes (path);
116
117   slash = rindex (path, '/');
118   if (slash == NULL)
119     path = ".";
120   else
121     {
122       /* Remove any trailing slashes and final element. */
123       while (slash > path && *slash == '/')
124         --slash;
125       slash[1] = 0;
126     }
127   puts (path);
128
129   exit (0);
130 }