blob: 8cbd6f1fa2a1706527c167cbec6bbc8c804f58ac [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
Denis Vlasenko06af2162007-02-03 17:28:39 +000018int rmdir_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000019int rmdir_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000020{
Matt Kraai9a71af52000-11-22 01:09:38 +000021 int status = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +000022 int flags;
23 int do_dot;
24 char *path;
Matt Kraai9a71af52000-11-22 01:09:38 +000025
Denis Vlasenko67b23e62006-10-03 21:00:06 +000026 flags = getopt32(argc, argv, "p");
Matt Kraai13506662001-08-29 21:18:47 +000027
Manuel Novoa III cad53642003-03-19 09:13:01 +000028 argv += optind;
Matt Kraai13506662001-08-29 21:18:47 +000029
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 if (!*argv) {
31 bb_show_usage();
32 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000033
Manuel Novoa III cad53642003-03-19 09:13:01 +000034 do {
35 path = *argv;
36
37 /* Record if the first char was a '.' so we can use dirname later. */
38 do_dot = (*path == '.');
39
40 do {
41 if (rmdir(path) < 0) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +000042 bb_perror_msg("'%s'", path); /* Match gnu rmdir msg. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000043 status = EXIT_FAILURE;
44 } else if (flags) {
45 /* Note: path was not empty or null since rmdir succeeded. */
46 path = dirname(path);
47 /* Path is now just the parent component. Note that dirname
48 * returns "." if there are no parents. We must distinguish
49 * this from the case of the original path starting with '.'.
Denis Vlasenko89f0b342006-11-18 22:04:09 +000050 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000051 if (do_dot || (*path != '.') || path[1]) {
52 continue;
53 }
54 }
55 break;
56 } while (1);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000057
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 } while (*++argv);
Matt Kraai13506662001-08-29 21:18:47 +000059
Matt Kraai9a71af52000-11-22 01:09:38 +000060 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +000061}