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, 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 void rm_tree(char **);
75 static void siginfo(int __unused);
76 static void usage(void);
80 * This rm is different from historic rm's, but is expected to match
81 * POSIX 1003.2 behavior. The most visible difference is that -f
82 * has two specific effects now, ignore non-existent files and force
86 main(int argc, char *argv[])
91 (void)setlocale(LC_ALL, "");
94 * Test for the special case where the utility is called as
95 * "unlink", for which the functionality provided is greatly
98 if ((p = strrchr(argv[0], '/')) == NULL)
102 if (strcmp(p, "unlink") == 0) {
105 else if (argc == 3 && strcmp(argv[1], "--") == 0)
113 while ((ch = getopt(argc, argv, "dfiIPRrvWx")) != -1)
130 /* Compatibility no-op. */
133 case 'r': /* Compatibility. */
161 (void)signal(SIGINFO, siginfo);
163 stdin_ok = isatty(STDIN_FILENO);
166 if (check2(argv) == 0)
188 * Remove a file hierarchy. If forcing removal (-f), or interactive
189 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
191 needstat = !uid || (!fflag && !iflag && stdin_ok);
194 * If the -i option is specified, the user can skip on the pre-order
195 * visit. The fts_number field flags skipped directories.
199 flags = FTS_PHYSICAL;
203 flags |= FTS_WHITEOUT;
206 if (!(fts = fts_open(argv, flags, NULL))) {
207 if (fflag && errno == ENOENT)
211 while ((p = fts_read(fts)) != NULL) {
212 switch (p->fts_info) {
214 if (!fflag || p->fts_errno != ENOENT) {
216 p->fts_path, strerror(p->fts_errno));
221 errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
224 * Assume that since fts_read() couldn't stat the
225 * file, it can't be unlinked.
229 if (!fflag || p->fts_errno != ENOENT) {
231 p->fts_path, strerror(p->fts_errno));
236 /* Pre-order: give user chance to skip. */
237 if (!fflag && !check(p->fts_path, p->fts_accpath,
239 (void)fts_set(fts, p, FTS_SKIP);
240 p->fts_number = SKIPPED;
243 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
244 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
245 lchflags(p->fts_accpath,
246 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0)
250 /* Post-order: see if user skipped. */
251 if (p->fts_number == SKIPPED)
256 !check(p->fts_path, p->fts_accpath, p->fts_statp))
262 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
263 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
264 rval = lchflags(p->fts_accpath,
265 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
268 * If we can't read or search the directory, may still be
269 * able to remove it. Don't print out the un{read,search}able
270 * message unless the remove fails.
272 switch (p->fts_info) {
275 rval = rmdir(p->fts_accpath);
276 if (rval == 0 || (fflag && errno == ENOENT)) {
277 if (rval == 0 && vflag)
280 if (rval == 0 && info) {
290 rval = undelete(p->fts_accpath);
291 if (rval == 0 && (fflag && errno == ENOENT)) {
306 * Assume that since fts_read() couldn't stat
307 * the file, it can't be unlinked.
316 rval = unlink(p->fts_accpath);
317 if (rval == 0 || (fflag && errno == ENOENT)) {
318 if (rval == 0 && vflag)
321 if (rval == 0 && info) {
331 warn("%s", p->fts_path);
347 * Remove a file. POSIX 1003.2 states that, by default, attempting
348 * to remove a directory is an error, so must always stat the file.
350 while ((f = *argv++) != NULL) {
351 /* Assume if can't stat the file, can't unlink it. */
354 sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
356 if (!fflag || errno != ENOENT) {
363 warnx("%s: %s", f, strerror(EEXIST));
368 if (S_ISDIR(sb.st_mode) && !dflag) {
369 warnx("%s: is a directory", f);
373 if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
376 if (!uid && !S_ISWHT(sb.st_mode) &&
377 (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
378 !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
379 rval = lchflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
381 if (S_ISWHT(sb.st_mode))
383 else if (S_ISDIR(sb.st_mode))
388 if (rval && (!fflag || errno != ENOENT)) {
392 if (vflag && rval == 0)
393 (void)printf("%s\n", f);
394 if (info && rval == 0) {
396 (void)printf("%s\n", f);
402 check(const char *path, const char *name, struct stat *sp)
405 char modep[15], *flagsp;
407 /* Check -i first. */
409 (void)fprintf(stderr, "remove %s? ", path);
412 * If it's not a symbolic link and it's unwritable and we're
413 * talking to a terminal, ask. Symbolic links are excluded
414 * because their permissions are meaningless. Check stdin_ok
415 * first because we may not have stat'ed the file.
417 if (!stdin_ok || S_ISLNK(sp->st_mode) ||
418 (!access(name, W_OK) &&
419 !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
420 (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid)))
422 strmode(sp->st_mode, modep);
423 if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
424 err(1, "fflagstostr");
425 (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
426 modep + 1, modep[10] == ' ' ? "" : " ",
427 user_from_uid(sp->st_uid, 0),
428 group_from_gid(sp->st_gid, 0),
429 *flagsp ? flagsp : "", *flagsp ? " " : "",
433 (void)fflush(stderr);
435 first = ch = getchar();
436 while (ch != '\n' && ch != EOF)
438 return (first == 'y' || first == 'Y');
441 #define ISSLASH(a) ((a)[0] == '/' && (a)[1] == '\0')
443 checkslash(char **argv)
449 for (t = argv; *t;) {
452 warnx("\"/\" may not be removed");
454 for (u = t; u[0] != NULL; ++u)
471 const char *dname = NULL;
473 for (i = 0; argv[i]; ++i) {
474 if (lstat(argv[i], &st) == 0) {
475 if (S_ISDIR(st.st_mode)) {
477 dname = argv[i]; /* only used if 1 dir */
484 while (first != 'n' && first != 'N' && first != 'y' && first != 'Y') {
485 if (dcount && rflag) {
486 fprintf(stderr, "recursively remove");
488 fprintf(stderr, " %s", dname);
490 fprintf(stderr, " %d dirs", dcount);
492 fprintf(stderr, " and 1 file");
494 fprintf(stderr, " and %d files", fcount);
495 } else if (dcount + fcount > 3) {
496 fprintf(stderr, "remove %d files", dcount + fcount);
500 fprintf(stderr, "? ");
503 first = ch = getchar();
504 while (ch != '\n' && ch != EOF)
509 return (first == 'y' || first == 'Y');
512 #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
514 checkdot(char **argv)
516 char *p, **save, **t;
520 for (t = argv; *t;) {
521 if ((p = strrchr(*t, '/')) != NULL)
527 warnx("\".\" and \"..\" may not be removed");
529 for (save = t; (t[0] = t[1]) != NULL; ++t)
541 (void)fprintf(stderr, "%s\n%s\n",
542 "usage: rm [-f | -i] [-dIPRrvWx] file ...",
543 " unlink [--] file");
548 siginfo(int sig __unused)