Initial public busybox maemo commit, 3:1.10.2.legal-1osso12
[busybox4maemo] / coreutils / rmdir.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * rmdir implementation for busybox
4  *
5  * Copyright (C) 2003  Manuel Novoa III  <mjn3@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 /* BB_AUDIT SUSv3 compliant */
11 /* http://www.opengroup.org/onlinepubs/007904975/utilities/rmdir.html */
12
13 #include "libbb.h"
14
15 /* This is a NOFORK applet. Be very careful! */
16
17
18 #define PARENTS 0x01
19 #define IGNORE_NON_EMPTY 0x02
20
21 int rmdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
22 int rmdir_main(int argc ATTRIBUTE_UNUSED, char **argv)
23 {
24         int status = EXIT_SUCCESS;
25         int flags;
26         char *path;
27
28 #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
29         static const char rmdir_longopts[] ALIGN1 =
30                 "parents\0"                  No_argument "p"
31                 /* Debian etch: many packages fail to be purged or installed
32                  * because they desperately want this option: */
33                 "ignore-fail-on-non-empty\0" No_argument "\xff"
34                 ;
35         applet_long_options = rmdir_longopts;
36 #endif
37         flags = getopt32(argv, "p");
38         argv += optind;
39
40         if (!*argv) {
41                 bb_show_usage();
42         }
43
44         do {
45                 path = *argv;
46
47                 while (1) {
48                         if (rmdir(path) < 0) {
49 #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
50                                 if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY)
51                                         break;
52 #endif
53                                 bb_perror_msg("'%s'", path);    /* Match gnu rmdir msg. */
54                                 status = EXIT_FAILURE;
55                         } else if (flags & PARENTS) {
56                                 /* Note: path was not "" since rmdir succeeded. */
57                                 path = dirname(path);
58                                 /* Path is now just the parent component.  Dirname
59                                  * returns "." if there are no parents.
60                                  */
61                                 if (NOT_LONE_CHAR(path, '.')) {
62                                         continue;
63                                 }
64                         }
65                         break;
66                 }
67         } while (*++argv);
68
69         return status;
70 }