14b88c807SRodney W. Grimes /*- 2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*8a16b7a1SPedro F. Giffuni * 44b88c807SRodney W. Grimes * Copyright (c) 1990, 1993, 1994 54b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 64b88c807SRodney W. Grimes * 74b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 84b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 94b88c807SRodney W. Grimes * are met: 104b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 114b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 124b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 134b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 144b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 164b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 174b88c807SRodney W. Grimes * without specific prior written permission. 184b88c807SRodney W. Grimes * 194b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 204b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 214b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 224b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 234b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 244b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 254b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 264b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 274b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 284b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 294b88c807SRodney W. Grimes * SUCH DAMAGE. 304b88c807SRodney W. Grimes */ 314b88c807SRodney W. Grimes 3209a80d48SDavid E. O'Brien #if 0 334b88c807SRodney W. Grimes #ifndef lint 3441cc862cSSteve Price static const char copyright[] = 354b88c807SRodney W. Grimes "@(#) Copyright (c) 1990, 1993, 1994\n\ 364b88c807SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 37395f4bf0SSteve Price #endif /* not lint */ 38395f4bf0SSteve Price 39395f4bf0SSteve Price #ifndef lint 40395f4bf0SSteve Price static char sccsid[] = "@(#)rm.c 8.5 (Berkeley) 4/18/94"; 414b88c807SRodney W. Grimes #endif /* not lint */ 4209a80d48SDavid E. O'Brien #endif 432749b141SDavid E. O'Brien #include <sys/cdefs.h> 442749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 454b88c807SRodney W. Grimes 464b88c807SRodney W. Grimes #include <sys/stat.h> 47f80db2b8SKris Kennaway #include <sys/param.h> 48f80db2b8SKris Kennaway #include <sys/mount.h> 494b88c807SRodney W. Grimes 504b88c807SRodney W. Grimes #include <err.h> 514b88c807SRodney W. Grimes #include <errno.h> 524b88c807SRodney W. Grimes #include <fcntl.h> 534b88c807SRodney W. Grimes #include <fts.h> 54fc69394fSWarner Losh #include <grp.h> 55b81cd107SXin LI #include <locale.h> 56fc69394fSWarner Losh #include <pwd.h> 576db1a7f1SMatthew D Fleming #include <stdint.h> 584b88c807SRodney W. Grimes #include <stdio.h> 594b88c807SRodney W. Grimes #include <stdlib.h> 604b88c807SRodney W. Grimes #include <string.h> 61bfbdd545SMichael Haro #include <sysexits.h> 624b88c807SRodney W. Grimes #include <unistd.h> 634b88c807SRodney W. Grimes 64f9d4afb4SEd Schouten static int dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok; 65d4319e74SEitan Adler static int rflag, Iflag, xflag; 66f9d4afb4SEd Schouten static uid_t uid; 67f9d4afb4SEd Schouten static volatile sig_atomic_t info; 684b88c807SRodney W. Grimes 699d6d3f96SEitan Adler static int check(const char *, const char *, struct stat *); 709d6d3f96SEitan Adler static int check2(char **); 719d6d3f96SEitan Adler static void checkdot(char **); 729d6d3f96SEitan Adler static void checkslash(char **); 739d6d3f96SEitan Adler static void rm_file(char **); 749d6d3f96SEitan Adler static int rm_overwrite(const char *, struct stat *); 759d6d3f96SEitan Adler static void rm_tree(char **); 76b5260db6SWarner Losh static void siginfo(int __unused); 779d6d3f96SEitan Adler static void usage(void); 784b88c807SRodney W. Grimes 794b88c807SRodney W. Grimes /* 804b88c807SRodney W. Grimes * rm -- 814b88c807SRodney W. Grimes * This rm is different from historic rm's, but is expected to match 824b88c807SRodney W. Grimes * POSIX 1003.2 behavior. The most visible difference is that -f 834b88c807SRodney W. Grimes * has two specific effects now, ignore non-existent files and force 844b88c807SRodney W. Grimes * file removal. 854b88c807SRodney W. Grimes */ 864b88c807SRodney W. Grimes int 8746251ddeSWarner Losh main(int argc, char *argv[]) 884b88c807SRodney W. Grimes { 8924c0f738SXin LI int ch; 90d71e172aSSheldon Hearn char *p; 91d71e172aSSheldon Hearn 92b81cd107SXin LI (void)setlocale(LC_ALL, ""); 93b81cd107SXin LI 94d71e172aSSheldon Hearn /* 95d71e172aSSheldon Hearn * Test for the special case where the utility is called as 96d71e172aSSheldon Hearn * "unlink", for which the functionality provided is greatly 97d71e172aSSheldon Hearn * simplified. 98d71e172aSSheldon Hearn */ 990cf90cd1SJilles Tjoelker if ((p = strrchr(argv[0], '/')) == NULL) 100d71e172aSSheldon Hearn p = argv[0]; 101d71e172aSSheldon Hearn else 102d71e172aSSheldon Hearn ++p; 103d71e172aSSheldon Hearn if (strcmp(p, "unlink") == 0) { 104e9393a92STim J. Robbins while (getopt(argc, argv, "") != -1) 105d71e172aSSheldon Hearn usage(); 106e9393a92STim J. Robbins argc -= optind; 107e9393a92STim J. Robbins argv += optind; 10890833c99STim J. Robbins if (argc != 1) 109e9393a92STim J. Robbins usage(); 110e9393a92STim J. Robbins rm_file(&argv[0]); 111e9393a92STim J. Robbins exit(eval); 112d71e172aSSheldon Hearn } 1134b88c807SRodney W. Grimes 114d4319e74SEitan Adler Pflag = rflag = xflag = 0; 115d4319e74SEitan Adler while ((ch = getopt(argc, argv, "dfiIPRrvWx")) != -1) 1164b88c807SRodney W. Grimes switch(ch) { 1174b88c807SRodney W. Grimes case 'd': 1184b88c807SRodney W. Grimes dflag = 1; 1194b88c807SRodney W. Grimes break; 1204b88c807SRodney W. Grimes case 'f': 1214b88c807SRodney W. Grimes fflag = 1; 1224b88c807SRodney W. Grimes iflag = 0; 1234b88c807SRodney W. Grimes break; 1244b88c807SRodney W. Grimes case 'i': 1254b88c807SRodney W. Grimes fflag = 0; 1264b88c807SRodney W. Grimes iflag = 1; 1274b88c807SRodney W. Grimes break; 12824c0f738SXin LI case 'I': 12924c0f738SXin LI Iflag = 1; 13024c0f738SXin LI break; 1314b88c807SRodney W. Grimes case 'P': 1324b88c807SRodney W. Grimes Pflag = 1; 1334b88c807SRodney W. Grimes break; 1344b88c807SRodney W. Grimes case 'R': 1354b88c807SRodney W. Grimes case 'r': /* Compatibility. */ 1364b88c807SRodney W. Grimes rflag = 1; 1374b88c807SRodney W. Grimes break; 138bfbdd545SMichael Haro case 'v': 139bfbdd545SMichael Haro vflag = 1; 140bfbdd545SMichael Haro break; 141777d1f82SMichael Haro case 'W': 142777d1f82SMichael Haro Wflag = 1; 143777d1f82SMichael Haro break; 144d4319e74SEitan Adler case 'x': 145d4319e74SEitan Adler xflag = 1; 146d4319e74SEitan Adler break; 1474b88c807SRodney W. Grimes default: 1484b88c807SRodney W. Grimes usage(); 1494b88c807SRodney W. Grimes } 1504b88c807SRodney W. Grimes argc -= optind; 1514b88c807SRodney W. Grimes argv += optind; 1524b88c807SRodney W. Grimes 1530e8f2d6cSJordan K. Hubbard if (argc < 1) { 1540e8f2d6cSJordan K. Hubbard if (fflag) 155f3761deeSGuido van Rooij return (0); 1564b88c807SRodney W. Grimes usage(); 1570e8f2d6cSJordan K. Hubbard } 1584b88c807SRodney W. Grimes 1594b88c807SRodney W. Grimes checkdot(argv); 16068ef5f71SDag-Erling Smørgrav checkslash(argv); 1616b419813SAndrey A. Chernov uid = geteuid(); 1624b88c807SRodney W. Grimes 163b5260db6SWarner Losh (void)signal(SIGINFO, siginfo); 1641f64b5c9SSteve Price if (*argv) { 1651f64b5c9SSteve Price stdin_ok = isatty(STDIN_FILENO); 1661f64b5c9SSteve Price 16724c0f738SXin LI if (Iflag) { 16824c0f738SXin LI if (check2(argv) == 0) 16924c0f738SXin LI exit (1); 17024c0f738SXin LI } 1714b88c807SRodney W. Grimes if (rflag) 1724b88c807SRodney W. Grimes rm_tree(argv); 1734b88c807SRodney W. Grimes else 1744b88c807SRodney W. Grimes rm_file(argv); 1751f64b5c9SSteve Price } 1761f64b5c9SSteve Price 1774b88c807SRodney W. Grimes exit (eval); 1784b88c807SRodney W. Grimes } 1794b88c807SRodney W. Grimes 1809d6d3f96SEitan Adler static void 18146251ddeSWarner Losh rm_tree(char **argv) 1824b88c807SRodney W. Grimes { 1834b88c807SRodney W. Grimes FTS *fts; 1844b88c807SRodney W. Grimes FTSENT *p; 1854b88c807SRodney W. Grimes int needstat; 1861f64b5c9SSteve Price int flags; 1876b419813SAndrey A. Chernov int rval; 1884b88c807SRodney W. Grimes 1894b88c807SRodney W. Grimes /* 1904b88c807SRodney W. Grimes * Remove a file hierarchy. If forcing removal (-f), or interactive 1914b88c807SRodney W. Grimes * (-i) or can't ask anyway (stdin_ok), don't stat the file. 1924b88c807SRodney W. Grimes */ 1931f64b5c9SSteve Price needstat = !uid || (!fflag && !iflag && stdin_ok); 1944b88c807SRodney W. Grimes 1954b88c807SRodney W. Grimes /* 1964b88c807SRodney W. Grimes * If the -i option is specified, the user can skip on the pre-order 1974b88c807SRodney W. Grimes * visit. The fts_number field flags skipped directories. 1984b88c807SRodney W. Grimes */ 1994b88c807SRodney W. Grimes #define SKIPPED 1 2004b88c807SRodney W. Grimes 2014a086b52SBruce Evans flags = FTS_PHYSICAL; 2021f64b5c9SSteve Price if (!needstat) 2031f64b5c9SSteve Price flags |= FTS_NOSTAT; 2041f64b5c9SSteve Price if (Wflag) 2051f64b5c9SSteve Price flags |= FTS_WHITEOUT; 206d4319e74SEitan Adler if (xflag) 207d4319e74SEitan Adler flags |= FTS_XDEV; 208de3abdfaSJordan K. Hubbard if (!(fts = fts_open(argv, flags, NULL))) { 209de3abdfaSJordan K. Hubbard if (fflag && errno == ENOENT) 210de3abdfaSJordan K. Hubbard return; 2115ad9e45fSMatthew Dillon err(1, "fts_open"); 212de3abdfaSJordan K. Hubbard } 2134b88c807SRodney W. Grimes while ((p = fts_read(fts)) != NULL) { 2144b88c807SRodney W. Grimes switch (p->fts_info) { 2154b88c807SRodney W. Grimes case FTS_DNR: 2164b88c807SRodney W. Grimes if (!fflag || p->fts_errno != ENOENT) { 2174b88c807SRodney W. Grimes warnx("%s: %s", 2184b88c807SRodney W. Grimes p->fts_path, strerror(p->fts_errno)); 2194b88c807SRodney W. Grimes eval = 1; 2204b88c807SRodney W. Grimes } 2214b88c807SRodney W. Grimes continue; 2224b88c807SRodney W. Grimes case FTS_ERR: 2234b88c807SRodney W. Grimes errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno)); 2244b88c807SRodney W. Grimes case FTS_NS: 2254b88c807SRodney W. Grimes /* 226b800f53dSJun Kuriyama * Assume that since fts_read() couldn't stat the 227b800f53dSJun Kuriyama * file, it can't be unlinked. 2284b88c807SRodney W. Grimes */ 2294b88c807SRodney W. Grimes if (!needstat) 2304b88c807SRodney W. Grimes break; 2314b88c807SRodney W. Grimes if (!fflag || p->fts_errno != ENOENT) { 2324b88c807SRodney W. Grimes warnx("%s: %s", 2334b88c807SRodney W. Grimes p->fts_path, strerror(p->fts_errno)); 2344b88c807SRodney W. Grimes eval = 1; 2354b88c807SRodney W. Grimes } 2364b88c807SRodney W. Grimes continue; 2374b88c807SRodney W. Grimes case FTS_D: 2384b88c807SRodney W. Grimes /* Pre-order: give user chance to skip. */ 2391f64b5c9SSteve Price if (!fflag && !check(p->fts_path, p->fts_accpath, 2404b88c807SRodney W. Grimes p->fts_statp)) { 2414b88c807SRodney W. Grimes (void)fts_set(fts, p, FTS_SKIP); 2424b88c807SRodney W. Grimes p->fts_number = SKIPPED; 2434b88c807SRodney W. Grimes } 2446b419813SAndrey A. Chernov else if (!uid && 2456b419813SAndrey A. Chernov (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 2466b419813SAndrey A. Chernov !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 2476911f596SJilles Tjoelker lchflags(p->fts_accpath, 2486b419813SAndrey A. Chernov p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0) 2496b419813SAndrey A. Chernov goto err; 2504b88c807SRodney W. Grimes continue; 2514b88c807SRodney W. Grimes case FTS_DP: 2524b88c807SRodney W. Grimes /* Post-order: see if user skipped. */ 2534b88c807SRodney W. Grimes if (p->fts_number == SKIPPED) 2544b88c807SRodney W. Grimes continue; 2554b88c807SRodney W. Grimes break; 2561f64b5c9SSteve Price default: 2574b88c807SRodney W. Grimes if (!fflag && 2584b88c807SRodney W. Grimes !check(p->fts_path, p->fts_accpath, p->fts_statp)) 2594b88c807SRodney W. Grimes continue; 2601f64b5c9SSteve Price } 2614b88c807SRodney W. Grimes 2626b419813SAndrey A. Chernov rval = 0; 2636b419813SAndrey A. Chernov if (!uid && 2646b419813SAndrey A. Chernov (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 2656b419813SAndrey A. Chernov !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE))) 2666911f596SJilles Tjoelker rval = lchflags(p->fts_accpath, 2676b419813SAndrey A. Chernov p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)); 2680efa2040SMichael Haro if (rval == 0) { 2694b88c807SRodney W. Grimes /* 2704b88c807SRodney W. Grimes * If we can't read or search the directory, may still be 2714b88c807SRodney W. Grimes * able to remove it. Don't print out the un{read,search}able 2724b88c807SRodney W. Grimes * message unless the remove fails. 2734b88c807SRodney W. Grimes */ 2741f64b5c9SSteve Price switch (p->fts_info) { 2751f64b5c9SSteve Price case FTS_DP: 2761f64b5c9SSteve Price case FTS_DNR: 2770efa2040SMichael Haro rval = rmdir(p->fts_accpath); 2780efa2040SMichael Haro if (rval == 0 || (fflag && errno == ENOENT)) { 2790efa2040SMichael Haro if (rval == 0 && vflag) 280777d1f82SMichael Haro (void)printf("%s\n", 281b6f80a8eSDavid E. O'Brien p->fts_path); 282b5260db6SWarner Losh if (rval == 0 && info) { 283b5260db6SWarner Losh info = 0; 284b5260db6SWarner Losh (void)printf("%s\n", 285b5260db6SWarner Losh p->fts_path); 286b5260db6SWarner Losh } 2874b88c807SRodney W. Grimes continue; 288bfbdd545SMichael Haro } 2891f64b5c9SSteve Price break; 2901f64b5c9SSteve Price 2911f64b5c9SSteve Price case FTS_W: 2920efa2040SMichael Haro rval = undelete(p->fts_accpath); 2930efa2040SMichael Haro if (rval == 0 && (fflag && errno == ENOENT)) { 2940efa2040SMichael Haro if (vflag) 295777d1f82SMichael Haro (void)printf("%s\n", 296b6f80a8eSDavid E. O'Brien p->fts_path); 297b5260db6SWarner Losh if (info) { 298b5260db6SWarner Losh info = 0; 299b5260db6SWarner Losh (void)printf("%s\n", 300b5260db6SWarner Losh p->fts_path); 301b5260db6SWarner Losh } 3024b88c807SRodney W. Grimes continue; 303bfbdd545SMichael Haro } 3041f64b5c9SSteve Price break; 3051f64b5c9SSteve Price 306b800f53dSJun Kuriyama case FTS_NS: 307b800f53dSJun Kuriyama /* 308b800f53dSJun Kuriyama * Assume that since fts_read() couldn't stat 309b800f53dSJun Kuriyama * the file, it can't be unlinked. 310b800f53dSJun Kuriyama */ 311b800f53dSJun Kuriyama if (fflag) 312b800f53dSJun Kuriyama continue; 313b800f53dSJun Kuriyama /* FALLTHROUGH */ 314930e3238SXin LI 315930e3238SXin LI case FTS_F: 316930e3238SXin LI case FTS_NSOK: 3174b88c807SRodney W. Grimes if (Pflag) 318930e3238SXin LI if (!rm_overwrite(p->fts_accpath, p->fts_info == 319930e3238SXin LI FTS_NSOK ? NULL : p->fts_statp)) 320f3761deeSGuido van Rooij continue; 321930e3238SXin LI /* FALLTHROUGH */ 322930e3238SXin LI 323930e3238SXin LI default: 3240efa2040SMichael Haro rval = unlink(p->fts_accpath); 3250efa2040SMichael Haro if (rval == 0 || (fflag && errno == ENOENT)) { 3260efa2040SMichael Haro if (rval == 0 && vflag) 327777d1f82SMichael Haro (void)printf("%s\n", 328b6f80a8eSDavid E. O'Brien p->fts_path); 329b5260db6SWarner Losh if (rval == 0 && info) { 330b5260db6SWarner Losh info = 0; 331b5260db6SWarner Losh (void)printf("%s\n", 332b5260db6SWarner Losh p->fts_path); 333b5260db6SWarner Losh } 3344b88c807SRodney W. Grimes continue; 3354b88c807SRodney W. Grimes } 3366b419813SAndrey A. Chernov } 337bfbdd545SMichael Haro } 3386b419813SAndrey A. Chernov err: 3394b88c807SRodney W. Grimes warn("%s", p->fts_path); 3404b88c807SRodney W. Grimes eval = 1; 3414b88c807SRodney W. Grimes } 342413a368cSWarner Losh if (!fflag && errno) 3434b88c807SRodney W. Grimes err(1, "fts_read"); 34408941824SMaxim Konovalov fts_close(fts); 3454b88c807SRodney W. Grimes } 3464b88c807SRodney W. Grimes 3474c99904bSEitan Adler static void 34846251ddeSWarner Losh rm_file(char **argv) 3494b88c807SRodney W. Grimes { 3504b88c807SRodney W. Grimes struct stat sb; 3511f64b5c9SSteve Price int rval; 3524b88c807SRodney W. Grimes char *f; 3534b88c807SRodney W. Grimes 3544b88c807SRodney W. Grimes /* 3554b88c807SRodney W. Grimes * Remove a file. POSIX 1003.2 states that, by default, attempting 3564b88c807SRodney W. Grimes * to remove a directory is an error, so must always stat the file. 3574b88c807SRodney W. Grimes */ 3584b88c807SRodney W. Grimes while ((f = *argv++) != NULL) { 3594b88c807SRodney W. Grimes /* Assume if can't stat the file, can't unlink it. */ 3604b88c807SRodney W. Grimes if (lstat(f, &sb)) { 3611f64b5c9SSteve Price if (Wflag) { 3621f64b5c9SSteve Price sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR; 3631f64b5c9SSteve Price } else { 3644b88c807SRodney W. Grimes if (!fflag || errno != ENOENT) { 3654b88c807SRodney W. Grimes warn("%s", f); 3664b88c807SRodney W. Grimes eval = 1; 3674b88c807SRodney W. Grimes } 3684b88c807SRodney W. Grimes continue; 3694b88c807SRodney W. Grimes } 3701f64b5c9SSteve Price } else if (Wflag) { 3711f64b5c9SSteve Price warnx("%s: %s", f, strerror(EEXIST)); 3721f64b5c9SSteve Price eval = 1; 3731f64b5c9SSteve Price continue; 3741f64b5c9SSteve Price } 3751f64b5c9SSteve Price 3761f64b5c9SSteve Price if (S_ISDIR(sb.st_mode) && !dflag) { 3774b88c807SRodney W. Grimes warnx("%s: is a directory", f); 3784b88c807SRodney W. Grimes eval = 1; 3794b88c807SRodney W. Grimes continue; 3804b88c807SRodney W. Grimes } 3811f64b5c9SSteve Price if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb)) 3824b88c807SRodney W. Grimes continue; 3836b419813SAndrey A. Chernov rval = 0; 384da29c656SMaxim Konovalov if (!uid && !S_ISWHT(sb.st_mode) && 3856b419813SAndrey A. Chernov (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) && 3866b419813SAndrey A. Chernov !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE))) 3876911f596SJilles Tjoelker rval = lchflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE)); 3880efa2040SMichael Haro if (rval == 0) { 3891f64b5c9SSteve Price if (S_ISWHT(sb.st_mode)) 3901f64b5c9SSteve Price rval = undelete(f); 3911f64b5c9SSteve Price else if (S_ISDIR(sb.st_mode)) 3924b88c807SRodney W. Grimes rval = rmdir(f); 3934b88c807SRodney W. Grimes else { 3944b88c807SRodney W. Grimes if (Pflag) 395f3761deeSGuido van Rooij if (!rm_overwrite(f, &sb)) 396f3761deeSGuido van Rooij continue; 3974b88c807SRodney W. Grimes rval = unlink(f); 3984b88c807SRodney W. Grimes } 3996b419813SAndrey A. Chernov } 4004b88c807SRodney W. Grimes if (rval && (!fflag || errno != ENOENT)) { 4014b88c807SRodney W. Grimes warn("%s", f); 4024b88c807SRodney W. Grimes eval = 1; 4034b88c807SRodney W. Grimes } 4040efa2040SMichael Haro if (vflag && rval == 0) 405bfbdd545SMichael Haro (void)printf("%s\n", f); 406b5260db6SWarner Losh if (info && rval == 0) { 407b5260db6SWarner Losh info = 0; 408b5260db6SWarner Losh (void)printf("%s\n", f); 409b5260db6SWarner Losh } 4104b88c807SRodney W. Grimes } 4114b88c807SRodney W. Grimes } 4124b88c807SRodney W. Grimes 4134b88c807SRodney W. Grimes /* 4144b88c807SRodney W. Grimes * rm_overwrite -- 4154b88c807SRodney W. Grimes * Overwrite the file 3 times with varying bit patterns. 4164b88c807SRodney W. Grimes * 4174b88c807SRodney W. Grimes * XXX 4184b88c807SRodney W. Grimes * This is a cheap way to *really* delete files. Note that only regular 4194b88c807SRodney W. Grimes * files are deleted, directories (and therefore names) will remain. 4204b88c807SRodney W. Grimes * Also, this assumes a fixed-block file system (like FFS, or a V7 or a 421a3800f8fSUlrich Spörlein * System V file system). In a logging or COW file system, you'll have to 422a3800f8fSUlrich Spörlein * have kernel support. 4234b88c807SRodney W. Grimes */ 4244c99904bSEitan Adler static int 4259d6d3f96SEitan Adler rm_overwrite(const char *file, struct stat *sbp) 4264b88c807SRodney W. Grimes { 4274ec1405bSKevin Lo struct stat sb, sb2; 428f80db2b8SKris Kennaway struct statfs fsb; 4294b88c807SRodney W. Grimes off_t len; 430f80db2b8SKris Kennaway int bsize, fd, wlen; 431f80db2b8SKris Kennaway char *buf = NULL; 4324b88c807SRodney W. Grimes 4334b88c807SRodney W. Grimes fd = -1; 4344b88c807SRodney W. Grimes if (sbp == NULL) { 4354b88c807SRodney W. Grimes if (lstat(file, &sb)) 4364b88c807SRodney W. Grimes goto err; 4374b88c807SRodney W. Grimes sbp = &sb; 4384b88c807SRodney W. Grimes } 4394b88c807SRodney W. Grimes if (!S_ISREG(sbp->st_mode)) 440f3761deeSGuido van Rooij return (1); 44186da4a5eSXin LI if (sbp->st_nlink > 1 && !fflag) { 4426db1a7f1SMatthew D Fleming warnx("%s (inode %ju): not overwritten due to multiple links", 4436db1a7f1SMatthew D Fleming file, (uintmax_t)sbp->st_ino); 44486da4a5eSXin LI return (0); 4450b6f55b7SXin LI } 4464ec1405bSKevin Lo if ((fd = open(file, O_WRONLY|O_NONBLOCK|O_NOFOLLOW, 0)) == -1) 4474b88c807SRodney W. Grimes goto err; 4484ec1405bSKevin Lo if (fstat(fd, &sb2)) 4494ec1405bSKevin Lo goto err; 4504ec1405bSKevin Lo if (sb2.st_dev != sbp->st_dev || sb2.st_ino != sbp->st_ino || 4514ec1405bSKevin Lo !S_ISREG(sb2.st_mode)) { 4524ec1405bSKevin Lo errno = EPERM; 4534ec1405bSKevin Lo goto err; 4544ec1405bSKevin Lo } 455f80db2b8SKris Kennaway if (fstatfs(fd, &fsb) == -1) 456f80db2b8SKris Kennaway goto err; 457f80db2b8SKris Kennaway bsize = MAX(fsb.f_iosize, 1024); 458f80db2b8SKris Kennaway if ((buf = malloc(bsize)) == NULL) 4598f1f4338SBruce Evans err(1, "%s: malloc", file); 4604b88c807SRodney W. Grimes 4614b88c807SRodney W. Grimes #define PASS(byte) { \ 462f80db2b8SKris Kennaway memset(buf, byte, bsize); \ 4634b88c807SRodney W. Grimes for (len = sbp->st_size; len > 0; len -= wlen) { \ 464f80db2b8SKris Kennaway wlen = len < bsize ? len : bsize; \ 4654b88c807SRodney W. Grimes if (write(fd, buf, wlen) != wlen) \ 4664b88c807SRodney W. Grimes goto err; \ 4674b88c807SRodney W. Grimes } \ 4684b88c807SRodney W. Grimes } 4694b88c807SRodney W. Grimes PASS(0xff); 4704b88c807SRodney W. Grimes if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) 4714b88c807SRodney W. Grimes goto err; 4724b88c807SRodney W. Grimes PASS(0x00); 4734b88c807SRodney W. Grimes if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) 4744b88c807SRodney W. Grimes goto err; 4754b88c807SRodney W. Grimes PASS(0xff); 476f80db2b8SKris Kennaway if (!fsync(fd) && !close(fd)) { 477f80db2b8SKris Kennaway free(buf); 478f3761deeSGuido van Rooij return (1); 479f80db2b8SKris Kennaway } 4804b88c807SRodney W. Grimes 4814b88c807SRodney W. Grimes err: eval = 1; 482f80db2b8SKris Kennaway if (buf) 483f80db2b8SKris Kennaway free(buf); 4848f1f4338SBruce Evans if (fd != -1) 4858f1f4338SBruce Evans close(fd); 4864b88c807SRodney W. Grimes warn("%s", file); 487f3761deeSGuido van Rooij return (0); 4884b88c807SRodney W. Grimes } 4894b88c807SRodney W. Grimes 4904b88c807SRodney W. Grimes 4919d6d3f96SEitan Adler static int 4929d6d3f96SEitan Adler check(const char *path, const char *name, struct stat *sp) 4934b88c807SRodney W. Grimes { 4944b88c807SRodney W. Grimes int ch, first; 495141d77b8SJosef Karthauser char modep[15], *flagsp; 4964b88c807SRodney W. Grimes 4974b88c807SRodney W. Grimes /* Check -i first. */ 4984b88c807SRodney W. Grimes if (iflag) 4994b88c807SRodney W. Grimes (void)fprintf(stderr, "remove %s? ", path); 5004b88c807SRodney W. Grimes else { 5014b88c807SRodney W. Grimes /* 5024b88c807SRodney W. Grimes * If it's not a symbolic link and it's unwritable and we're 5034b88c807SRodney W. Grimes * talking to a terminal, ask. Symbolic links are excluded 5044b88c807SRodney W. Grimes * because their permissions are meaningless. Check stdin_ok 5054b88c807SRodney W. Grimes * first because we may not have stat'ed the file. 5064b88c807SRodney W. Grimes */ 507a5f62950SDoug Barton if (!stdin_ok || S_ISLNK(sp->st_mode) || 5081f64b5c9SSteve Price (!access(name, W_OK) && 5096b419813SAndrey A. Chernov !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 5101f64b5c9SSteve Price (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid))) 5114b88c807SRodney W. Grimes return (1); 5124b88c807SRodney W. Grimes strmode(sp->st_mode, modep); 513141d77b8SJosef Karthauser if ((flagsp = fflagstostr(sp->st_flags)) == NULL) 5145ad9e45fSMatthew Dillon err(1, "fflagstostr"); 515a5f62950SDoug Barton if (Pflag) 516a5f62950SDoug Barton errx(1, 517a5f62950SDoug Barton "%s: -P was specified, but file is not writable", 518a5f62950SDoug Barton path); 519141d77b8SJosef Karthauser (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ", 5206f74a1c9SEnji Cooper modep + 1, modep[10] == ' ' ? "" : " ", 5214b88c807SRodney W. Grimes user_from_uid(sp->st_uid, 0), 5226b419813SAndrey A. Chernov group_from_gid(sp->st_gid, 0), 523141d77b8SJosef Karthauser *flagsp ? flagsp : "", *flagsp ? " " : "", 5246b419813SAndrey A. Chernov path); 525141d77b8SJosef Karthauser free(flagsp); 5264b88c807SRodney W. Grimes } 5274b88c807SRodney W. Grimes (void)fflush(stderr); 5284b88c807SRodney W. Grimes 5294b88c807SRodney W. Grimes first = ch = getchar(); 5304b88c807SRodney W. Grimes while (ch != '\n' && ch != EOF) 5314b88c807SRodney W. Grimes ch = getchar(); 532b0205affSWolfram Schneider return (first == 'y' || first == 'Y'); 5334b88c807SRodney W. Grimes } 5344b88c807SRodney W. Grimes 53568ef5f71SDag-Erling Smørgrav #define ISSLASH(a) ((a)[0] == '/' && (a)[1] == '\0') 5369d6d3f96SEitan Adler static void 53768ef5f71SDag-Erling Smørgrav checkslash(char **argv) 53868ef5f71SDag-Erling Smørgrav { 53968ef5f71SDag-Erling Smørgrav char **t, **u; 54068ef5f71SDag-Erling Smørgrav int complained; 54168ef5f71SDag-Erling Smørgrav 54268ef5f71SDag-Erling Smørgrav complained = 0; 54368ef5f71SDag-Erling Smørgrav for (t = argv; *t;) { 54468ef5f71SDag-Erling Smørgrav if (ISSLASH(*t)) { 54568ef5f71SDag-Erling Smørgrav if (!complained++) 54668ef5f71SDag-Erling Smørgrav warnx("\"/\" may not be removed"); 54768ef5f71SDag-Erling Smørgrav eval = 1; 54868ef5f71SDag-Erling Smørgrav for (u = t; u[0] != NULL; ++u) 54968ef5f71SDag-Erling Smørgrav u[0] = u[1]; 55068ef5f71SDag-Erling Smørgrav } else { 55168ef5f71SDag-Erling Smørgrav ++t; 55268ef5f71SDag-Erling Smørgrav } 55368ef5f71SDag-Erling Smørgrav } 55468ef5f71SDag-Erling Smørgrav } 55568ef5f71SDag-Erling Smørgrav 5569d6d3f96SEitan Adler static int 55724c0f738SXin LI check2(char **argv) 55824c0f738SXin LI { 55924c0f738SXin LI struct stat st; 56024c0f738SXin LI int first; 56124c0f738SXin LI int ch; 56224c0f738SXin LI int fcount = 0; 56324c0f738SXin LI int dcount = 0; 56424c0f738SXin LI int i; 56524c0f738SXin LI const char *dname = NULL; 56624c0f738SXin LI 56724c0f738SXin LI for (i = 0; argv[i]; ++i) { 56824c0f738SXin LI if (lstat(argv[i], &st) == 0) { 56924c0f738SXin LI if (S_ISDIR(st.st_mode)) { 57024c0f738SXin LI ++dcount; 57124c0f738SXin LI dname = argv[i]; /* only used if 1 dir */ 57224c0f738SXin LI } else { 57324c0f738SXin LI ++fcount; 57424c0f738SXin LI } 57524c0f738SXin LI } 57624c0f738SXin LI } 57724c0f738SXin LI first = 0; 57824c0f738SXin LI while (first != 'n' && first != 'N' && first != 'y' && first != 'Y') { 57924c0f738SXin LI if (dcount && rflag) { 58024c0f738SXin LI fprintf(stderr, "recursively remove"); 58124c0f738SXin LI if (dcount == 1) 58224c0f738SXin LI fprintf(stderr, " %s", dname); 58324c0f738SXin LI else 58424c0f738SXin LI fprintf(stderr, " %d dirs", dcount); 58524c0f738SXin LI if (fcount == 1) 58624c0f738SXin LI fprintf(stderr, " and 1 file"); 58724c0f738SXin LI else if (fcount > 1) 58824c0f738SXin LI fprintf(stderr, " and %d files", fcount); 58924c0f738SXin LI } else if (dcount + fcount > 3) { 59024c0f738SXin LI fprintf(stderr, "remove %d files", dcount + fcount); 59124c0f738SXin LI } else { 59224c0f738SXin LI return(1); 59324c0f738SXin LI } 59424c0f738SXin LI fprintf(stderr, "? "); 59524c0f738SXin LI fflush(stderr); 59624c0f738SXin LI 59724c0f738SXin LI first = ch = getchar(); 59824c0f738SXin LI while (ch != '\n' && ch != EOF) 59924c0f738SXin LI ch = getchar(); 60024c0f738SXin LI if (ch == EOF) 60124c0f738SXin LI break; 60224c0f738SXin LI } 60324c0f738SXin LI return (first == 'y' || first == 'Y'); 60424c0f738SXin LI } 60524c0f738SXin LI 60667a3d3a8SPoul-Henning Kamp #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2]))) 6079d6d3f96SEitan Adler static void 60846251ddeSWarner Losh checkdot(char **argv) 6094b88c807SRodney W. Grimes { 6104b88c807SRodney W. Grimes char *p, **save, **t; 6114b88c807SRodney W. Grimes int complained; 6124b88c807SRodney W. Grimes 6134b88c807SRodney W. Grimes complained = 0; 6144b88c807SRodney W. Grimes for (t = argv; *t;) { 6154b88c807SRodney W. Grimes if ((p = strrchr(*t, '/')) != NULL) 6164b88c807SRodney W. Grimes ++p; 6174b88c807SRodney W. Grimes else 6184b88c807SRodney W. Grimes p = *t; 6194b88c807SRodney W. Grimes if (ISDOT(p)) { 6204b88c807SRodney W. Grimes if (!complained++) 6214b88c807SRodney W. Grimes warnx("\".\" and \"..\" may not be removed"); 6224b88c807SRodney W. Grimes eval = 1; 6231f64b5c9SSteve Price for (save = t; (t[0] = t[1]) != NULL; ++t) 6241f64b5c9SSteve Price continue; 6254b88c807SRodney W. Grimes t = save; 6264b88c807SRodney W. Grimes } else 6274b88c807SRodney W. Grimes ++t; 6284b88c807SRodney W. Grimes } 6294b88c807SRodney W. Grimes } 6304b88c807SRodney W. Grimes 6319d6d3f96SEitan Adler static void 63246251ddeSWarner Losh usage(void) 6334b88c807SRodney W. Grimes { 6340efa2040SMichael Haro 635d71e172aSSheldon Hearn (void)fprintf(stderr, "%s\n%s\n", 636d4319e74SEitan Adler "usage: rm [-f | -i] [-dIPRrvWx] file ...", 637d71e172aSSheldon Hearn " unlink file"); 638bfbdd545SMichael Haro exit(EX_USAGE); 6394b88c807SRodney W. Grimes } 640b5260db6SWarner Losh 641b5260db6SWarner Losh static void 642b5260db6SWarner Losh siginfo(int sig __unused) 643b5260db6SWarner Losh { 644b5260db6SWarner Losh 645b5260db6SWarner Losh info = 1; 646b5260db6SWarner Losh } 647