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)
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. */
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> */
28 #include <sys/types.h>
34 void strip_trailing_slashes ();
36 /* The name this program was run with. */
39 /* If nonzero, remove empty parent directories. */
40 static int empty_paths
;
42 /* If nonzero, display usage information and exit. */
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},
57 /* Remove any empty parent directories of `path'.
58 Replaces '/' characters in `path' with NULs. */
61 remove_parents (char *path
)
67 slash
= strrchr (path
, '/');
70 /* Remove any characters after the slash, skipping any extra
72 while (slash
> path
&& *slash
== '/')
76 while (rmdir (path
) == 0);
83 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
87 printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name
);
89 Remove the DIRECTORY(ies), if they are empty.\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"));
99 main (int argc
, char **argv
)
104 program_name
= argv
[0];
105 setlocale (LC_ALL
, "");
106 bindtextdomain (PACKAGE
, LOCALEDIR
);
107 textdomain (PACKAGE
);
111 while ((optc
= getopt_long (argc
, argv
, "p", longopts
, (int *) 0)) != EOF
)
115 case 0: /* Long option. */
127 printf ("rmdir - %s\n", version_string
);
136 error (0, 0, _("too few arguments"));
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
]);
150 else if (empty_paths
)
151 remove_parents (argv
[optind
]);