VirtualBox

source: kBuild/trunk/src/gmake/kmkbuiltin/rmdir.c@ 809

Last change on this file since 809 was 809, checked in by bird, 18 years ago

Solaris + cleanup.

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