rm.c revision 1.4 1 /*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * 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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 static char sccsid[] = "@(#)rm.c 4.26 (Berkeley) 3/10/91";
42 static char rcsid[] = "$Header: /tank/opengrok/rsync2/NetBSD/src/bin/rm/rm.c,v 1.4 1993/04/10 00:57:03 mycroft Exp $";
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/errno.h>
48 #include <fts.h>
49 #include <unistd.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <stdlib.h>
53
54 int dflag, fflag, iflag, retval, stdin_ok;
55
56 /*
57 * rm --
58 * This rm is different from historic rm's, but is expected to match
59 * POSIX 1003.2 behavior. The most visible difference is that -f
60 * has two specific effects now, ignore non-existent files and force
61 * file removal.
62 */
63
64 main(argc, argv)
65 int argc;
66 char **argv;
67 {
68 extern char *optarg;
69 extern int optind;
70 int ch, rflag;
71
72 rflag = 0;
73 while ((ch = getopt(argc, argv, "dfiRr")) != EOF)
74 switch(ch) {
75 case 'd':
76 dflag = 1;
77 break;
78 case 'f':
79 fflag = 1;
80 iflag = 0;
81 break;
82 case 'i':
83 fflag = 0;
84 iflag = 1;
85 break;
86 case 'R':
87 case 'r': /* compatibility */
88 rflag = 1;
89 break;
90 case '?':
91 default:
92 usage();
93 }
94 argc -= optind;
95 argv += optind;
96
97 if (argc < 1)
98 usage();
99
100 checkdot(argv);
101 if (!*argv)
102 exit(fflag ? 0 : retval);
103
104 stdin_ok = isatty(STDIN_FILENO);
105
106 if (rflag)
107 rmtree(argv);
108 else
109 rmfile(argv);
110 exit(fflag ? 0 : retval);
111 }
112
113 rmtree(argv)
114 char **argv;
115 {
116 register FTS *fts;
117 register FTSENT *p;
118 register int needstat;
119 struct stat sb;
120
121 /*
122 * Remove a file hierarchy. If forcing removal (-f), or interactive
123 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
124 */
125 needstat = !fflag && !iflag && stdin_ok;
126
127 /*
128 * If the -i option is specified, the user can skip on the pre-order
129 * visit. The fts_number field flags skipped directories.
130 */
131 #define SKIPPED 1
132
133 if (!(fts = fts_open(argv,
134 needstat ? FTS_PHYSICAL : FTS_PHYSICAL|FTS_NOSTAT,
135 (int (*)())NULL)) && !fflag) {
136 (void)fprintf(stderr, "rm: %s.\n", strerror(errno));
137 exit(1);
138 }
139 while (p = fts_read(fts)) {
140 switch(p->fts_info) {
141 case FTS_DNR:
142 case FTS_ERR:
143 error(p->fts_path, errno);
144 exit(1);
145 /*
146 * FTS_NS: assume that if can't stat the file, it can't be
147 * unlinked.
148 */
149 case FTS_NS:
150 if (!needstat)
151 break;
152 if (!fflag || errno != ENOENT)
153 error(p->fts_path, errno);
154 continue;
155 /* Pre-order: give user chance to skip. */
156 case FTS_D:
157 if (iflag && !check(p->fts_path, p->fts_accpath,
158 &p->fts_statb)) {
159 (void)fts_set(fts, p, FTS_SKIP);
160 p->fts_number = SKIPPED;
161 }
162 continue;
163 /* Post-order: see if user skipped. */
164 case FTS_DP:
165 if (p->fts_number == SKIPPED)
166 continue;
167 break;
168 }
169
170 if (!fflag &&
171 !check(p->fts_path, p->fts_accpath, &p->fts_statb))
172 continue;
173
174 /*
175 * If we can't read or search the directory, may still be
176 * able to remove it. Don't print out the un{read,search}able
177 * message unless the remove fails.
178 */
179 if (p->fts_info == FTS_DP || p->fts_info == FTS_DNR) {
180 if (!rmdir(p->fts_accpath))
181 continue;
182 if (errno == ENOENT) {
183 if (fflag)
184 continue;
185 } else if (p->fts_info != FTS_DP && !fflag)
186 (void)fprintf(stderr,
187 "rm: unable to read %s.\n", p->fts_path);
188 } else if (!unlink(p->fts_accpath) || fflag && errno == ENOENT)
189 continue;
190 error(p->fts_path, errno);
191 }
192 }
193
194 rmfile(argv)
195 char **argv;
196 {
197 register int df;
198 register char *f;
199 struct stat sb;
200
201 df = dflag;
202 /*
203 * Remove a file. POSIX 1003.2 states that, by default, attempting
204 * to remove a directory is an error, so must always stat the file.
205 */
206 while (f = *argv++) {
207 /* Assume if can't stat the file, can't unlink it. */
208 if (lstat(f, &sb)) {
209 if (!fflag || errno != ENOENT)
210 error(f, errno);
211 continue;
212 }
213 if (S_ISDIR(sb.st_mode) && !df && !fflag) {
214 (void)fprintf(stderr, "rm: %s: is a directory\n", f);
215 retval = 1;
216 continue;
217 }
218 if (!fflag && !check(f, f, &sb))
219 continue;
220 if ((S_ISDIR(sb.st_mode) ? rmdir(f) : unlink(f)) &&
221 (!fflag || errno != ENOENT))
222 error(f, errno);
223 }
224 }
225
226 check(path, name, sp)
227 char *path, *name;
228 struct stat *sp;
229 {
230 register int first, ch;
231 char modep[15], *user_from_uid(), *group_from_gid();
232
233 /* Check -i first. */
234 if (iflag)
235 (void)fprintf(stderr, "remove %s? ", path);
236 else {
237 /*
238 * If it's not a symbolic link and it's unwritable and we're
239 * talking to a terminal, ask. Symbolic links are excluded
240 * because their permissions are meaningless.
241 */
242 if (S_ISLNK(sp->st_mode) || !stdin_ok || !access(name, W_OK))
243 return(1);
244 strmode(sp->st_mode, modep);
245 if (!fflag)
246 (void)fprintf(stderr, "override %s%s%s/%s for %s? ",
247 modep + 1, modep[9] == ' ' ? "" : " ",
248 user_from_uid(sp->st_uid, 0),
249 group_from_gid(sp->st_gid, 0), path);
250 }
251 (void)fflush(stderr);
252
253 first = ch = getchar();
254 while (ch != '\n' && ch != EOF)
255 ch = getchar();
256 return(first == 'y');
257 }
258
259 #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || (a)[1] == '.' && !(a)[2]))
260 checkdot(argv)
261 char **argv;
262 {
263 register char *p, **t, **save;
264 int complained;
265
266 complained = 0;
267 for (t = argv; *t;) {
268 if (p = rindex(*t, '/'))
269 ++p;
270 else
271 p = *t;
272 if (ISDOT(p)) {
273 if (!complained++ && !fflag)
274 (void)fprintf(stderr,
275 "rm: \".\" and \"..\" may not be removed.\n");
276 retval = 1;
277 for (save = t; t[0] = t[1]; ++t);
278 t = save;
279 } else
280 ++t;
281 }
282 }
283
284 error(name, val)
285 char *name;
286 int val;
287 {
288 if (!fflag)
289 (void)fprintf(stderr, "rm: %s: %s.\n", name, strerror(val));
290 retval = 1;
291 }
292
293 usage()
294 {
295 if (!fflag)
296 (void)fprintf(stderr, "usage: rm [-dfiRr] file ...\n");
297 exit(1);
298 }
299