| 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 | #include "config.h"
|
|---|
| 47 | #include "err.h"
|
|---|
| 48 | #include <stdio.h>
|
|---|
| 49 | #include <stdlib.h>
|
|---|
| 50 | #include <string.h>
|
|---|
| 51 | #include <errno.h>
|
|---|
| 52 | #include <unistd.h>
|
|---|
| 53 | #ifdef HAVE_ALLOCA_H
|
|---|
| 54 | # include <alloca.h>
|
|---|
| 55 | #endif
|
|---|
| 56 | #include "getopt.h"
|
|---|
| 57 | #include "kmkbuiltin.h"
|
|---|
| 58 |
|
|---|
| 59 | #ifdef _MSC_VER
|
|---|
| 60 | # include "mscfakes.h"
|
|---|
| 61 | #endif
|
|---|
| 62 |
|
|---|
| 63 | static int rm_path(char *);
|
|---|
| 64 | static int usage(FILE *);
|
|---|
| 65 |
|
|---|
| 66 | static int pflag;
|
|---|
| 67 | static int vflag;
|
|---|
| 68 | static int ignore_fail_on_non_empty;
|
|---|
| 69 | static int ignore_fail_on_not_exist;
|
|---|
| 70 |
|
|---|
| 71 | static struct option long_options[] =
|
|---|
| 72 | {
|
|---|
| 73 | { "help", no_argument, 0, 262 },
|
|---|
| 74 | { "ignore-fail-on-non-empty", no_argument, 0, 260 },
|
|---|
| 75 | { "ignore-fail-on-not-exist", no_argument, 0, 261 },
|
|---|
| 76 | { "parents", no_argument, 0, 'p' },
|
|---|
| 77 | { "verbose", no_argument, 0, 'v' },
|
|---|
| 78 | { "version", no_argument, 0, 263 },
|
|---|
| 79 | { 0, 0, 0, 0 },
|
|---|
| 80 | };
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 | int
|
|---|
| 84 | kmk_builtin_rmdir(int argc, char *argv[], char **envp)
|
|---|
| 85 | {
|
|---|
| 86 | int ch, errors;
|
|---|
| 87 |
|
|---|
| 88 | /* reinitialize globals */
|
|---|
| 89 | ignore_fail_on_not_exist = ignore_fail_on_non_empty = vflag = pflag = 0;
|
|---|
| 90 |
|
|---|
| 91 | /* kmk: reset getopt and set progname */
|
|---|
| 92 | g_progname = argv[0];
|
|---|
| 93 | opterr = 1;
|
|---|
| 94 | optarg = NULL;
|
|---|
| 95 | optopt = 0;
|
|---|
| 96 | optind = 0; /* init */
|
|---|
| 97 | while ((ch = getopt_long(argc, argv, "pv", long_options, NULL)) != -1)
|
|---|
| 98 | switch(ch) {
|
|---|
| 99 | case 'p':
|
|---|
| 100 | pflag = 1;
|
|---|
| 101 | break;
|
|---|
| 102 | case 'v':
|
|---|
| 103 | vflag = 1;
|
|---|
| 104 | break;
|
|---|
| 105 | case 260:
|
|---|
| 106 | ignore_fail_on_non_empty = 1;
|
|---|
| 107 | break;
|
|---|
| 108 | case 261:
|
|---|
| 109 | ignore_fail_on_not_exist = 1;
|
|---|
| 110 | break;
|
|---|
| 111 | case 262:
|
|---|
| 112 | usage(stdout);
|
|---|
| 113 | return 0;
|
|---|
| 114 | case 263:
|
|---|
| 115 | return kbuild_version(argv[0]);
|
|---|
| 116 | case '?':
|
|---|
| 117 | default:
|
|---|
| 118 | return usage(stderr);
|
|---|
| 119 | }
|
|---|
| 120 | argc -= optind;
|
|---|
| 121 | argv += optind;
|
|---|
| 122 |
|
|---|
| 123 | if (argc == 0)
|
|---|
| 124 | return /*usage(stderr)*/0;
|
|---|
| 125 |
|
|---|
| 126 | for (errors = 0; *argv; argv++) {
|
|---|
| 127 | if (rmdir(*argv) < 0) {
|
|---|
| 128 | if ( (!ignore_fail_on_non_empty || (errno != ENOTEMPTY && errno != EPERM && errno != EACCES && errno != EINVAL && errno != EEXIST))
|
|---|
| 129 | && (!ignore_fail_on_not_exist || errno != ENOENT)) {
|
|---|
| 130 | warn("%s", *argv);
|
|---|
| 131 | errors = 1;
|
|---|
| 132 | continue;
|
|---|
| 133 | }
|
|---|
| 134 | if (!ignore_fail_on_not_exist || errno != ENOENT)
|
|---|
| 135 | continue;
|
|---|
| 136 | /* (only ignored doesn't exist errors fall thru) */
|
|---|
| 137 | } else if (vflag) {
|
|---|
| 138 | printf("%s\n", *argv);
|
|---|
| 139 | }
|
|---|
| 140 | if (pflag)
|
|---|
| 141 | errors |= rm_path(*argv);
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | return errors;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | static int
|
|---|
| 148 | rm_path(char *path)
|
|---|
| 149 | {
|
|---|
| 150 | char *p;
|
|---|
| 151 | const size_t len = strlen(path);
|
|---|
| 152 | p = alloca(len + 1);
|
|---|
| 153 | path = memcpy(p, path, len + 1);
|
|---|
| 154 |
|
|---|
| 155 | #if defined(_MSC_VER) || defined(__EMX__)
|
|---|
| 156 | p = strchr(path, '\\');
|
|---|
| 157 | while (p) {
|
|---|
| 158 | *p++ = '/';
|
|---|
| 159 | p = strchr(p, '\\');
|
|---|
| 160 | }
|
|---|
| 161 | #endif
|
|---|
| 162 |
|
|---|
| 163 | p = path + len;
|
|---|
| 164 | while (--p > path && *p == '/')
|
|---|
| 165 | ;
|
|---|
| 166 | *++p = '\0';
|
|---|
| 167 | while ((p = strrchr(path, '/')) != NULL) {
|
|---|
| 168 | /* Delete trailing slashes. */
|
|---|
| 169 | while (--p >= path && *p == '/')
|
|---|
| 170 | ;
|
|---|
| 171 | *++p = '\0';
|
|---|
| 172 | if (p == path)
|
|---|
| 173 | break;
|
|---|
| 174 | #if defined(_MSC_VER) || defined(__EMX__)
|
|---|
| 175 | if (p[-1] == ':' && p - 2 == path)
|
|---|
| 176 | break;
|
|---|
| 177 | #endif
|
|---|
| 178 |
|
|---|
| 179 | if (rmdir(path) < 0) {
|
|---|
| 180 | if (ignore_fail_on_non_empty && (errno == ENOTEMPTY || errno == EPERM || errno == EACCES || errno == EINVAL || errno == EEXIST))
|
|---|
| 181 | break;
|
|---|
| 182 | if (!ignore_fail_on_not_exist || errno != ENOENT) {
|
|---|
| 183 | warn("%s", path);
|
|---|
| 184 | return (1);
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 | if (vflag)
|
|---|
| 188 | printf("%s\n", path);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | return (0);
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | static int
|
|---|
| 195 | usage(FILE *pf)
|
|---|
| 196 | {
|
|---|
| 197 | (void)fprintf(pf, "usage: %s [-pv --ignore-fail-on-non-empty --ignore-fail-on-not-exist] directory ...\n"
|
|---|
| 198 | " or: %s --help\n"
|
|---|
| 199 | " or: %s --version\n",
|
|---|
| 200 | g_progname, g_progname, g_progname);
|
|---|
| 201 | return 1;
|
|---|
| 202 | }
|
|---|