Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 2 | /* |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3 | * rmdir implementation for busybox |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 4 | * |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 10 | /* BB_AUDIT SUSv3 compliant */ |
| 11 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/rmdir.html */ |
Matt Kraai | 1350666 | 2001-08-29 21:18:47 +0000 | [diff] [blame] | 12 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 14 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 15 | /* This is a NOFORK applet. Be very careful! */ |
| 16 | |
| 17 | |
Denis Vlasenko | 52feee9 | 2008-02-24 14:56:10 +0000 | [diff] [blame] | 18 | #define PARENTS 0x01 |
| 19 | #define IGNORE_NON_EMPTY 0x02 |
| 20 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 21 | int rmdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 22 | int rmdir_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 23 | { |
Matt Kraai | 9a71af5 | 2000-11-22 01:09:38 +0000 | [diff] [blame] | 24 | int status = EXIT_SUCCESS; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 25 | int flags; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 26 | char *path; |
Matt Kraai | 9a71af5 | 2000-11-22 01:09:38 +0000 | [diff] [blame] | 27 | |
Denis Vlasenko | 52feee9 | 2008-02-24 14:56:10 +0000 | [diff] [blame] | 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 |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 37 | flags = getopt32(argv, "p"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | argv += optind; |
Matt Kraai | 1350666 | 2001-08-29 21:18:47 +0000 | [diff] [blame] | 39 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 40 | if (!*argv) { |
| 41 | bb_show_usage(); |
| 42 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 43 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 44 | do { |
| 45 | path = *argv; |
| 46 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 47 | while (1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 48 | if (rmdir(path) < 0) { |
Denis Vlasenko | 52feee9 | 2008-02-24 14:56:10 +0000 | [diff] [blame] | 49 | #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS |
| 50 | if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY) |
| 51 | break; |
| 52 | #endif |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 53 | bb_perror_msg("'%s'", path); /* Match gnu rmdir msg. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 54 | status = EXIT_FAILURE; |
Denis Vlasenko | 52feee9 | 2008-02-24 14:56:10 +0000 | [diff] [blame] | 55 | } else if (flags & PARENTS) { |
| 56 | /* Note: path was not "" since rmdir succeeded. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 57 | path = dirname(path); |
Denis Vlasenko | 52feee9 | 2008-02-24 14:56:10 +0000 | [diff] [blame] | 58 | /* Path is now just the parent component. Dirname |
| 59 | * returns "." if there are no parents. |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 60 | */ |
Denis Vlasenko | 52feee9 | 2008-02-24 14:56:10 +0000 | [diff] [blame] | 61 | if (NOT_LONE_CHAR(path, '.')) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 62 | continue; |
| 63 | } |
| 64 | } |
| 65 | break; |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 66 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 67 | } while (*++argv); |
Matt Kraai | 1350666 | 2001-08-29 21:18:47 +0000 | [diff] [blame] | 68 | |
Matt Kraai | 9a71af5 | 2000-11-22 01:09:38 +0000 | [diff] [blame] | 69 | return status; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 70 | } |