blob: 2450a43a3fe60cdbab2c1276fe216e2bfbae56b0 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenf6be9441999-10-13 21:12:06 +00002/*
Manuel Novoa III cad53642003-03-19 09:13:01 +00003 * rmdir implementation for busybox
Eric Andersenf6be9441999-10-13 21:12:06 +00004 *
Manuel Novoa III cad53642003-03-19 09:13:01 +00005 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
Eric Andersenf6be9441999-10-13 21:12:06 +00006 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenf6be9441999-10-13 21:12:06 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* BB_AUDIT SUSv3 compliant */
11/* http://www.opengroup.org/onlinepubs/007904975/utilities/rmdir.html */
Matt Kraai13506662001-08-29 21:18:47 +000012
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000014
Denis Vlasenko99912ca2007-04-10 15:43:37 +000015/* This is a NOFORK applet. Be very careful! */
16
17
Denis Vlasenko52feee92008-02-24 14:56:10 +000018#define PARENTS 0x01
19#define IGNORE_NON_EMPTY 0x02
20
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000021int rmdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000022int rmdir_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000023{
Matt Kraai9a71af52000-11-22 01:09:38 +000024 int status = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +000025 int flags;
Manuel Novoa III cad53642003-03-19 09:13:01 +000026 char *path;
Matt Kraai9a71af52000-11-22 01:09:38 +000027
Denis Vlasenko52feee92008-02-24 14:56:10 +000028#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 Vlasenkofe7cd642007-08-18 15:32:12 +000037 flags = getopt32(argv, "p");
Manuel Novoa III cad53642003-03-19 09:13:01 +000038 argv += optind;
Matt Kraai13506662001-08-29 21:18:47 +000039
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 if (!*argv) {
41 bb_show_usage();
42 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000043
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 do {
45 path = *argv;
46
Denis Vlasenko99912ca2007-04-10 15:43:37 +000047 while (1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 if (rmdir(path) < 0) {
Denis Vlasenko52feee92008-02-24 14:56:10 +000049#if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
50 if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY)
51 break;
52#endif
Denis Vlasenko89f0b342006-11-18 22:04:09 +000053 bb_perror_msg("'%s'", path); /* Match gnu rmdir msg. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 status = EXIT_FAILURE;
Denis Vlasenko52feee92008-02-24 14:56:10 +000055 } else if (flags & PARENTS) {
56 /* Note: path was not "" since rmdir succeeded. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 path = dirname(path);
Denis Vlasenko52feee92008-02-24 14:56:10 +000058 /* Path is now just the parent component. Dirname
59 * returns "." if there are no parents.
Denis Vlasenko89f0b342006-11-18 22:04:09 +000060 */
Denis Vlasenko52feee92008-02-24 14:56:10 +000061 if (NOT_LONE_CHAR(path, '.')) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 continue;
63 }
64 }
65 break;
Denis Vlasenko99912ca2007-04-10 15:43:37 +000066 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 } while (*++argv);
Matt Kraai13506662001-08-29 21:18:47 +000068
Matt Kraai9a71af52000-11-22 01:09:38 +000069 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +000070}