[813] | 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 |
|
---|
[3192] | 46 |
|
---|
| 47 | /*********************************************************************************************************************************
|
---|
| 48 | * Header Files *
|
---|
| 49 | *********************************************************************************************************************************/
|
---|
[3216] | 50 | #define FAKES_NO_GETOPT_H /* bird */
|
---|
[2113] | 51 | #include "config.h"
|
---|
[813] | 52 | #include "err.h"
|
---|
| 53 | #include <stdio.h>
|
---|
| 54 | #include <stdlib.h>
|
---|
| 55 | #include <string.h>
|
---|
| 56 | #include <errno.h>
|
---|
| 57 | #include <unistd.h>
|
---|
[2121] | 58 | #ifdef HAVE_ALLOCA_H
|
---|
| 59 | # include <alloca.h>
|
---|
| 60 | #endif
|
---|
[3216] | 61 | #include "getopt_r.h"
|
---|
[1183] | 62 | #include "kmkbuiltin.h"
|
---|
[813] | 63 |
|
---|
| 64 | #ifdef _MSC_VER
|
---|
| 65 | # include "mscfakes.h"
|
---|
| 66 | #endif
|
---|
| 67 |
|
---|
| 68 |
|
---|
[3192] | 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;
|
---|
[813] | 81 |
|
---|
[3192] | 82 |
|
---|
| 83 | /*********************************************************************************************************************************
|
---|
| 84 | * Global Variables *
|
---|
| 85 | *********************************************************************************************************************************/
|
---|
[813] | 86 | static struct option long_options[] =
|
---|
| 87 | {
|
---|
[1183] | 88 | { "help", no_argument, 0, 262 },
|
---|
[813] | 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' },
|
---|
[1183] | 93 | { "version", no_argument, 0, 263 },
|
---|
[813] | 94 | { 0, 0, 0, 0 },
|
---|
| 95 | };
|
---|
| 96 |
|
---|
| 97 |
|
---|
[3192] | 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 |
|
---|
[813] | 109 | int
|
---|
[3192] | 110 | kmk_builtin_rmdir(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
|
---|
[813] | 111 | {
|
---|
[3192] | 112 | RMDIRINSTANCE This;
|
---|
[3216] | 113 | struct getopt_state_r gos;
|
---|
[813] | 114 | int ch, errors;
|
---|
| 115 |
|
---|
[3192] | 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;
|
---|
[813] | 122 |
|
---|
[3216] | 123 | getopt_initialize_r(&gos, argc, argv, "pv", long_options, envp, pCtx);
|
---|
| 124 | while ((ch = getopt_long_r(&gos, NULL)) != -1)
|
---|
[813] | 125 | switch(ch) {
|
---|
| 126 | case 'p':
|
---|
[3192] | 127 | This.pflag = 1;
|
---|
[813] | 128 | break;
|
---|
| 129 | case 'v':
|
---|
[3192] | 130 | This.vflag = 1;
|
---|
[813] | 131 | break;
|
---|
| 132 | case 260:
|
---|
[3192] | 133 | This.ignore_fail_on_non_empty = 1;
|
---|
[813] | 134 | break;
|
---|
| 135 | case 261:
|
---|
[3192] | 136 | This.ignore_fail_on_not_exist = 1;
|
---|
[813] | 137 | break;
|
---|
[1183] | 138 | case 262:
|
---|
[3192] | 139 | usage(pCtx, 0);
|
---|
[1183] | 140 | return 0;
|
---|
[1190] | 141 | case 263:
|
---|
| 142 | return kbuild_version(argv[0]);
|
---|
[813] | 143 | case '?':
|
---|
| 144 | default:
|
---|
[3192] | 145 | return usage(pCtx, 1);
|
---|
[813] | 146 | }
|
---|
[3216] | 147 | argc -= gos.optind;
|
---|
| 148 | argv += gos.optind;
|
---|
[813] | 149 |
|
---|
| 150 | if (argc == 0)
|
---|
[1183] | 151 | return /*usage(stderr)*/0;
|
---|
[813] | 152 |
|
---|
| 153 | for (errors = 0; *argv; argv++) {
|
---|
| 154 | if (rmdir(*argv) < 0) {
|
---|
[3192] | 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);
|
---|
[813] | 160 | errors = 1;
|
---|
[982] | 161 | continue;
|
---|
[813] | 162 | }
|
---|
[3192] | 163 | if (!This.ignore_fail_on_not_exist || errno != ENOENT)
|
---|
[982] | 164 | continue;
|
---|
| 165 | /* (only ignored doesn't exist errors fall thru) */
|
---|
[2912] | 166 | } else {
|
---|
[3192] | 167 | #if !defined(KMK_BUILTIN_STANDALONE) && defined(KBUILD_OS_WINDOWS)
|
---|
[2912] | 168 | dir_cache_deleted_directory(*argv);
|
---|
| 169 | #endif
|
---|
[3192] | 170 | if (This.vflag)
|
---|
| 171 | kmk_builtin_ctx_printf(pCtx, 0, "%s\n", *argv);
|
---|
[813] | 172 | }
|
---|
[3192] | 173 | if (This.pflag)
|
---|
| 174 | errors |= rm_path(&This, *argv);
|
---|
[813] | 175 | }
|
---|
| 176 |
|
---|
| 177 | return errors;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[3192] | 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 |
|
---|
[813] | 188 | static int
|
---|
[3192] | 189 | rm_path(PRMDIRINSTANCE pThis, char *path)
|
---|
[813] | 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) {
|
---|
[3192] | 221 | if ( pThis->ignore_fail_on_non_empty
|
---|
[2912] | 222 | && ( errno == ENOTEMPTY || errno == EPERM || errno == EACCES || errno == EINVAL || errno == EEXIST))
|
---|
[813] | 223 | break;
|
---|
[3192] | 224 | if (!pThis->ignore_fail_on_not_exist || errno != ENOENT) {
|
---|
| 225 | warn(pThis->pCtx, "rmdir: %s", path);
|
---|
[813] | 226 | return (1);
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
[2912] | 229 | #if defined(KMK) && defined(KBUILD_OS_WINDOWS)
|
---|
| 230 | else {
|
---|
| 231 | dir_cache_deleted_directory(path);
|
---|
| 232 | }
|
---|
| 233 | #endif
|
---|
[3192] | 234 | if (pThis->vflag)
|
---|
| 235 | kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s\n", path);
|
---|
[813] | 236 | }
|
---|
| 237 |
|
---|
| 238 | return (0);
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | static int
|
---|
[3192] | 242 | usage(PKMKBUILTINCTX pCtx, int fIsErr)
|
---|
[813] | 243 | {
|
---|
[3192] | 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);
|
---|
[813] | 249 | return 1;
|
---|
| 250 | }
|
---|
[3192] | 251 |
|
---|