.
[coreutils.git] / src / rmdir.c
blob513575ab8d9947e9499c3f5a35fb0a7a5b26d565
1 /* rmdir -- remove directories
2 Copyright (C) 90, 91, 95, 1996 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 "error.h"
33 void strip_trailing_slashes ();
35 /* The name this program was run with. */
36 char *program_name;
38 /* If nonzero, remove empty parent directories. */
39 static int empty_paths;
41 /* If nonzero, display usage information and exit. */
42 static int show_help;
44 /* If nonzero, print the version on standard output and exit. */
45 static int show_version;
47 static struct option const longopts[] =
49 {"path", no_argument, &empty_paths, 1},
50 {"parents", no_argument, &empty_paths, 1},
51 {"help", no_argument, &show_help, 1},
52 {"version", no_argument, &show_version, 1},
53 {NULL, 0, NULL, 0}
56 /* Remove any empty parent directories of `path'.
57 Replaces '/' characters in `path' with NULs. */
59 static void
60 remove_parents (char *path)
62 char *slash;
66 slash = strrchr (path, '/');
67 if (slash == NULL)
68 break;
69 /* Remove any characters after the slash, skipping any extra
70 slashes in a row. */
71 while (slash > path && *slash == '/')
72 --slash;
73 slash[1] = 0;
75 while (rmdir (path) == 0);
78 static void
79 usage (int status)
81 if (status != 0)
82 fprintf (stderr, _("Try `%s --help' for more information.\n"),
83 program_name);
84 else
86 printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name);
87 printf (_("\
88 Remove the DIRECTORY(ies), if they are empty.\n\
89 \n\
90 -p, --parents remove explicit parent directories if being emptied\n\
91 --help display this help and exit\n\
92 --version output version information and exit\n\
93 "));
94 puts (_("\nReport bugs to fileutils-bugs@gnu.ai.mit.edu"));
96 exit (status);
99 int
100 main (int argc, char **argv)
102 int errors = 0;
103 int optc;
105 program_name = argv[0];
106 setlocale (LC_ALL, "");
107 bindtextdomain (PACKAGE, LOCALEDIR);
108 textdomain (PACKAGE);
110 empty_paths = 0;
112 while ((optc = getopt_long (argc, argv, "p", longopts, (int *) 0)) != EOF)
114 switch (optc)
116 case 0: /* Long option. */
117 break;
118 case 'p':
119 empty_paths = 1;
120 break;
121 default:
122 usage (1);
126 if (show_version)
128 printf ("rmdir (%s) %s\n", GNU_PACKAGE, VERSION);
129 exit (0);
132 if (show_help)
133 usage (0);
135 if (optind == argc)
137 error (0, 0, _("too few arguments"));
138 usage (1);
141 for (; optind < argc; ++optind)
143 /* Stripping slashes is harmless for rmdir;
144 if the arg is not a directory, it will fail with ENOTDIR. */
145 strip_trailing_slashes (argv[optind]);
146 if (rmdir (argv[optind]) != 0)
148 error (0, errno, "%s", argv[optind]);
149 errors = 1;
151 else if (empty_paths)
152 remove_parents (argv[optind]);
155 exit (errors);