blob: 05ea0e9ffe7c6b2104902e0d5ed501e09e164e88 [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 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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
Manuel Novoa III cad53642003-03-19 09:13:01 +000013#include <stdlib.h>
14#include <unistd.h>
15#include <libgen.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000016#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000017
Rob Landleydfba7412006-03-06 20:47:33 +000018int rmdir_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000019{
Matt Kraai9a71af52000-11-22 01:09:38 +000020 int status = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +000021 int flags;
22 int do_dot;
23 char *path;
Matt Kraai9a71af52000-11-22 01:09:38 +000024
Denis Vlasenko67b23e62006-10-03 21:00:06 +000025 flags = getopt32(argc, argv, "p");
Matt Kraai13506662001-08-29 21:18:47 +000026
Manuel Novoa III cad53642003-03-19 09:13:01 +000027 argv += optind;
Matt Kraai13506662001-08-29 21:18:47 +000028
Manuel Novoa III cad53642003-03-19 09:13:01 +000029 if (!*argv) {
30 bb_show_usage();
31 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000032
Manuel Novoa III cad53642003-03-19 09:13:01 +000033 do {
34 path = *argv;
35
36 /* Record if the first char was a '.' so we can use dirname later. */
37 do_dot = (*path == '.');
38
39 do {
40 if (rmdir(path) < 0) {
41 bb_perror_msg("`%s'", path); /* Match gnu rmdir msg. */
42 status = EXIT_FAILURE;
43 } else if (flags) {
44 /* Note: path was not empty or null since rmdir succeeded. */
45 path = dirname(path);
46 /* Path is now just the parent component. Note that dirname
47 * returns "." if there are no parents. We must distinguish
48 * this from the case of the original path starting with '.'.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000049 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000050 if (do_dot || (*path != '.') || path[1]) {
51 continue;
52 }
53 }
54 break;
55 } while (1);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000056
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 } while (*++argv);
Matt Kraai13506662001-08-29 21:18:47 +000058
Matt Kraai9a71af52000-11-22 01:09:38 +000059 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +000060}