blob: 2840d1cfaa3465732cc29cb35ca7346ed48dbcc6 [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
Denis Vlasenko52feee92008-02-24 14:56:10 +000033#define PARENTS 0x01
34#define IGNORE_NON_EMPTY 0x02
35
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000036int rmdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000037int rmdir_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000038{
Matt Kraai9a71af52000-11-22 01:09:38 +000039 int status = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 int flags;
Manuel Novoa III cad53642003-03-19 09:13:01 +000041 char *path;
Matt Kraai9a71af52000-11-22 01:09:38 +000042
Denis Vlasenko52feee92008-02-24 14:56:10 +000043#if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
44 static const char rmdir_longopts[] ALIGN1 =
45 "parents\0" No_argument "p"
46 /* Debian etch: many packages fail to be purged or installed
47 * because they desperately want this option: */
48 "ignore-fail-on-non-empty\0" No_argument "\xff"
49 ;
50 applet_long_options = rmdir_longopts;
51#endif
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000052 flags = getopt32(argv, "p");
Manuel Novoa III cad53642003-03-19 09:13:01 +000053 argv += optind;
Matt Kraai13506662001-08-29 21:18:47 +000054
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 if (!*argv) {
56 bb_show_usage();
57 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000058
Manuel Novoa III cad53642003-03-19 09:13:01 +000059 do {
60 path = *argv;
61
Denis Vlasenko99912ca2007-04-10 15:43:37 +000062 while (1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000063 if (rmdir(path) < 0) {
Denis Vlasenko52feee92008-02-24 14:56:10 +000064#if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
65 if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY)
66 break;
67#endif
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020068 bb_perror_msg("'%s'", path); /* Match gnu rmdir msg. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 status = EXIT_FAILURE;
Denis Vlasenko52feee92008-02-24 14:56:10 +000070 } else if (flags & PARENTS) {
71 /* Note: path was not "" since rmdir succeeded. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 path = dirname(path);
Denis Vlasenko52feee92008-02-24 14:56:10 +000073 /* Path is now just the parent component. Dirname
74 * returns "." if there are no parents.
Denis Vlasenko89f0b342006-11-18 22:04:09 +000075 */
Denis Vlasenko52feee92008-02-24 14:56:10 +000076 if (NOT_LONE_CHAR(path, '.')) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000077 continue;
78 }
79 }
80 break;
Denis Vlasenko99912ca2007-04-10 15:43:37 +000081 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 } while (*++argv);
Matt Kraai13506662001-08-29 21:18:47 +000083
Matt Kraai9a71af52000-11-22 01:09:38 +000084 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +000085}