.
[coreutils.git] / src / rmdir.c
blob5e66d3bd2a0b44317d9a3a554dc1ef21d6e832a6
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"));
94 exit (status);
97 int
98 main (int argc, char **argv)
100 int errors = 0;
101 int optc;
103 program_name = argv[0];
104 setlocale (LC_ALL, "");
105 bindtextdomain (PACKAGE, LOCALEDIR);
106 textdomain (PACKAGE);
108 empty_paths = 0;
110 while ((optc = getopt_long (argc, argv, "p", longopts, (int *) 0)) != EOF)
112 switch (optc)
114 case 0: /* Long option. */
115 break;
116 case 'p':
117 empty_paths = 1;
118 break;
119 default:
120 usage (1);
124 if (show_version)
126 printf ("rmdir - %s\n", PACKAGE_VERSION);
127 exit (0);
130 if (show_help)
131 usage (0);
133 if (optind == argc)
135 error (0, 0, _("too few arguments"));
136 usage (1);
139 for (; optind < argc; ++optind)
141 /* Stripping slashes is harmless for rmdir;
142 if the arg is not a directory, it will fail with ENOTDIR. */
143 strip_trailing_slashes (argv[optind]);
144 if (rmdir (argv[optind]) != 0)
146 error (0, errno, "%s", argv[optind]);
147 errors = 1;
149 else if (empty_paths)
150 remove_parents (argv[optind]);
153 exit (errors);