blob: cc2dea010ce5bfa3eed40ff16ab1fb85a896c019 [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 */
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
Pere Orga34425382011-03-31 14:43:25 +020013//usage:#define rmdir_trivial_usage
14//usage: "[OPTIONS] DIRECTORY..."
15//usage:#define rmdir_full_usage "\n\n"
16//usage: "Remove DIRECTORY if it is empty\n"
Pere Orga34425382011-03-31 14:43:25 +020017//usage: IF_FEATURE_RMDIR_LONG_OPTIONS(
18//usage: "\n -p|--parents Include parents"
19//usage: "\n --ignore-fail-on-non-empty"
20//usage: )
21//usage: IF_NOT_FEATURE_RMDIR_LONG_OPTIONS(
22//usage: "\n -p Include parents"
23//usage: )
24//usage:
25//usage:#define rmdir_example_usage
26//usage: "# rmdir /tmp/foo\n"
27
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000028#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000029
Denis Vlasenko99912ca2007-04-10 15:43:37 +000030/* This is a NOFORK applet. Be very careful! */
31
32
Simon B3698ed12012-05-06 15:03:32 +020033#define PARENTS (1 << 0)
34//efine VERBOSE (1 << 1) //accepted but ignored
35#define IGNORE_NON_EMPTY (1 << 2)
Denis Vlasenko52feee92008-02-24 14:56:10 +000036
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000037int rmdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000038int rmdir_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000039{
Matt Kraai9a71af52000-11-22 01:09:38 +000040 int status = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +000041 int flags;
Manuel Novoa III cad53642003-03-19 09:13:01 +000042 char *path;
Matt Kraai9a71af52000-11-22 01:09:38 +000043
Denis Vlasenko52feee92008-02-24 14:56:10 +000044#if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
45 static const char rmdir_longopts[] ALIGN1 =
46 "parents\0" No_argument "p"
Simon B3698ed12012-05-06 15:03:32 +020047 "verbose\0" No_argument "v"
Denis Vlasenko52feee92008-02-24 14:56:10 +000048 /* Debian etch: many packages fail to be purged or installed
49 * because they desperately want this option: */
50 "ignore-fail-on-non-empty\0" No_argument "\xff"
51 ;
52 applet_long_options = rmdir_longopts;
53#endif
Simon B3698ed12012-05-06 15:03:32 +020054 flags = getopt32(argv, "pv");
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 argv += optind;
Matt Kraai13506662001-08-29 21:18:47 +000056
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 if (!*argv) {
58 bb_show_usage();
59 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000060
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 do {
62 path = *argv;
63
Denis Vlasenko99912ca2007-04-10 15:43:37 +000064 while (1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000065 if (rmdir(path) < 0) {
Denis Vlasenko52feee92008-02-24 14:56:10 +000066#if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
67 if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY)
68 break;
69#endif
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020070 bb_perror_msg("'%s'", path); /* Match gnu rmdir msg. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 status = EXIT_FAILURE;
Denis Vlasenko52feee92008-02-24 14:56:10 +000072 } else if (flags & PARENTS) {
73 /* Note: path was not "" since rmdir succeeded. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 path = dirname(path);
Denis Vlasenko52feee92008-02-24 14:56:10 +000075 /* Path is now just the parent component. Dirname
76 * returns "." if there are no parents.
Denis Vlasenko89f0b342006-11-18 22:04:09 +000077 */
Denis Vlasenko52feee92008-02-24 14:56:10 +000078 if (NOT_LONE_CHAR(path, '.')) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 continue;
80 }
81 }
82 break;
Denis Vlasenko99912ca2007-04-10 15:43:37 +000083 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000084 } while (*++argv);
Matt Kraai13506662001-08-29 21:18:47 +000085
Matt Kraai9a71af52000-11-22 01:09:38 +000086 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +000087}