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 | * |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
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 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
| 14 | #include <unistd.h> |
| 15 | #include <libgen.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 16 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 17 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 18 | int rmdir_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 19 | { |
Matt Kraai | 9a71af5 | 2000-11-22 01:09:38 +0000 | [diff] [blame] | 20 | int status = EXIT_SUCCESS; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 21 | int flags; |
| 22 | int do_dot; |
| 23 | char *path; |
Matt Kraai | 9a71af5 | 2000-11-22 01:09:38 +0000 | [diff] [blame] | 24 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 25 | flags = bb_getopt_ulflags(argc, argv, "p"); |
Matt Kraai | 1350666 | 2001-08-29 21:18:47 +0000 | [diff] [blame] | 26 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 27 | argv += optind; |
Matt Kraai | 1350666 | 2001-08-29 21:18:47 +0000 | [diff] [blame] | 28 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 29 | if (!*argv) { |
| 30 | bb_show_usage(); |
| 31 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 32 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 33 | do { |
| 34 | path = *argv; |
| 35 | |
| 36 | /* Record if the first char was a '.' so we can use dirname later. */ |
| 37 | do_dot = (*path == '.'); |
| 38 | |
| 39 | do { |
| 40 | if (rmdir(path) < 0) { |
| 41 | bb_perror_msg("`%s'", path); /* Match gnu rmdir msg. */ |
| 42 | status = EXIT_FAILURE; |
| 43 | } else if (flags) { |
| 44 | /* Note: path was not empty or null since rmdir succeeded. */ |
| 45 | path = dirname(path); |
| 46 | /* Path is now just the parent component. Note that dirname |
| 47 | * returns "." if there are no parents. We must distinguish |
| 48 | * this from the case of the original path starting with '.'. |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 49 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 50 | if (do_dot || (*path != '.') || path[1]) { |
| 51 | continue; |
| 52 | } |
| 53 | } |
| 54 | break; |
| 55 | } while (1); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 56 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 57 | } while (*++argv); |
Matt Kraai | 1350666 | 2001-08-29 21:18:47 +0000 | [diff] [blame] | 58 | |
Matt Kraai | 9a71af5 | 2000-11-22 01:09:38 +0000 | [diff] [blame] | 59 | return status; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 60 | } |