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>
63 static int dflag, eval, fflag, iflag, vflag, Wflag, stdin_ok;
64 static int rflag, Iflag, xflag;
66 static volatile sig_atomic_t info;
68 static int check(const char *, const char *, struct stat *);
69 static int check2(char **);
70 static void checkdot(char **);
71 static void checkslash(char **);
72 static void rm_file(char **);
73 static void rm_tree(char **);
74 static void siginfo(int __unused);
75 static void usage(void);
79 * This rm is different from historic rm's, but is expected to match
80 * POSIX 1003.2 behavior. The most visible difference is that -f
81 * has two specific effects now, ignore non-existent files and force
85 main(int argc, char *argv[])
90 (void)setlocale(LC_ALL, "");
93 * Test for the special case where the utility is called as
94 * "unlink", for which the functionality provided is greatly
97 if ((p = strrchr(argv[0], '/')) == NULL)
101 if (strcmp(p, "unlink") == 0) {
104 else if (argc == 3 && strcmp(argv[1], "--") == 0)
112 while ((ch = getopt(argc, argv, "dfiIPRrvWx")) != -1)
129 /* Compatibility no-op. */
132 case 'r': /* Compatibility. */
160 (void)signal(SIGINFO, siginfo);
162 stdin_ok = isatty(STDIN_FILENO);
165 if (check2(argv) == 0)
187 * Remove a file hierarchy. If forcing removal (-f), or interactive
188 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
190 needstat = !uid || (!fflag && !iflag && stdin_ok);
193 * If the -i option is specified, the user can skip on the pre-order
194 * visit. The fts_number field flags skipped directories.
198 flags = FTS_PHYSICAL;
202 flags |= FTS_WHITEOUT;
205 if (!(fts = fts_open(argv, flags, NULL))) {
206 if (fflag && errno == ENOENT)
210 while (errno = 0, (p = fts_read(fts)) != NULL) {
211 switch (p->fts_info) {
213 if (!fflag || p->fts_errno != ENOENT) {
215 p->fts_path, strerror(p->fts_errno));
220 errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
223 * Assume that since fts_read() couldn't stat the
224 * file, it can't be unlinked.
228 if (!fflag || p->fts_errno != ENOENT) {
230 p->fts_path, strerror(p->fts_errno));
235 /* Pre-order: give user chance to skip. */
236 if (!fflag && !check(p->fts_path, p->fts_accpath,
238 (void)fts_set(fts, p, FTS_SKIP);
239 p->fts_number = SKIPPED;
242 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
243 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
244 lchflags(p->fts_accpath,
245 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0)
249 /* Post-order: see if user skipped. */
250 if (p->fts_number == SKIPPED)
255 !check(p->fts_path, p->fts_accpath, p->fts_statp))
261 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
262 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
263 rval = lchflags(p->fts_accpath,
264 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
267 * If we can't read or search the directory, may still be
268 * able to remove it. Don't print out the un{read,search}able
269 * message unless the remove fails.
271 switch (p->fts_info) {
274 rval = rmdir(p->fts_accpath);
275 if (rval == 0 || (fflag && errno == ENOENT)) {
276 if (rval == 0 && vflag)
279 if (rval == 0 && info) {
289 rval = undelete(p->fts_accpath);
290 if (rval == 0 && (fflag && errno == ENOENT)) {
305 * Assume that since fts_read() couldn't stat
306 * the file, it can't be unlinked.
315 rval = unlink(p->fts_accpath);
316 if (rval == 0 || (fflag && errno == ENOENT)) {
317 if (rval == 0 && vflag)
320 if (rval == 0 && info) {
330 warn("%s", p->fts_path);
346 * Remove a file. POSIX 1003.2 states that, by default, attempting
347 * to remove a directory is an error, so must always stat the file.
349 while ((f = *argv++) != NULL) {
350 /* Assume if can't stat the file, can't unlink it. */
353 sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
355 if (!fflag || errno != ENOENT) {
362 warnx("%s: %s", f, strerror(EEXIST));
367 if (S_ISDIR(sb.st_mode) && !dflag) {
368 warnx("%s: is a directory", f);
372 if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
375 if (!uid && !S_ISWHT(sb.st_mode) &&
376 (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
377 !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
378 rval = lchflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
380 if (S_ISWHT(sb.st_mode))
382 else if (S_ISDIR(sb.st_mode))
387 if (rval && (!fflag || errno != ENOENT)) {
391 if (vflag && rval == 0)
392 (void)printf("%s\n", f);
393 if (info && rval == 0) {
395 (void)printf("%s\n", f);
401 check(const char *path, const char *name, struct stat *sp)
404 char modep[15], *flagsp;
406 /* Check -i first. */
408 (void)fprintf(stderr, "remove %s? ", path);
411 * If it's not a symbolic link and it's unwritable and we're
412 * talking to a terminal, ask. Symbolic links are excluded
413 * because their permissions are meaningless. Check stdin_ok
414 * first because we may not have stat'ed the file.
416 if (!stdin_ok || S_ISLNK(sp->st_mode) ||
417 (!access(name, W_OK) &&
418 !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
419 (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid)))
421 strmode(sp->st_mode, modep);
422 if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
423 err(1, "fflagstostr");
424 (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
425 modep + 1, modep[10] == ' ' ? "" : " ",
426 user_from_uid(sp->st_uid, 0),
427 group_from_gid(sp->st_gid, 0),
428 *flagsp ? flagsp : "", *flagsp ? " " : "",
432 (void)fflush(stderr);
434 first = ch = getchar();
435 while (ch != '\n' && ch != EOF)
437 return (first == 'y' || first == 'Y');
440 #define ISSLASH(a) ((a)[0] == '/' && (a)[1] == '\0')
442 checkslash(char **argv)
448 for (t = argv; *t;) {
451 warnx("\"/\" may not be removed");
453 for (u = t; u[0] != NULL; ++u)
470 const char *dname = NULL;
472 for (i = 0; argv[i]; ++i) {
473 if (lstat(argv[i], &st) == 0) {
474 if (S_ISDIR(st.st_mode)) {
476 dname = argv[i]; /* only used if 1 dir */
483 while (first != 'n' && first != 'N' && first != 'y' && first != 'Y') {
484 if (dcount && rflag) {
485 fprintf(stderr, "recursively remove");
487 fprintf(stderr, " %s", dname);
489 fprintf(stderr, " %d dirs", dcount);
491 fprintf(stderr, " and 1 file");
493 fprintf(stderr, " and %d files", fcount);
494 } else if (dcount + fcount > 3) {
495 fprintf(stderr, "remove %d files", dcount + fcount);
499 fprintf(stderr, "? ");
502 first = ch = getchar();
503 while (ch != '\n' && ch != EOF)
508 return (first == 'y' || first == 'Y');
511 #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
513 checkdot(char **argv)
515 char *p, **save, **t;
519 for (t = argv; *t;) {
520 if ((p = strrchr(*t, '/')) != NULL)
526 warnx("\".\" and \"..\" may not be removed");
528 for (save = t; (t[0] = t[1]) != NULL; ++t)
540 (void)fprintf(stderr, "%s\n%s\n",
541 "usage: rm [-f | -i] [-dIPRrvWx] file ...",
542 " unlink [--] file");
547 siginfo(int sig __unused)