.
[coreutils.git] / src / rmdir.c
blobe091fd063b0021adab1a0ab50a98a0d786d36d08
1 /* rmdir -- remove directories
2 Copyright (C) 1990, 1991, 1995 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 /* 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.
23 David MacKenzie <djm@ai.mit.edu> */
25 #include <config.h>
26 #include <stdio.h>
27 #include <getopt.h>
28 #include <sys/types.h>
30 #include "system.h"
31 #include "version.h"
32 #include "error.h"
34 void strip_trailing_slashes ();
36 /* The name this program was run with. */
37 char *program_name;
39 /* If nonzero, remove empty parent directories. */
40 static int empty_paths;
42 /* If nonzero, display usage information and exit. */
43 static int show_help;
45 /* If nonzero, print the version on standard output and exit. */
46 static int show_version;
48 static struct option const longopts[] =
50 {"path", no_argument, &empty_paths, 1},
51 {"parents", no_argument, &empty_paths, 1},
52 {"help", no_argument, &show_help, 1},
53 {"version", no_argument, &show_version, 1},
54 {NULL, 0, NULL, 0}
57 /* Remove any empty parent directories of `path'.
58 Replaces '/' characters in `path' with NULs. */
60 static void
61 remove_parents (char *path)
63 char *slash;
67 slash = strrchr (path, '/');
68 if (slash == NULL)
69 break;
70 /* Remove any characters after the slash, skipping any extra
71 slashes in a row. */
72 while (slash > path && *slash == '/')
73 --slash;
74 slash[1] = 0;
76 while (rmdir (path) == 0);
79 static void
80 usage (int status)
82 if (status != 0)
83 fprintf (stderr, _("Try `%s --help' for more information.\n"),
84 program_name);
85 else
87 printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name);
88 printf (_("\
89 Remove the DIRECTORY(ies), if they are empty.\n\
90 \n\
91 -p, --parents remove explicit parent directories if being emptied\n\
92 --help display this help and exit\n\
93 --version output version information and exit\n"));
95 exit (status);
98 void
99 main (int argc, char **argv)
101 int errors = 0;
102 int optc;
104 program_name = argv[0];
105 setlocale (LC_ALL, "");
106 bindtextdomain (PACKAGE, LOCALEDIR);
107 textdomain (PACKAGE);
109 empty_paths = 0;
111 while ((optc = getopt_long (argc, argv, "p", longopts, (int *) 0)) != EOF)
113 switch (optc)
115 case 0: /* Long option. */
116 break;
117 case 'p':
118 empty_paths = 1;
119 break;
120 default:
121 usage (1);
125 if (show_version)
127 printf ("rmdir - %s\n", version_string);
128 exit (0);
131 if (show_help)
132 usage (0);
134 if (optind == argc)
136 error (0, 0, _("too few arguments"));
137 usage (1);
140 for (; optind < argc; ++optind)
142 /* Stripping slashes is harmless for rmdir;
143 if the arg is not a directory, it will fail with ENOTDIR. */
144 strip_trailing_slashes (argv[optind]);
145 if (rmdir (argv[optind]) != 0)
147 error (0, errno, "%s", argv[optind]);
148 errors = 1;
150 else if (empty_paths)
151 remove_parents (argv[optind]);
154 exit (errors);