Reimplement rename()
[git/pclouds.git] / box / coreutils / rmdir.c
blobc1b89e4279bc829595fe89c9aa0e88f6ad1bcfba
1 /* vi: set sw=4 ts=4: */
2 /*
3 * rmdir implementation for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
10 /* BB_AUDIT SUSv3 compliant */
11 /* http://www.opengroup.org/onlinepubs/007904975/utilities/rmdir.html */
13 #include <libgen.h>
14 #include "libbb.h"
16 /* This is a NOFORK applet. Be very careful! */
19 int rmdir_main(int argc, char **argv);
20 int rmdir_main(int argc, char **argv)
22 int status = EXIT_SUCCESS;
23 int flags;
24 int do_dot;
25 char *path;
27 flags = getopt32(argc, argv, "p");
28 argv += optind;
30 if (!*argv) {
31 bb_show_usage();
34 do {
35 path = *argv;
37 /* Record if the first char was a '.' so we can use dirname later. */
38 do_dot = (*path == '.');
40 while (1) {
41 if (rmdir(path) < 0) {
42 bb_perror_msg("'%s'", path); /* Match gnu rmdir msg. */
43 status = EXIT_FAILURE;
44 } else if (flags) {
45 /* Note: path was not empty or null since rmdir succeeded. */
46 path = dirname(path);
47 /* Path is now just the parent component. Note that dirname
48 * returns "." if there are no parents. We must distinguish
49 * this from the case of the original path starting with '.'.
51 if (do_dot || (*path != '.') || path[1]) {
52 continue;
55 break;
58 } while (*++argv);
60 return status;