(usage): Include one- or two-line synopsis in --help output.
[platform/upstream/coreutils.git] / src / rmdir.c
1 /* rmdir -- remove directories
2    Copyright (C) 1990, 1991, 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 /* Options:
19    -p, --parent         Remove any parent dirs that are explicitly mentioned
20                         in an argument, if they become empty after the
21                         argument file is removed.
22
23    David MacKenzie <djm@ai.mit.edu>  */
24
25 #include <config.h>
26 #include <stdio.h>
27 #include <getopt.h>
28 #include <sys/types.h>
29
30 #include "system.h"
31 #include "version.h"
32 #include "error.h"
33
34 void strip_trailing_slashes ();
35
36 static void remove_parents ();
37 static void usage ();
38
39 /* The name this program was run with. */
40 char *program_name;
41
42 /* If nonzero, remove empty parent directories. */
43 static int empty_paths;
44
45 /* If non-zero, display usage information and exit.  */
46 static int show_help;
47
48 /* If non-zero, print the version on standard output and exit.  */
49 static int show_version;
50
51 static struct option const longopts[] =
52 {
53   {"path", no_argument, &empty_paths, 1},
54   {"parents", no_argument, &empty_paths, 1},
55   {"help", no_argument, &show_help, 1},
56   {"version", no_argument, &show_version, 1},
57   {NULL, 0, NULL, 0}
58 };
59
60 void
61 main (argc, argv)
62      int argc;
63      char **argv;
64 {
65   int errors = 0;
66   int optc;
67
68   program_name = argv[0];
69   empty_paths = 0;
70
71   while ((optc = getopt_long (argc, argv, "p", longopts, (int *) 0)) != EOF)
72     {
73       switch (optc)
74         {
75         case 0:                 /* Long option. */
76           break;
77         case 'p':
78           empty_paths = 1;
79           break;
80         default:
81           usage (1);
82         }
83     }
84
85   if (show_version)
86     {
87       printf ("%s\n", version_string);
88       exit (0);
89     }
90
91   if (show_help)
92     usage (0);
93
94   if (optind == argc)
95     {
96       error (0, 0, "too few arguments");
97       usage (1);
98     }
99
100   for (; optind < argc; ++optind)
101     {
102       /* Stripping slashes is harmless for rmdir;
103          if the arg is not a directory, it will fail with ENOTDIR.  */
104       strip_trailing_slashes (argv[optind]);
105       if (rmdir (argv[optind]) != 0)
106         {
107           error (0, errno, "%s", argv[optind]);
108           errors = 1;
109         }
110       else if (empty_paths)
111         remove_parents (argv[optind]);
112     }
113
114   exit (errors);
115 }
116
117 /* Remove any empty parent directories of `path'.
118    Replaces '/' characters in `path' with NULs. */
119
120 static void
121 remove_parents (path)
122      char *path;
123 {
124   char *slash;
125
126   do
127     {
128       slash = strrchr (path, '/');
129       if (slash == NULL)
130         break;
131       /* Remove any characters after the slash, skipping any extra
132          slashes in a row. */
133       while (slash > path && *slash == '/')
134         --slash;
135       slash[1] = 0;
136     }
137   while (rmdir (path) == 0);
138 }
139
140 static void
141 usage (status)
142      int status;
143 {
144   if (status != 0)
145     fprintf (stderr, "Try `%s --help' for more information.\n",
146              program_name);
147   else
148     {
149       printf ("Usage: %s [OPTION]... DIRECTORY...\n", program_name);
150       printf ("\
151 \n\
152   -p, --parents   remove explicit parent directories if being emptied\n\
153       --help      display this help and exit\n\
154       --version   output version information and exit\n");
155     }
156   exit (status);
157 }