| 1 | /*-
|
|---|
| 2 | * Copyright (c) 1992, 1993, 1994
|
|---|
| 3 | * The Regents of the University of California. All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | * 1. Redistributions of source code must retain the above copyright
|
|---|
| 9 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 10 | * 2. Redistributions in binary form must reproduce the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 12 | * documentation and/or other materials provided with the distribution.
|
|---|
| 13 | * 4. Neither the name of the University nor the names of its contributors
|
|---|
| 14 | * may be used to endorse or promote products derived from this software
|
|---|
| 15 | * without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|---|
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|---|
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|---|
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|---|
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|---|
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|---|
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|---|
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|---|
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|---|
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|---|
| 27 | * SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | #if 0
|
|---|
| 31 | #ifndef lint
|
|---|
| 32 | static char const copyright[] =
|
|---|
| 33 | "@(#) Copyright (c) 1992, 1993, 1994\n\
|
|---|
| 34 | The Regents of the University of California. All rights reserved.\n";
|
|---|
| 35 | #endif /* not lint */
|
|---|
| 36 |
|
|---|
| 37 | #ifndef lint
|
|---|
| 38 | static char sccsid[] = "@(#)rmdir.c 8.3 (Berkeley) 4/2/94";
|
|---|
| 39 | #endif /* not lint */
|
|---|
| 40 | #endif
|
|---|
| 41 | #if 0
|
|---|
| 42 | #include <sys/cdefs.h>
|
|---|
| 43 | __FBSDID("$FreeBSD: src/bin/rmdir/rmdir.c,v 1.20 2005/01/26 06:51:28 ssouhlal Exp $");
|
|---|
| 44 | #endif
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 | /*********************************************************************************************************************************
|
|---|
| 48 | * Header Files *
|
|---|
| 49 | *********************************************************************************************************************************/
|
|---|
| 50 | #define FAKES_NO_GETOPT_H /* bird */
|
|---|
| 51 | #include "config.h"
|
|---|
| 52 | #include "err.h"
|
|---|
| 53 | #include <stdio.h>
|
|---|
| 54 | #include <stdlib.h>
|
|---|
| 55 | #include <string.h>
|
|---|
| 56 | #include <errno.h>
|
|---|
| 57 | #include <unistd.h>
|
|---|
| 58 | #ifdef HAVE_ALLOCA_H
|
|---|
| 59 | # include <alloca.h>
|
|---|
| 60 | #endif
|
|---|
| 61 | #include "getopt_r.h"
|
|---|
| 62 | #include "kmkbuiltin.h"
|
|---|
| 63 |
|
|---|
| 64 | #ifdef _MSC_VER
|
|---|
| 65 | # include "mscfakes.h"
|
|---|
| 66 | #endif
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 | /*********************************************************************************************************************************
|
|---|
| 70 | * Structures and Typedefs *
|
|---|
| 71 | *********************************************************************************************************************************/
|
|---|
| 72 | typedef struct RMDIRINSTANCE
|
|---|
| 73 | {
|
|---|
| 74 | PKMKBUILTINCTX pCtx;
|
|---|
| 75 | int pflag;
|
|---|
| 76 | int vflag;
|
|---|
| 77 | int ignore_fail_on_non_empty;
|
|---|
| 78 | int ignore_fail_on_not_exist;
|
|---|
| 79 | } RMDIRINSTANCE;
|
|---|
| 80 | typedef RMDIRINSTANCE *PRMDIRINSTANCE;
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 | /*********************************************************************************************************************************
|
|---|
| 84 | * Global Variables *
|
|---|
| 85 | *********************************************************************************************************************************/
|
|---|
| 86 | static struct option long_options[] =
|
|---|
| 87 | {
|
|---|
| 88 | { "help", no_argument, 0, 262 },
|
|---|
| 89 | { "ignore-fail-on-non-empty", no_argument, 0, 260 },
|
|---|
| 90 | { "ignore-fail-on-not-exist", no_argument, 0, 261 },
|
|---|
| 91 | { "parents", no_argument, 0, 'p' },
|
|---|
| 92 | { "verbose", no_argument, 0, 'v' },
|
|---|
| 93 | { "version", no_argument, 0, 263 },
|
|---|
| 94 | { 0, 0, 0, 0 },
|
|---|
| 95 | };
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 | /*********************************************************************************************************************************
|
|---|
| 100 | * Internal Functions *
|
|---|
| 101 | *********************************************************************************************************************************/
|
|---|
| 102 | #if !defined(KMK_BUILTIN_STANDALONE) && defined(KBUILD_OS_WINDOWS)
|
|---|
| 103 | extern int dir_cache_deleted_directory(const char *pszDir);
|
|---|
| 104 | #endif
|
|---|
| 105 | static int rm_path(PRMDIRINSTANCE, char *);
|
|---|
| 106 | static int usage(PKMKBUILTINCTX, int);
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 | int
|
|---|
| 110 | kmk_builtin_rmdir(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
|
|---|
| 111 | {
|
|---|
| 112 | RMDIRINSTANCE This;
|
|---|
| 113 | struct getopt_state_r gos;
|
|---|
| 114 | int ch, errors;
|
|---|
| 115 |
|
|---|
| 116 | /* Initialize global instance. */
|
|---|
| 117 | This.pCtx = pCtx;
|
|---|
| 118 | This.ignore_fail_on_not_exist = 0;
|
|---|
| 119 | This.ignore_fail_on_non_empty = 0;
|
|---|
| 120 | This.vflag = 0;
|
|---|
| 121 | This.pflag = 0;
|
|---|
| 122 |
|
|---|
| 123 | getopt_initialize_r(&gos, argc, argv, "pv", long_options, envp, pCtx);
|
|---|
| 124 | while ((ch = getopt_long_r(&gos, NULL)) != -1)
|
|---|
| 125 | switch(ch) {
|
|---|
| 126 | case 'p':
|
|---|
| 127 | This.pflag = 1;
|
|---|
| 128 | break;
|
|---|
| 129 | case 'v':
|
|---|
| 130 | This.vflag = 1;
|
|---|
| 131 | break;
|
|---|
| 132 | case 260:
|
|---|
| 133 | This.ignore_fail_on_non_empty = 1;
|
|---|
| 134 | break;
|
|---|
| 135 | case 261:
|
|---|
| 136 | This.ignore_fail_on_not_exist = 1;
|
|---|
| 137 | break;
|
|---|
| 138 | case 262:
|
|---|
| 139 | usage(pCtx, 0);
|
|---|
| 140 | return 0;
|
|---|
| 141 | case 263:
|
|---|
| 142 | return kbuild_version(argv[0]);
|
|---|
| 143 | case '?':
|
|---|
| 144 | default:
|
|---|
| 145 | return usage(pCtx, 1);
|
|---|
| 146 | }
|
|---|
| 147 | argc -= gos.optind;
|
|---|
| 148 | argv += gos.optind;
|
|---|
| 149 |
|
|---|
| 150 | if (argc == 0)
|
|---|
| 151 | return /*usage(stderr)*/0;
|
|---|
| 152 |
|
|---|
| 153 | for (errors = 0; *argv; argv++) {
|
|---|
| 154 | if (rmdir(*argv) < 0) {
|
|---|
| 155 | if ( ( !This.ignore_fail_on_non_empty
|
|---|
| 156 | || (errno != ENOTEMPTY && errno != EPERM && errno != EACCES && errno != EINVAL && errno != EEXIST))
|
|---|
| 157 | && ( !This.ignore_fail_on_not_exist
|
|---|
| 158 | || errno != ENOENT)) {
|
|---|
| 159 | warn(pCtx, "rmdir: %s", *argv);
|
|---|
| 160 | errors = 1;
|
|---|
| 161 | continue;
|
|---|
| 162 | }
|
|---|
| 163 | if (!This.ignore_fail_on_not_exist || errno != ENOENT)
|
|---|
| 164 | continue;
|
|---|
| 165 | /* (only ignored doesn't exist errors fall thru) */
|
|---|
| 166 | } else {
|
|---|
| 167 | #if !defined(KMK_BUILTIN_STANDALONE) && defined(KBUILD_OS_WINDOWS)
|
|---|
| 168 | dir_cache_deleted_directory(*argv);
|
|---|
| 169 | #endif
|
|---|
| 170 | if (This.vflag)
|
|---|
| 171 | kmk_builtin_ctx_printf(pCtx, 0, "%s\n", *argv);
|
|---|
| 172 | }
|
|---|
| 173 | if (This.pflag)
|
|---|
| 174 | errors |= rm_path(&This, *argv);
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | return errors;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | #ifdef KMK_BUILTIN_STANDALONE
|
|---|
| 181 | int main(int argc, char **argv, char **envp)
|
|---|
| 182 | {
|
|---|
| 183 | KMKBUILTINCTX Ctx = { "kmk_rmdir", NULL };
|
|---|
| 184 | return kmk_builtin_rmdir(argc, argv, envp, &Ctx);
|
|---|
| 185 | }
|
|---|
| 186 | #endif
|
|---|
| 187 |
|
|---|
| 188 | static int
|
|---|
| 189 | rm_path(PRMDIRINSTANCE pThis, char *path)
|
|---|
| 190 | {
|
|---|
| 191 | char *p;
|
|---|
| 192 | const size_t len = strlen(path);
|
|---|
| 193 | p = alloca(len + 1);
|
|---|
| 194 | path = memcpy(p, path, len + 1);
|
|---|
| 195 |
|
|---|
| 196 | #if defined(_MSC_VER) || defined(__EMX__)
|
|---|
| 197 | p = strchr(path, '\\');
|
|---|
| 198 | while (p) {
|
|---|
| 199 | *p++ = '/';
|
|---|
| 200 | p = strchr(p, '\\');
|
|---|
| 201 | }
|
|---|
| 202 | #endif
|
|---|
| 203 |
|
|---|
| 204 | p = path + len;
|
|---|
| 205 | while (--p > path && *p == '/')
|
|---|
| 206 | ;
|
|---|
| 207 | *++p = '\0';
|
|---|
| 208 | while ((p = strrchr(path, '/')) != NULL) {
|
|---|
| 209 | /* Delete trailing slashes. */
|
|---|
| 210 | while (--p >= path && *p == '/')
|
|---|
| 211 | ;
|
|---|
| 212 | *++p = '\0';
|
|---|
| 213 | if (p == path)
|
|---|
| 214 | break;
|
|---|
| 215 | #if defined(_MSC_VER) || defined(__EMX__)
|
|---|
| 216 | if (p[-1] == ':' && p - 2 == path)
|
|---|
| 217 | break;
|
|---|
| 218 | #endif
|
|---|
| 219 |
|
|---|
| 220 | if (rmdir(path) < 0) {
|
|---|
| 221 | if ( pThis->ignore_fail_on_non_empty
|
|---|
| 222 | && ( errno == ENOTEMPTY || errno == EPERM || errno == EACCES || errno == EINVAL || errno == EEXIST))
|
|---|
| 223 | break;
|
|---|
| 224 | if (!pThis->ignore_fail_on_not_exist || errno != ENOENT) {
|
|---|
| 225 | warn(pThis->pCtx, "rmdir: %s", path);
|
|---|
| 226 | return (1);
|
|---|
| 227 | }
|
|---|
| 228 | }
|
|---|
| 229 | #if defined(KMK) && defined(KBUILD_OS_WINDOWS)
|
|---|
| 230 | else {
|
|---|
| 231 | dir_cache_deleted_directory(path);
|
|---|
| 232 | }
|
|---|
| 233 | #endif
|
|---|
| 234 | if (pThis->vflag)
|
|---|
| 235 | kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s\n", path);
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | return (0);
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | static int
|
|---|
| 242 | usage(PKMKBUILTINCTX pCtx, int fIsErr)
|
|---|
| 243 | {
|
|---|
| 244 | kmk_builtin_ctx_printf(pCtx, fIsErr,
|
|---|
| 245 | "usage: %s [-pv --ignore-fail-on-non-empty --ignore-fail-on-not-exist] directory ...\n"
|
|---|
| 246 | " or: %s --help\n"
|
|---|
| 247 | " or: %s --version\n",
|
|---|
| 248 | pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName);
|
|---|
| 249 | return 1;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|