VirtualBox

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

Last change on this file since 2113 was 2113, checked in by bird, 16 years ago

kmkbuiltin: include config.h

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

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