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