trylink: automatically use custom link script if user provides one
[oweals/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 tarball for details.
8  */
9
10 /* BB_AUDIT SUSv3 compliant */
11 /* http://www.opengroup.org/onlinepubs/007904975/utilities/rmdir.html */
12
13 #include <libgen.h>
14 #include "libbb.h"
15
16 /* This is a NOFORK applet. Be very careful! */
17
18
19 int rmdir_main(int argc, char **argv);
20 int rmdir_main(int argc, char **argv)
21 {
22         int status = EXIT_SUCCESS;
23         int flags;
24         int do_dot;
25         char *path;
26
27         flags = getopt32(argc, argv, "p");
28         argv += optind;
29
30         if (!*argv) {
31                 bb_show_usage();
32         }
33
34         do {
35                 path = *argv;
36
37                 /* Record if the first char was a '.' so we can use dirname later. */
38                 do_dot = (*path == '.');
39
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 '.'.
50                                  */
51                                 if (do_dot || (*path != '.') || path[1]) {
52                                         continue;
53                                 }
54                         }
55                         break;
56                 }
57
58         } while (*++argv);
59
60         return status;
61 }