hostid: fix behavior on identifiers starting with zeros
[platform/upstream/busybox.git] / 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 source tree.
8  */
9
10 /* BB_AUDIT SUSv3 compliant */
11 /* http://www.opengroup.org/onlinepubs/007904975/utilities/rmdir.html */
12
13 //usage:#define rmdir_trivial_usage
14 //usage:       "[OPTIONS] DIRECTORY..."
15 //usage:#define rmdir_full_usage "\n\n"
16 //usage:       "Remove DIRECTORY if it is empty\n"
17 //usage:        IF_FEATURE_RMDIR_LONG_OPTIONS(
18 //usage:     "\n        -p|--parents    Include parents"
19 //usage:     "\n        --ignore-fail-on-non-empty"
20 //usage:        )
21 //usage:        IF_NOT_FEATURE_RMDIR_LONG_OPTIONS(
22 //usage:     "\n        -p      Include parents"
23 //usage:        )
24 //usage:
25 //usage:#define rmdir_example_usage
26 //usage:       "# rmdir /tmp/foo\n"
27
28 #include "libbb.h"
29
30 /* This is a NOFORK applet. Be very careful! */
31
32
33 #define PARENTS 0x01
34 #define IGNORE_NON_EMPTY 0x02
35
36 int rmdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
37 int rmdir_main(int argc UNUSED_PARAM, char **argv)
38 {
39         int status = EXIT_SUCCESS;
40         int flags;
41         char *path;
42
43 #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
44         static const char rmdir_longopts[] ALIGN1 =
45                 "parents\0"                  No_argument "p"
46                 /* Debian etch: many packages fail to be purged or installed
47                  * because they desperately want this option: */
48                 "ignore-fail-on-non-empty\0" No_argument "\xff"
49                 ;
50         applet_long_options = rmdir_longopts;
51 #endif
52         flags = getopt32(argv, "p");
53         argv += optind;
54
55         if (!*argv) {
56                 bb_show_usage();
57         }
58
59         do {
60                 path = *argv;
61
62                 while (1) {
63                         if (rmdir(path) < 0) {
64 #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
65                                 if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY)
66                                         break;
67 #endif
68                                 bb_perror_msg("'%s'", path);  /* Match gnu rmdir msg. */
69                                 status = EXIT_FAILURE;
70                         } else if (flags & PARENTS) {
71                                 /* Note: path was not "" since rmdir succeeded. */
72                                 path = dirname(path);
73                                 /* Path is now just the parent component.  Dirname
74                                  * returns "." if there are no parents.
75                                  */
76                                 if (NOT_LONE_CHAR(path, '.')) {
77                                         continue;
78                                 }
79                         }
80                         break;
81                 }
82         } while (*++argv);
83
84         return status;
85 }