blob: c04ce78f846ac66bd19ccb05f6f505493385b377 [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 Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "rmdir (3.4 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//config:
15//config:config FEATURE_RMDIR_LONG_OPTIONS
16//config: bool "Enable long options"
17//config: default y
18//config: depends on RMDIR && LONG_OPTS
19//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020020//config: Support long options for the rmdir applet, including
21//config: --ignore-fail-on-non-empty for compatibility with GNU rmdir.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010022
23//applet:IF_RMDIR(APPLET_NOFORK(rmdir, rmdir, BB_DIR_BIN, BB_SUID_DROP, rmdir))
24
25//kbuild:lib-$(CONFIG_RMDIR) += rmdir.o
Eric Andersenf6be9441999-10-13 21:12:06 +000026
Manuel Novoa III cad53642003-03-19 09:13:01 +000027/* BB_AUDIT SUSv3 compliant */
28/* http://www.opengroup.org/onlinepubs/007904975/utilities/rmdir.html */
Matt Kraai13506662001-08-29 21:18:47 +000029
Pere Orga34425382011-03-31 14:43:25 +020030//usage:#define rmdir_trivial_usage
31//usage: "[OPTIONS] DIRECTORY..."
32//usage:#define rmdir_full_usage "\n\n"
33//usage: "Remove DIRECTORY if it is empty\n"
Pere Orga34425382011-03-31 14:43:25 +020034//usage: IF_FEATURE_RMDIR_LONG_OPTIONS(
35//usage: "\n -p|--parents Include parents"
36//usage: "\n --ignore-fail-on-non-empty"
37//usage: )
38//usage: IF_NOT_FEATURE_RMDIR_LONG_OPTIONS(
39//usage: "\n -p Include parents"
40//usage: )
41//usage:
42//usage:#define rmdir_example_usage
43//usage: "# rmdir /tmp/foo\n"
44
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000045#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000046
Denis Vlasenko99912ca2007-04-10 15:43:37 +000047/* This is a NOFORK applet. Be very careful! */
48
49
Simon B3698ed12012-05-06 15:03:32 +020050#define PARENTS (1 << 0)
Denys Vlasenko17f84182014-05-19 16:23:50 +020051#define VERBOSE ((1 << 1) * ENABLE_FEATURE_VERBOSE)
Simon B3698ed12012-05-06 15:03:32 +020052#define IGNORE_NON_EMPTY (1 << 2)
Denis Vlasenko52feee92008-02-24 14:56:10 +000053
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000054int rmdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000055int rmdir_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000056{
Matt Kraai9a71af52000-11-22 01:09:38 +000057 int status = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 int flags;
Manuel Novoa III cad53642003-03-19 09:13:01 +000059 char *path;
Matt Kraai9a71af52000-11-22 01:09:38 +000060
Denis Vlasenko52feee92008-02-24 14:56:10 +000061#if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
62 static const char rmdir_longopts[] ALIGN1 =
63 "parents\0" No_argument "p"
64 /* Debian etch: many packages fail to be purged or installed
65 * because they desperately want this option: */
66 "ignore-fail-on-non-empty\0" No_argument "\xff"
Denys Vlasenko17f84182014-05-19 16:23:50 +020067 IF_FEATURE_VERBOSE(
68 "verbose\0" No_argument "v"
69 )
Denis Vlasenko52feee92008-02-24 14:56:10 +000070 ;
71 applet_long_options = rmdir_longopts;
72#endif
Simon B3698ed12012-05-06 15:03:32 +020073 flags = getopt32(argv, "pv");
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 argv += optind;
Matt Kraai13506662001-08-29 21:18:47 +000075
Manuel Novoa III cad53642003-03-19 09:13:01 +000076 if (!*argv) {
77 bb_show_usage();
78 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000079
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 do {
81 path = *argv;
82
Denis Vlasenko99912ca2007-04-10 15:43:37 +000083 while (1) {
Denys Vlasenko17f84182014-05-19 16:23:50 +020084 if (flags & VERBOSE) {
85 printf("rmdir: removing directory, '%s'\n", path);
86 }
87
Manuel Novoa III cad53642003-03-19 09:13:01 +000088 if (rmdir(path) < 0) {
Denis Vlasenko52feee92008-02-24 14:56:10 +000089#if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
90 if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY)
91 break;
92#endif
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020093 bb_perror_msg("'%s'", path); /* Match gnu rmdir msg. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 status = EXIT_FAILURE;
Denis Vlasenko52feee92008-02-24 14:56:10 +000095 } else if (flags & PARENTS) {
96 /* Note: path was not "" since rmdir succeeded. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 path = dirname(path);
Denis Vlasenko52feee92008-02-24 14:56:10 +000098 /* Path is now just the parent component. Dirname
99 * returns "." if there are no parents.
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000100 */
Denis Vlasenko52feee92008-02-24 14:56:10 +0000101 if (NOT_LONE_CHAR(path, '.')) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 continue;
103 }
104 }
105 break;
Denis Vlasenko99912ca2007-04-10 15:43:37 +0000106 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000107 } while (*++argv);
Matt Kraai13506662001-08-29 21:18:47 +0000108
Matt Kraai9a71af52000-11-22 01:09:38 +0000109 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000110}