rm.c revision 1.24 1 1.24 mycroft /* $NetBSD: rm.c,v 1.24 1998/07/28 11:41:51 mycroft Exp $ */
2 1.18 cgd
3 1.1 cgd /*-
4 1.15 mycroft * Copyright (c) 1990, 1993, 1994
5 1.15 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.20 christos #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.20 christos __COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\n\
39 1.20 christos The Regents of the University of California. All rights reserved.\n");
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.18 cgd #if 0
44 1.19 jtc static char sccsid[] = "@(#)rm.c 8.8 (Berkeley) 4/27/95";
45 1.18 cgd #else
46 1.24 mycroft __RCSID("$NetBSD: rm.c,v 1.24 1998/07/28 11:41:51 mycroft Exp $");
47 1.18 cgd #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.15 mycroft #include <sys/types.h>
51 1.15 mycroft #include <sys/stat.h>
52 1.15 mycroft
53 1.15 mycroft #include <locale.h>
54 1.15 mycroft #include <err.h>
55 1.15 mycroft #include <errno.h>
56 1.15 mycroft #include <fcntl.h>
57 1.15 mycroft #include <fts.h>
58 1.7 jtc #include <stdio.h>
59 1.15 mycroft #include <stdlib.h>
60 1.7 jtc #include <string.h>
61 1.7 jtc #include <unistd.h>
62 1.20 christos #include <pwd.h>
63 1.20 christos #include <grp.h>
64 1.7 jtc
65 1.17 mycroft int dflag, eval, fflag, iflag, Pflag, Wflag, stdin_ok;
66 1.1 cgd
67 1.15 mycroft int check __P((char *, char *, struct stat *));
68 1.15 mycroft void checkdot __P((char **));
69 1.15 mycroft void rm_file __P((char **));
70 1.15 mycroft void rm_overwrite __P((char *, struct stat *));
71 1.15 mycroft void rm_tree __P((char **));
72 1.15 mycroft void usage __P((void));
73 1.20 christos int main __P((int, char *[]));
74 1.1 cgd
75 1.1 cgd /*
76 1.21 kleink * For the sake of the `-f' flag, check whether an error number indicates the
77 1.21 kleink * failure of an operation due to an non-existent file, either per se (ENOENT)
78 1.21 kleink * or because its filename argument was illegal (ENAMETOOLONG, ENOTDIR).
79 1.21 kleink */
80 1.21 kleink #define NONEXISTENT(x) \
81 1.21 kleink ((x) == ENOENT || (x) == ENAMETOOLONG || (x) == ENOTDIR)
82 1.21 kleink
83 1.21 kleink /*
84 1.1 cgd * rm --
85 1.1 cgd * This rm is different from historic rm's, but is expected to match
86 1.1 cgd * POSIX 1003.2 behavior. The most visible difference is that -f
87 1.1 cgd * has two specific effects now, ignore non-existent files and force
88 1.1 cgd * file removal.
89 1.1 cgd */
90 1.7 jtc int
91 1.1 cgd main(argc, argv)
92 1.1 cgd int argc;
93 1.15 mycroft char *argv[];
94 1.1 cgd {
95 1.1 cgd int ch, rflag;
96 1.1 cgd
97 1.22 mycroft (void)setlocale(LC_ALL, "");
98 1.7 jtc
99 1.15 mycroft Pflag = rflag = 0;
100 1.17 mycroft while ((ch = getopt(argc, argv, "dfiPRrW")) != -1)
101 1.1 cgd switch(ch) {
102 1.1 cgd case 'd':
103 1.1 cgd dflag = 1;
104 1.1 cgd break;
105 1.1 cgd case 'f':
106 1.1 cgd fflag = 1;
107 1.1 cgd iflag = 0;
108 1.1 cgd break;
109 1.1 cgd case 'i':
110 1.1 cgd fflag = 0;
111 1.1 cgd iflag = 1;
112 1.1 cgd break;
113 1.15 mycroft case 'P':
114 1.15 mycroft Pflag = 1;
115 1.15 mycroft break;
116 1.1 cgd case 'R':
117 1.15 mycroft case 'r': /* Compatibility. */
118 1.1 cgd rflag = 1;
119 1.1 cgd break;
120 1.17 mycroft case 'W':
121 1.17 mycroft Wflag = 1;
122 1.17 mycroft break;
123 1.1 cgd case '?':
124 1.1 cgd default:
125 1.1 cgd usage();
126 1.1 cgd }
127 1.1 cgd argc -= optind;
128 1.1 cgd argv += optind;
129 1.1 cgd
130 1.1 cgd if (argc < 1)
131 1.1 cgd usage();
132 1.1 cgd
133 1.1 cgd checkdot(argv);
134 1.1 cgd
135 1.11 jtc if (*argv) {
136 1.11 jtc stdin_ok = isatty(STDIN_FILENO);
137 1.1 cgd
138 1.11 jtc if (rflag)
139 1.15 mycroft rm_tree(argv);
140 1.11 jtc else
141 1.15 mycroft rm_file(argv);
142 1.11 jtc }
143 1.9 jtc
144 1.24 mycroft exit(eval);
145 1.24 mycroft /* NOTREACHED */
146 1.1 cgd }
147 1.1 cgd
148 1.7 jtc void
149 1.15 mycroft rm_tree(argv)
150 1.1 cgd char **argv;
151 1.1 cgd {
152 1.15 mycroft FTS *fts;
153 1.15 mycroft FTSENT *p;
154 1.15 mycroft int needstat;
155 1.17 mycroft int flags;
156 1.1 cgd
157 1.1 cgd /*
158 1.1 cgd * Remove a file hierarchy. If forcing removal (-f), or interactive
159 1.1 cgd * (-i) or can't ask anyway (stdin_ok), don't stat the file.
160 1.1 cgd */
161 1.1 cgd needstat = !fflag && !iflag && stdin_ok;
162 1.1 cgd
163 1.1 cgd /*
164 1.1 cgd * If the -i option is specified, the user can skip on the pre-order
165 1.1 cgd * visit. The fts_number field flags skipped directories.
166 1.1 cgd */
167 1.1 cgd #define SKIPPED 1
168 1.1 cgd
169 1.17 mycroft flags = FTS_PHYSICAL;
170 1.17 mycroft if (!needstat)
171 1.17 mycroft flags |= FTS_NOSTAT;
172 1.17 mycroft if (Wflag)
173 1.17 mycroft flags |= FTS_WHITEOUT;
174 1.20 christos if (!(fts = fts_open(argv, flags,
175 1.20 christos (int (*) __P((const FTSENT **, const FTSENT **)))NULL)))
176 1.20 christos err(1, "%s", "");
177 1.7 jtc while ((p = fts_read(fts)) != NULL) {
178 1.15 mycroft switch (p->fts_info) {
179 1.1 cgd case FTS_DNR:
180 1.15 mycroft if (!fflag || p->fts_errno != ENOENT) {
181 1.15 mycroft warnx("%s: %s",
182 1.15 mycroft p->fts_path, strerror(p->fts_errno));
183 1.15 mycroft eval = 1;
184 1.15 mycroft }
185 1.15 mycroft continue;
186 1.1 cgd case FTS_ERR:
187 1.15 mycroft errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
188 1.24 mycroft /* NOTREACHED */
189 1.1 cgd case FTS_NS:
190 1.15 mycroft /*
191 1.15 mycroft * FTS_NS: assume that if can't stat the file, it
192 1.15 mycroft * can't be unlinked.
193 1.15 mycroft */
194 1.1 cgd if (!needstat)
195 1.1 cgd break;
196 1.21 kleink if (!fflag || !NONEXISTENT(p->fts_errno)) {
197 1.15 mycroft warnx("%s: %s",
198 1.15 mycroft p->fts_path, strerror(p->fts_errno));
199 1.15 mycroft eval = 1;
200 1.15 mycroft }
201 1.1 cgd continue;
202 1.1 cgd case FTS_D:
203 1.15 mycroft /* Pre-order: give user chance to skip. */
204 1.8 jtc if (!fflag && !check(p->fts_path, p->fts_accpath,
205 1.6 deraadt p->fts_statp)) {
206 1.1 cgd (void)fts_set(fts, p, FTS_SKIP);
207 1.1 cgd p->fts_number = SKIPPED;
208 1.1 cgd }
209 1.1 cgd continue;
210 1.1 cgd case FTS_DP:
211 1.15 mycroft /* Post-order: see if user skipped. */
212 1.1 cgd if (p->fts_number == SKIPPED)
213 1.1 cgd continue;
214 1.1 cgd break;
215 1.9 jtc default:
216 1.15 mycroft if (!fflag &&
217 1.15 mycroft !check(p->fts_path, p->fts_accpath, p->fts_statp))
218 1.9 jtc continue;
219 1.1 cgd }
220 1.1 cgd
221 1.1 cgd /*
222 1.1 cgd * If we can't read or search the directory, may still be
223 1.1 cgd * able to remove it. Don't print out the un{read,search}able
224 1.1 cgd * message unless the remove fails.
225 1.1 cgd */
226 1.17 mycroft switch (p->fts_info) {
227 1.17 mycroft case FTS_DP:
228 1.17 mycroft case FTS_DNR:
229 1.20 christos if (!rmdir(p->fts_accpath) ||
230 1.20 christos (fflag && errno == ENOENT))
231 1.1 cgd continue;
232 1.17 mycroft break;
233 1.17 mycroft
234 1.17 mycroft case FTS_W:
235 1.17 mycroft if (!undelete(p->fts_accpath) ||
236 1.20 christos (fflag && errno == ENOENT))
237 1.17 mycroft continue;
238 1.17 mycroft break;
239 1.17 mycroft
240 1.17 mycroft default:
241 1.15 mycroft if (Pflag)
242 1.15 mycroft rm_overwrite(p->fts_accpath, NULL);
243 1.20 christos if (!unlink(p->fts_accpath) ||
244 1.21 kleink (fflag && NONEXISTENT(errno)))
245 1.14 jtc continue;
246 1.14 jtc }
247 1.15 mycroft warn("%s", p->fts_path);
248 1.15 mycroft eval = 1;
249 1.1 cgd }
250 1.15 mycroft if (errno)
251 1.15 mycroft err(1, "fts_read");
252 1.1 cgd }
253 1.1 cgd
254 1.7 jtc void
255 1.15 mycroft rm_file(argv)
256 1.1 cgd char **argv;
257 1.1 cgd {
258 1.1 cgd struct stat sb;
259 1.15 mycroft int rval;
260 1.15 mycroft char *f;
261 1.1 cgd
262 1.1 cgd /*
263 1.1 cgd * Remove a file. POSIX 1003.2 states that, by default, attempting
264 1.1 cgd * to remove a directory is an error, so must always stat the file.
265 1.1 cgd */
266 1.7 jtc while ((f = *argv++) != NULL) {
267 1.15 mycroft /* Assume if can't stat the file, can't unlink it. */
268 1.1 cgd if (lstat(f, &sb)) {
269 1.17 mycroft if (Wflag) {
270 1.17 mycroft sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
271 1.17 mycroft } else {
272 1.21 kleink if (!fflag || !NONEXISTENT(errno)) {
273 1.17 mycroft warn("%s", f);
274 1.17 mycroft eval = 1;
275 1.17 mycroft }
276 1.17 mycroft continue;
277 1.9 jtc }
278 1.17 mycroft } else if (Wflag) {
279 1.17 mycroft warnx("%s: %s", f, strerror(EEXIST));
280 1.17 mycroft eval = 1;
281 1.1 cgd continue;
282 1.1 cgd }
283 1.17 mycroft
284 1.7 jtc if (S_ISDIR(sb.st_mode) && !dflag) {
285 1.15 mycroft warnx("%s: is a directory", f);
286 1.15 mycroft eval = 1;
287 1.1 cgd continue;
288 1.1 cgd }
289 1.17 mycroft if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
290 1.1 cgd continue;
291 1.17 mycroft if (S_ISWHT(sb.st_mode))
292 1.17 mycroft rval = undelete(f);
293 1.17 mycroft else if (S_ISDIR(sb.st_mode))
294 1.15 mycroft rval = rmdir(f);
295 1.15 mycroft else {
296 1.15 mycroft if (Pflag)
297 1.15 mycroft rm_overwrite(f, &sb);
298 1.15 mycroft rval = unlink(f);
299 1.15 mycroft }
300 1.21 kleink if (rval && (!fflag || !NONEXISTENT(errno))) {
301 1.15 mycroft warn("%s", f);
302 1.15 mycroft eval = 1;
303 1.7 jtc }
304 1.15 mycroft }
305 1.15 mycroft }
306 1.7 jtc
307 1.15 mycroft /*
308 1.15 mycroft * rm_overwrite --
309 1.15 mycroft * Overwrite the file 3 times with varying bit patterns.
310 1.15 mycroft *
311 1.15 mycroft * XXX
312 1.15 mycroft * This is a cheap way to *really* delete files. Note that only regular
313 1.15 mycroft * files are deleted, directories (and therefore names) will remain.
314 1.15 mycroft * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
315 1.15 mycroft * System V file system). In a logging file system, you'll have to have
316 1.15 mycroft * kernel support.
317 1.15 mycroft */
318 1.15 mycroft void
319 1.15 mycroft rm_overwrite(file, sbp)
320 1.15 mycroft char *file;
321 1.15 mycroft struct stat *sbp;
322 1.15 mycroft {
323 1.15 mycroft struct stat sb;
324 1.15 mycroft off_t len;
325 1.15 mycroft int fd, wlen;
326 1.15 mycroft char buf[8 * 1024];
327 1.15 mycroft
328 1.15 mycroft fd = -1;
329 1.15 mycroft if (sbp == NULL) {
330 1.15 mycroft if (lstat(file, &sb))
331 1.15 mycroft goto err;
332 1.15 mycroft sbp = &sb;
333 1.1 cgd }
334 1.15 mycroft if (!S_ISREG(sbp->st_mode))
335 1.15 mycroft return;
336 1.15 mycroft if ((fd = open(file, O_WRONLY, 0)) == -1)
337 1.15 mycroft goto err;
338 1.15 mycroft
339 1.15 mycroft #define PASS(byte) { \
340 1.15 mycroft memset(buf, byte, sizeof(buf)); \
341 1.15 mycroft for (len = sbp->st_size; len > 0; len -= wlen) { \
342 1.15 mycroft wlen = len < sizeof(buf) ? len : sizeof(buf); \
343 1.15 mycroft if (write(fd, buf, wlen) != wlen) \
344 1.15 mycroft goto err; \
345 1.15 mycroft } \
346 1.15 mycroft }
347 1.15 mycroft PASS(0xff);
348 1.15 mycroft if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
349 1.15 mycroft goto err;
350 1.15 mycroft PASS(0x00);
351 1.15 mycroft if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
352 1.15 mycroft goto err;
353 1.15 mycroft PASS(0xff);
354 1.15 mycroft if (!fsync(fd) && !close(fd))
355 1.15 mycroft return;
356 1.15 mycroft
357 1.15 mycroft err: eval = 1;
358 1.15 mycroft warn("%s", file);
359 1.1 cgd }
360 1.1 cgd
361 1.15 mycroft
362 1.7 jtc int
363 1.1 cgd check(path, name, sp)
364 1.1 cgd char *path, *name;
365 1.1 cgd struct stat *sp;
366 1.1 cgd {
367 1.15 mycroft int ch, first;
368 1.15 mycroft char modep[15];
369 1.1 cgd
370 1.1 cgd /* Check -i first. */
371 1.1 cgd if (iflag)
372 1.1 cgd (void)fprintf(stderr, "remove %s? ", path);
373 1.1 cgd else {
374 1.1 cgd /*
375 1.1 cgd * If it's not a symbolic link and it's unwritable and we're
376 1.1 cgd * talking to a terminal, ask. Symbolic links are excluded
377 1.15 mycroft * because their permissions are meaningless. Check stdin_ok
378 1.15 mycroft * first because we may not have stat'ed the file.
379 1.1 cgd */
380 1.12 jtc if (!stdin_ok || S_ISLNK(sp->st_mode) || !access(name, W_OK))
381 1.15 mycroft return (1);
382 1.1 cgd strmode(sp->st_mode, modep);
383 1.8 jtc (void)fprintf(stderr, "override %s%s%s/%s for %s? ",
384 1.8 jtc modep + 1, modep[9] == ' ' ? "" : " ",
385 1.8 jtc user_from_uid(sp->st_uid, 0),
386 1.8 jtc group_from_gid(sp->st_gid, 0), path);
387 1.1 cgd }
388 1.1 cgd (void)fflush(stderr);
389 1.1 cgd
390 1.1 cgd first = ch = getchar();
391 1.1 cgd while (ch != '\n' && ch != EOF)
392 1.1 cgd ch = getchar();
393 1.15 mycroft return (first == 'y' || first == 'Y');
394 1.1 cgd }
395 1.1 cgd
396 1.16 jtc /*
397 1.16 jtc * POSIX.2 requires that if "." or ".." are specified as the basename
398 1.16 jtc * portion of an operand, a diagnostic message be written to standard
399 1.16 jtc * error and nothing more be done with such operands.
400 1.16 jtc *
401 1.16 jtc * Since POSIX.2 defines basename as the final portion of a path after
402 1.16 jtc * trailing slashes have been removed, we'll remove them here.
403 1.16 jtc */
404 1.20 christos #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
405 1.7 jtc void
406 1.1 cgd checkdot(argv)
407 1.1 cgd char **argv;
408 1.1 cgd {
409 1.15 mycroft char *p, **save, **t;
410 1.1 cgd int complained;
411 1.1 cgd
412 1.1 cgd complained = 0;
413 1.1 cgd for (t = argv; *t;) {
414 1.16 jtc /* strip trailing slashes */
415 1.16 jtc p = strrchr (*t, '\0');
416 1.16 jtc while (--p > *t && *p == '/')
417 1.16 jtc *p = '\0';
418 1.16 jtc
419 1.16 jtc /* extract basename */
420 1.15 mycroft if ((p = strrchr(*t, '/')) != NULL)
421 1.1 cgd ++p;
422 1.1 cgd else
423 1.1 cgd p = *t;
424 1.16 jtc
425 1.1 cgd if (ISDOT(p)) {
426 1.8 jtc if (!complained++)
427 1.15 mycroft warnx("\".\" and \"..\" may not be removed");
428 1.15 mycroft eval = 1;
429 1.15 mycroft for (save = t; (t[0] = t[1]) != NULL; ++t)
430 1.13 jtc continue;
431 1.1 cgd t = save;
432 1.1 cgd } else
433 1.1 cgd ++t;
434 1.1 cgd }
435 1.1 cgd }
436 1.1 cgd
437 1.7 jtc void
438 1.15 mycroft usage()
439 1.1 cgd {
440 1.1 cgd
441 1.17 mycroft (void)fprintf(stderr, "usage: rm [-dfiPRrW] file ...\n");
442 1.1 cgd exit(1);
443 1.23 mycroft /* NOTREACHED */
444 1.1 cgd }
445