blob: 5092a5dfc43f2982799e0cb8b19e58747bb75fa0 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenf6be9441999-10-13 21:12:06 +00008 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +01009//config:config RMDIR
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "rmdir (3.5 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: rmdir is used to remove empty directories.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010014
15//applet:IF_RMDIR(APPLET_NOFORK(rmdir, rmdir, BB_DIR_BIN, BB_SUID_DROP, rmdir))
16
17//kbuild:lib-$(CONFIG_RMDIR) += rmdir.o
Eric Andersenf6be9441999-10-13 21:12:06 +000018
Manuel Novoa III cad53642003-03-19 09:13:01 +000019/* BB_AUDIT SUSv3 compliant */
20/* http://www.opengroup.org/onlinepubs/007904975/utilities/rmdir.html */
Matt Kraai13506662001-08-29 21:18:47 +000021
Pere Orga34425382011-03-31 14:43:25 +020022//usage:#define rmdir_trivial_usage
23//usage: "[OPTIONS] DIRECTORY..."
24//usage:#define rmdir_full_usage "\n\n"
25//usage: "Remove DIRECTORY if it is empty\n"
Pere Orga34425382011-03-31 14:43:25 +020026//usage: "\n -p Include parents"
Denys Vlasenko036585a2017-08-08 16:38:18 +020027//usage: IF_LONG_OPTS(
28//usage: "\n --ignore-fail-on-non-empty"
Pere Orga34425382011-03-31 14:43:25 +020029//usage: )
30//usage:
31//usage:#define rmdir_example_usage
32//usage: "# rmdir /tmp/foo\n"
33
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000034#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000035
Denis Vlasenko99912ca2007-04-10 15:43:37 +000036/* This is a NOFORK applet. Be very careful! */
37
38
Simon B3698ed12012-05-06 15:03:32 +020039#define PARENTS (1 << 0)
Denys Vlasenko17f84182014-05-19 16:23:50 +020040#define VERBOSE ((1 << 1) * ENABLE_FEATURE_VERBOSE)
Denys Vlasenko036585a2017-08-08 16:38:18 +020041#define IGNORE_NON_EMPTY ((1 << 2) * ENABLE_LONG_OPTS)
Denis Vlasenko52feee92008-02-24 14:56:10 +000042
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000043int rmdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000044int rmdir_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000045{
Matt Kraai9a71af52000-11-22 01:09:38 +000046 int status = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 int flags;
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 char *path;
Matt Kraai9a71af52000-11-22 01:09:38 +000049
Denys Vlasenko036585a2017-08-08 16:38:18 +020050 flags = getopt32long(argv, "pv",
Denis Vlasenko52feee92008-02-24 14:56:10 +000051 "parents\0" No_argument "p"
52 /* Debian etch: many packages fail to be purged or installed
53 * because they desperately want this option: */
54 "ignore-fail-on-non-empty\0" No_argument "\xff"
Denys Vlasenko17f84182014-05-19 16:23:50 +020055 IF_FEATURE_VERBOSE(
56 "verbose\0" No_argument "v"
57 )
Denys Vlasenko036585a2017-08-08 16:38:18 +020058 );
Manuel Novoa III cad53642003-03-19 09:13:01 +000059 argv += optind;
Matt Kraai13506662001-08-29 21:18:47 +000060
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 if (!*argv) {
62 bb_show_usage();
63 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000064
Manuel Novoa III cad53642003-03-19 09:13:01 +000065 do {
66 path = *argv;
67
Denis Vlasenko99912ca2007-04-10 15:43:37 +000068 while (1) {
Denys Vlasenko17f84182014-05-19 16:23:50 +020069 if (flags & VERBOSE) {
70 printf("rmdir: removing directory, '%s'\n", path);
71 }
72
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 if (rmdir(path) < 0) {
Denys Vlasenko036585a2017-08-08 16:38:18 +020074#if ENABLE_LONG_OPTS
Denis Vlasenko52feee92008-02-24 14:56:10 +000075 if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY)
76 break;
77#endif
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020078 bb_perror_msg("'%s'", path); /* Match gnu rmdir msg. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 status = EXIT_FAILURE;
Denis Vlasenko52feee92008-02-24 14:56:10 +000080 } else if (flags & PARENTS) {
81 /* Note: path was not "" since rmdir succeeded. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 path = dirname(path);
Denis Vlasenko52feee92008-02-24 14:56:10 +000083 /* Path is now just the parent component. Dirname
84 * returns "." if there are no parents.
Denis Vlasenko89f0b342006-11-18 22:04:09 +000085 */
Denis Vlasenko52feee92008-02-24 14:56:10 +000086 if (NOT_LONE_CHAR(path, '.')) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000087 continue;
88 }
89 }
90 break;
Denis Vlasenko99912ca2007-04-10 15:43:37 +000091 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000092 } while (*++argv);
Matt Kraai13506662001-08-29 21:18:47 +000093
Matt Kraai9a71af52000-11-22 01:09:38 +000094 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +000095}