2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1990, 1993, 1994\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)rm.c 8.5 (Berkeley) 4/18/94";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/mount.h>
64 static int dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok;
65 static int rflag, Iflag, xflag;
67 static volatile sig_atomic_t info;
69 static int check(const char *, const char *, struct stat *);
70 static int check2(char **);
71 static void checkdot(char **);
72 static void checkslash(char **);
73 static void rm_file(char **);
74 static int rm_overwrite(const char *, struct stat *);
75 static void rm_tree(char **);
76 static void siginfo(int __unused);
77 static void usage(void);
81 * This rm is different from historic rm's, but is expected to match
82 * POSIX 1003.2 behavior. The most visible difference is that -f
83 * has two specific effects now, ignore non-existent files and force
87 main(int argc, char *argv[])
92 (void)setlocale(LC_ALL, "");
95 * Test for the special case where the utility is called as
96 * "unlink", for which the functionality provided is greatly
99 if ((p = strrchr(argv[0], '/')) == NULL)
103 if (strcmp(p, "unlink") == 0) {
104 while (getopt(argc, argv, "") != -1)
114 Pflag = rflag = xflag = 0;
115 while ((ch = getopt(argc, argv, "dfiIPRrvWx")) != -1)
135 case 'r': /* Compatibility. */
163 (void)signal(SIGINFO, siginfo);
165 stdin_ok = isatty(STDIN_FILENO);
168 if (check2(argv) == 0)
190 * Remove a file hierarchy. If forcing removal (-f), or interactive
191 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
193 needstat = !uid || (!fflag && !iflag && stdin_ok);
196 * If the -i option is specified, the user can skip on the pre-order
197 * visit. The fts_number field flags skipped directories.
201 flags = FTS_PHYSICAL;
205 flags |= FTS_WHITEOUT;
208 if (!(fts = fts_open(argv, flags, NULL))) {
209 if (fflag && errno == ENOENT)
213 while ((p = fts_read(fts)) != NULL) {
214 switch (p->fts_info) {
216 if (!fflag || p->fts_errno != ENOENT) {
218 p->fts_path, strerror(p->fts_errno));
223 errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
226 * Assume that since fts_read() couldn't stat the
227 * file, it can't be unlinked.
231 if (!fflag || p->fts_errno != ENOENT) {
233 p->fts_path, strerror(p->fts_errno));
238 /* Pre-order: give user chance to skip. */
239 if (!fflag && !check(p->fts_path, p->fts_accpath,
241 (void)fts_set(fts, p, FTS_SKIP);
242 p->fts_number = SKIPPED;
245 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
246 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
247 lchflags(p->fts_accpath,
248 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0)
252 /* Post-order: see if user skipped. */
253 if (p->fts_number == SKIPPED)
258 !check(p->fts_path, p->fts_accpath, p->fts_statp))
264 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
265 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
266 rval = lchflags(p->fts_accpath,
267 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
270 * If we can't read or search the directory, may still be
271 * able to remove it. Don't print out the un{read,search}able
272 * message unless the remove fails.
274 switch (p->fts_info) {
277 rval = rmdir(p->fts_accpath);
278 if (rval == 0 || (fflag && errno == ENOENT)) {
279 if (rval == 0 && vflag)
282 if (rval == 0 && info) {
292 rval = undelete(p->fts_accpath);
293 if (rval == 0 && (fflag && errno == ENOENT)) {
308 * Assume that since fts_read() couldn't stat
309 * the file, it can't be unlinked.
318 if (!rm_overwrite(p->fts_accpath, p->fts_info ==
319 FTS_NSOK ? NULL : p->fts_statp))
324 rval = unlink(p->fts_accpath);
325 if (rval == 0 || (fflag && errno == ENOENT)) {
326 if (rval == 0 && vflag)
329 if (rval == 0 && info) {
339 warn("%s", p->fts_path);
355 * Remove a file. POSIX 1003.2 states that, by default, attempting
356 * to remove a directory is an error, so must always stat the file.
358 while ((f = *argv++) != NULL) {
359 /* Assume if can't stat the file, can't unlink it. */
362 sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
364 if (!fflag || errno != ENOENT) {
371 warnx("%s: %s", f, strerror(EEXIST));
376 if (S_ISDIR(sb.st_mode) && !dflag) {
377 warnx("%s: is a directory", f);
381 if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
384 if (!uid && !S_ISWHT(sb.st_mode) &&
385 (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
386 !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
387 rval = lchflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
389 if (S_ISWHT(sb.st_mode))
391 else if (S_ISDIR(sb.st_mode))
395 if (!rm_overwrite(f, &sb))
400 if (rval && (!fflag || errno != ENOENT)) {
404 if (vflag && rval == 0)
405 (void)printf("%s\n", f);
406 if (info && rval == 0) {
408 (void)printf("%s\n", f);
415 * Overwrite the file 3 times with varying bit patterns.
418 * This is a cheap way to *really* delete files. Note that only regular
419 * files are deleted, directories (and therefore names) will remain.
420 * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
421 * System V file system). In a logging or COW file system, you'll have to
422 * have kernel support.
425 rm_overwrite(const char *file, struct stat *sbp)
435 if (lstat(file, &sb))
439 if (!S_ISREG(sbp->st_mode))
441 if (sbp->st_nlink > 1 && !fflag) {
442 warnx("%s (inode %ju): not overwritten due to multiple links",
443 file, (uintmax_t)sbp->st_ino);
446 if ((fd = open(file, O_WRONLY|O_NONBLOCK|O_NOFOLLOW, 0)) == -1)
450 if (sb2.st_dev != sbp->st_dev || sb2.st_ino != sbp->st_ino ||
451 !S_ISREG(sb2.st_mode)) {
455 if (fstatfs(fd, &fsb) == -1)
457 bsize = MAX(fsb.f_iosize, 1024);
458 if ((buf = malloc(bsize)) == NULL)
459 err(1, "%s: malloc", file);
461 #define PASS(byte) { \
462 memset(buf, byte, bsize); \
463 for (len = sbp->st_size; len > 0; len -= wlen) { \
464 wlen = len < bsize ? len : bsize; \
465 if (write(fd, buf, wlen) != wlen) \
470 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
473 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
476 if (!fsync(fd) && !close(fd)) {
492 check(const char *path, const char *name, struct stat *sp)
495 char modep[15], *flagsp;
497 /* Check -i first. */
499 (void)fprintf(stderr, "remove %s? ", path);
502 * If it's not a symbolic link and it's unwritable and we're
503 * talking to a terminal, ask. Symbolic links are excluded
504 * because their permissions are meaningless. Check stdin_ok
505 * first because we may not have stat'ed the file.
507 if (!stdin_ok || S_ISLNK(sp->st_mode) ||
508 (!access(name, W_OK) &&
509 !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
510 (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid)))
512 strmode(sp->st_mode, modep);
513 if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
514 err(1, "fflagstostr");
517 "%s: -P was specified, but file is not writable",
519 (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
520 modep + 1, modep[10] == ' ' ? "" : " ",
521 user_from_uid(sp->st_uid, 0),
522 group_from_gid(sp->st_gid, 0),
523 *flagsp ? flagsp : "", *flagsp ? " " : "",
527 (void)fflush(stderr);
529 first = ch = getchar();
530 while (ch != '\n' && ch != EOF)
532 return (first == 'y' || first == 'Y');
535 #define ISSLASH(a) ((a)[0] == '/' && (a)[1] == '\0')
537 checkslash(char **argv)
543 for (t = argv; *t;) {
546 warnx("\"/\" may not be removed");
548 for (u = t; u[0] != NULL; ++u)
565 const char *dname = NULL;
567 for (i = 0; argv[i]; ++i) {
568 if (lstat(argv[i], &st) == 0) {
569 if (S_ISDIR(st.st_mode)) {
571 dname = argv[i]; /* only used if 1 dir */
578 while (first != 'n' && first != 'N' && first != 'y' && first != 'Y') {
579 if (dcount && rflag) {
580 fprintf(stderr, "recursively remove");
582 fprintf(stderr, " %s", dname);
584 fprintf(stderr, " %d dirs", dcount);
586 fprintf(stderr, " and 1 file");
588 fprintf(stderr, " and %d files", fcount);
589 } else if (dcount + fcount > 3) {
590 fprintf(stderr, "remove %d files", dcount + fcount);
594 fprintf(stderr, "? ");
597 first = ch = getchar();
598 while (ch != '\n' && ch != EOF)
603 return (first == 'y' || first == 'Y');
606 #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
608 checkdot(char **argv)
610 char *p, **save, **t;
614 for (t = argv; *t;) {
615 if ((p = strrchr(*t, '/')) != NULL)
621 warnx("\".\" and \"..\" may not be removed");
623 for (save = t; (t[0] = t[1]) != NULL; ++t)
635 (void)fprintf(stderr, "%s\n%s\n",
636 "usage: rm [-f | -i] [-dIPRrvWx] file ...",
642 siginfo(int sig __unused)