source: trunk/src/gmake/kmkbuiltin/rmdir.c@ 684

Last change on this file since 684 was 684, checked in by bird, 19 years ago

Added --ignore-fail-on-non-empty similar as described in the GNU coreutils manpage.

File size: 4.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 "err.h"
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#include <errno.h>
51#ifndef _MSC_VER
52#include <unistd.h>
53#else
54#include <malloc.h>
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;
64
65static struct option long_options[] =
66{
67 { "ignore-fail-on-non-empty", no_argument, 0, 260 },
68 { "parents", no_argument, 0, 'p' },
69 { "verbose", no_argument, 0, 'v' },
70 { 0, 0, 0, 0 },
71};
72
73
74int
75kmk_builtin_rmdir(int argc, char *argv[])
76{
77 int ch, errors;
78
79 /* reinitialize globals */
80 ignore_fail_on_non_empty = vflag = pflag = 0;
81
82 /* kmk: reset getopt and set progname */
83 g_progname = argv[0];
84 opterr = 1;
85 optarg = NULL;
86 optopt = 0;
87#if defined(__FreeBSD__) || defined(__EMX__) || defined(__APPLE__)
88 optreset = 1;
89 optind = 1;
90#else
91 optind = 0; /* init */
92#endif
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 '?':
105 default:
106 return usage();
107 }
108 argc -= optind;
109 argv += optind;
110
111 if (argc == 0)
112 return usage();
113
114 for (errors = 0; *argv; argv++) {
115 if (rmdir(*argv) < 0) {
116 if (!ignore_fail_on_non_empty || errno != ENOTEMPTY) {
117 warn("%s", *argv);
118 errors = 1;
119 }
120 } else {
121 if (vflag)
122 printf("%s\n", *argv);
123 if (pflag)
124 errors |= rm_path(*argv);
125 }
126 }
127
128 return errors;
129}
130
131static int
132rm_path(char *path)
133{
134 char *p;
135 const size_t len = strlen(path);
136 p = alloca(len + 1);
137 path = memcpy(p, path, len + 1);
138
139#if defined(_MSC_VER) || defined(__EMX__)
140 p = strchr(path, '\\');
141 while (p) {
142 *p++ = '/';
143 p = strchr(p, '\\');
144 }
145#endif
146
147 p = path + len;
148 while (--p > path && *p == '/')
149 ;
150 *++p = '\0';
151 while ((p = strrchr(path, '/')) != NULL) {
152 /* Delete trailing slashes. */
153 while (--p >= path && *p == '/')
154 ;
155 *++p = '\0';
156 if (p == path)
157 break;
158#if defined(_MSC_VER) || defined(__EMX__)
159 if (p[-1] == ':' && p - 2 == path)
160 break;
161#endif
162
163 if (rmdir(path) < 0) {
164 if (ignore_fail_on_non_empty && errno != ENOTEMPTY)
165 break;
166 warn("%s", path);
167 return (1);
168 }
169 if (vflag)
170 printf("%s\n", path);
171 }
172
173 return (0);
174}
175
176static int
177usage(void)
178{
179
180 (void)fprintf(stderr, "usage: rmdir [-pv] directory ...\n");
181 return 1;
182}
Note: See TracBrowser for help on using the repository browser.