blob: 0792a1c8e2487ba0410e3fd3084abd309d9673da [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)
Denys Vlasenko17f84182014-05-19 16:23:50 +020034#define VERBOSE ((1 << 1) * ENABLE_FEATURE_VERBOSE)
Simon B3698ed12012-05-06 15:03:32 +020035#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"
47 /* Debian etch: many packages fail to be purged or installed
48 * because they desperately want this option: */
49 "ignore-fail-on-non-empty\0" No_argument "\xff"
Denys Vlasenko17f84182014-05-19 16:23:50 +020050 IF_FEATURE_VERBOSE(
51 "verbose\0" No_argument "v"
52 )
Denis Vlasenko52feee92008-02-24 14:56:10 +000053 ;
54 applet_long_options = rmdir_longopts;
55#endif
Simon B3698ed12012-05-06 15:03:32 +020056 flags = getopt32(argv, "pv");
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 argv += optind;
Matt Kraai13506662001-08-29 21:18:47 +000058
Manuel Novoa III cad53642003-03-19 09:13:01 +000059 if (!*argv) {
60 bb_show_usage();
61 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000062
Manuel Novoa III cad53642003-03-19 09:13:01 +000063 do {
64 path = *argv;
65
Denis Vlasenko99912ca2007-04-10 15:43:37 +000066 while (1) {
Denys Vlasenko17f84182014-05-19 16:23:50 +020067 if (flags & VERBOSE) {
68 printf("rmdir: removing directory, '%s'\n", path);
69 }
70
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 if (rmdir(path) < 0) {
Denis Vlasenko52feee92008-02-24 14:56:10 +000072#if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
73 if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY)
74 break;
75#endif
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020076 bb_perror_msg("'%s'", path); /* Match gnu rmdir msg. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000077 status = EXIT_FAILURE;
Denis Vlasenko52feee92008-02-24 14:56:10 +000078 } else if (flags & PARENTS) {
79 /* Note: path was not "" since rmdir succeeded. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 path = dirname(path);
Denis Vlasenko52feee92008-02-24 14:56:10 +000081 /* Path is now just the parent component. Dirname
82 * returns "." if there are no parents.
Denis Vlasenko89f0b342006-11-18 22:04:09 +000083 */
Denis Vlasenko52feee92008-02-24 14:56:10 +000084 if (NOT_LONE_CHAR(path, '.')) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000085 continue;
86 }
87 }
88 break;
Denis Vlasenko99912ca2007-04-10 15:43:37 +000089 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 } while (*++argv);
Matt Kraai13506662001-08-29 21:18:47 +000091
Matt Kraai9a71af52000-11-22 01:09:38 +000092 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +000093}