VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/rmdir.c@ 3213

Last change on this file since 3213 was 3192, checked in by bird, 7 years ago

kmkbuiltin: funnel output thru output.c (usually via err.c).

  • Property svn:eol-style set to native
File size: 7.8 KB
Line 
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
32static 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
38static 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#include "config.h"
51#include "err.h"
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <errno.h>
56#include <unistd.h>
57#ifdef HAVE_ALLOCA_H
58# include <alloca.h>
59#endif
60#include "getopt.h"
61#include "kmkbuiltin.h"
62
63#ifdef _MSC_VER
64# include "mscfakes.h"
65#endif
66
67
68/*********************************************************************************************************************************
69* Structures and Typedefs *
70*********************************************************************************************************************************/
71typedef struct RMDIRINSTANCE
72{
73 PKMKBUILTINCTX pCtx;
74 int pflag;
75 int vflag;
76 int ignore_fail_on_non_empty;
77 int ignore_fail_on_not_exist;
78} RMDIRINSTANCE;
79typedef RMDIRINSTANCE *PRMDIRINSTANCE;
80
81
82/*********************************************************************************************************************************
83* Global Variables *
84*********************************************************************************************************************************/
85static struct option long_options[] =
86{
87 { "help", no_argument, 0, 262 },
88 { "ignore-fail-on-non-empty", no_argument, 0, 260 },
89 { "ignore-fail-on-not-exist", no_argument, 0, 261 },
90 { "parents", no_argument, 0, 'p' },
91 { "verbose", no_argument, 0, 'v' },
92 { "version", no_argument, 0, 263 },
93 { 0, 0, 0, 0 },
94};
95
96
97
98/*********************************************************************************************************************************
99* Internal Functions *
100*********************************************************************************************************************************/
101#if !defined(KMK_BUILTIN_STANDALONE) && defined(KBUILD_OS_WINDOWS)
102extern int dir_cache_deleted_directory(const char *pszDir);
103#endif
104static int rm_path(PRMDIRINSTANCE, char *);
105static int usage(PKMKBUILTINCTX, int);
106
107
108int
109kmk_builtin_rmdir(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
110{
111 RMDIRINSTANCE This;
112 int ch, errors;
113
114 /* Initialize global instance. */
115 This.pCtx = pCtx;
116 This.ignore_fail_on_not_exist = 0;
117 This.ignore_fail_on_non_empty = 0;
118 This.vflag = 0;
119 This.pflag = 0;
120
121 /* kmk: reset getopt and set progname */
122 opterr = 1;
123 optarg = NULL;
124 optopt = 0;
125 optind = 0; /* init */
126 while ((ch = getopt_long(argc, argv, "pv", long_options, NULL)) != -1)
127 switch(ch) {
128 case 'p':
129 This.pflag = 1;
130 break;
131 case 'v':
132 This.vflag = 1;
133 break;
134 case 260:
135 This.ignore_fail_on_non_empty = 1;
136 break;
137 case 261:
138 This.ignore_fail_on_not_exist = 1;
139 break;
140 case 262:
141 usage(pCtx, 0);
142 return 0;
143 case 263:
144 return kbuild_version(argv[0]);
145 case '?':
146 default:
147 return usage(pCtx, 1);
148 }
149 argc -= optind;
150 argv += optind;
151
152 if (argc == 0)
153 return /*usage(stderr)*/0;
154
155 for (errors = 0; *argv; argv++) {
156 if (rmdir(*argv) < 0) {
157 if ( ( !This.ignore_fail_on_non_empty
158 || (errno != ENOTEMPTY && errno != EPERM && errno != EACCES && errno != EINVAL && errno != EEXIST))
159 && ( !This.ignore_fail_on_not_exist
160 || errno != ENOENT)) {
161 warn(pCtx, "rmdir: %s", *argv);
162 errors = 1;
163 continue;
164 }
165 if (!This.ignore_fail_on_not_exist || errno != ENOENT)
166 continue;
167 /* (only ignored doesn't exist errors fall thru) */
168 } else {
169#if !defined(KMK_BUILTIN_STANDALONE) && defined(KBUILD_OS_WINDOWS)
170 dir_cache_deleted_directory(*argv);
171#endif
172 if (This.vflag)
173 kmk_builtin_ctx_printf(pCtx, 0, "%s\n", *argv);
174 }
175 if (This.pflag)
176 errors |= rm_path(&This, *argv);
177 }
178
179 return errors;
180}
181
182#ifdef KMK_BUILTIN_STANDALONE
183int main(int argc, char **argv, char **envp)
184{
185 KMKBUILTINCTX Ctx = { "kmk_rmdir", NULL };
186 return kmk_builtin_rmdir(argc, argv, envp, &Ctx);
187}
188#endif
189
190static int
191rm_path(PRMDIRINSTANCE pThis, char *path)
192{
193 char *p;
194 const size_t len = strlen(path);
195 p = alloca(len + 1);
196 path = memcpy(p, path, len + 1);
197
198#if defined(_MSC_VER) || defined(__EMX__)
199 p = strchr(path, '\\');
200 while (p) {
201 *p++ = '/';
202 p = strchr(p, '\\');
203 }
204#endif
205
206 p = path + len;
207 while (--p > path && *p == '/')
208 ;
209 *++p = '\0';
210 while ((p = strrchr(path, '/')) != NULL) {
211 /* Delete trailing slashes. */
212 while (--p >= path && *p == '/')
213 ;
214 *++p = '\0';
215 if (p == path)
216 break;
217#if defined(_MSC_VER) || defined(__EMX__)
218 if (p[-1] == ':' && p - 2 == path)
219 break;
220#endif
221
222 if (rmdir(path) < 0) {
223 if ( pThis->ignore_fail_on_non_empty
224 && ( errno == ENOTEMPTY || errno == EPERM || errno == EACCES || errno == EINVAL || errno == EEXIST))
225 break;
226 if (!pThis->ignore_fail_on_not_exist || errno != ENOENT) {
227 warn(pThis->pCtx, "rmdir: %s", path);
228 return (1);
229 }
230 }
231#if defined(KMK) && defined(KBUILD_OS_WINDOWS)
232 else {
233 dir_cache_deleted_directory(path);
234 }
235#endif
236 if (pThis->vflag)
237 kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s\n", path);
238 }
239
240 return (0);
241}
242
243static int
244usage(PKMKBUILTINCTX pCtx, int fIsErr)
245{
246 kmk_builtin_ctx_printf(pCtx, fIsErr,
247 "usage: %s [-pv --ignore-fail-on-non-empty --ignore-fail-on-not-exist] directory ...\n"
248 " or: %s --help\n"
249 " or: %s --version\n",
250 pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName);
251 return 1;
252}
253
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette