14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1990, 1993, 1994 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 64b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 74b88c807SRodney W. Grimes * are met: 84b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 94b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 104b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 114b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 124b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 134b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 144b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 154b88c807SRodney W. Grimes * without specific prior written permission. 164b88c807SRodney W. Grimes * 174b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 184b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 194b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 204b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 214b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 224b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 234b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 244b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 254b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 264b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 274b88c807SRodney W. Grimes * SUCH DAMAGE. 284b88c807SRodney W. Grimes */ 294b88c807SRodney W. Grimes 3009a80d48SDavid E. O'Brien #if 0 314b88c807SRodney W. Grimes #ifndef lint 3241cc862cSSteve Price static const char copyright[] = 334b88c807SRodney W. Grimes "@(#) Copyright (c) 1990, 1993, 1994\n\ 344b88c807SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 35395f4bf0SSteve Price #endif /* not lint */ 36395f4bf0SSteve Price 37395f4bf0SSteve Price #ifndef lint 38395f4bf0SSteve Price static char sccsid[] = "@(#)rm.c 8.5 (Berkeley) 4/18/94"; 394b88c807SRodney W. Grimes #endif /* not lint */ 4009a80d48SDavid E. O'Brien #endif 412749b141SDavid E. O'Brien #include <sys/cdefs.h> 422749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 434b88c807SRodney W. Grimes 444b88c807SRodney W. Grimes #include <sys/stat.h> 45f80db2b8SKris Kennaway #include <sys/param.h> 46f80db2b8SKris Kennaway #include <sys/mount.h> 474b88c807SRodney W. Grimes 484b88c807SRodney W. Grimes #include <err.h> 494b88c807SRodney W. Grimes #include <errno.h> 504b88c807SRodney W. Grimes #include <fcntl.h> 514b88c807SRodney W. Grimes #include <fts.h> 52fc69394fSWarner Losh #include <grp.h> 53fc69394fSWarner Losh #include <pwd.h> 544b88c807SRodney W. Grimes #include <stdio.h> 554b88c807SRodney W. Grimes #include <stdlib.h> 564b88c807SRodney W. Grimes #include <string.h> 57bfbdd545SMichael Haro #include <sysexits.h> 584b88c807SRodney W. Grimes #include <unistd.h> 594b88c807SRodney W. Grimes 60777d1f82SMichael Haro int dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok; 6124c0f738SXin LI int rflag, Iflag; 626b419813SAndrey A. Chernov uid_t uid; 63b5260db6SWarner Losh volatile sig_atomic_t info; 644b88c807SRodney W. Grimes 6546251ddeSWarner Losh int check(char *, char *, struct stat *); 6624c0f738SXin LI int check2(char **); 6746251ddeSWarner Losh void checkdot(char **); 6868ef5f71SDag-Erling Smørgrav void checkslash(char **); 6946251ddeSWarner Losh void rm_file(char **); 70f3761deeSGuido van Rooij int rm_overwrite(char *, struct stat *); 7146251ddeSWarner Losh void rm_tree(char **); 72b5260db6SWarner Losh static void siginfo(int __unused); 7346251ddeSWarner Losh void usage(void); 744b88c807SRodney W. Grimes 754b88c807SRodney W. Grimes /* 764b88c807SRodney W. Grimes * rm -- 774b88c807SRodney W. Grimes * This rm is different from historic rm's, but is expected to match 784b88c807SRodney W. Grimes * POSIX 1003.2 behavior. The most visible difference is that -f 794b88c807SRodney W. Grimes * has two specific effects now, ignore non-existent files and force 804b88c807SRodney W. Grimes * file removal. 814b88c807SRodney W. Grimes */ 824b88c807SRodney W. Grimes int 8346251ddeSWarner Losh main(int argc, char *argv[]) 844b88c807SRodney W. Grimes { 8524c0f738SXin LI int ch; 86d71e172aSSheldon Hearn char *p; 87d71e172aSSheldon Hearn 88d71e172aSSheldon Hearn /* 89d71e172aSSheldon Hearn * Test for the special case where the utility is called as 90d71e172aSSheldon Hearn * "unlink", for which the functionality provided is greatly 91d71e172aSSheldon Hearn * simplified. 92d71e172aSSheldon Hearn */ 93d71e172aSSheldon Hearn if ((p = rindex(argv[0], '/')) == NULL) 94d71e172aSSheldon Hearn p = argv[0]; 95d71e172aSSheldon Hearn else 96d71e172aSSheldon Hearn ++p; 97d71e172aSSheldon Hearn if (strcmp(p, "unlink") == 0) { 98e9393a92STim J. Robbins while (getopt(argc, argv, "") != -1) 99d71e172aSSheldon Hearn usage(); 100e9393a92STim J. Robbins argc -= optind; 101e9393a92STim J. Robbins argv += optind; 10290833c99STim J. Robbins if (argc != 1) 103e9393a92STim J. Robbins usage(); 104e9393a92STim J. Robbins rm_file(&argv[0]); 105e9393a92STim J. Robbins exit(eval); 106d71e172aSSheldon Hearn } 1074b88c807SRodney W. Grimes 1081f64b5c9SSteve Price Pflag = rflag = 0; 10924c0f738SXin LI while ((ch = getopt(argc, argv, "dfiIPRrvW")) != -1) 1104b88c807SRodney W. Grimes switch(ch) { 1114b88c807SRodney W. Grimes case 'd': 1124b88c807SRodney W. Grimes dflag = 1; 1134b88c807SRodney W. Grimes break; 1144b88c807SRodney W. Grimes case 'f': 1154b88c807SRodney W. Grimes fflag = 1; 1164b88c807SRodney W. Grimes iflag = 0; 1174b88c807SRodney W. Grimes break; 1184b88c807SRodney W. Grimes case 'i': 1194b88c807SRodney W. Grimes fflag = 0; 1204b88c807SRodney W. Grimes iflag = 1; 1214b88c807SRodney W. Grimes break; 12224c0f738SXin LI case 'I': 12324c0f738SXin LI Iflag = 1; 12424c0f738SXin LI break; 1254b88c807SRodney W. Grimes case 'P': 1264b88c807SRodney W. Grimes Pflag = 1; 1274b88c807SRodney W. Grimes break; 1284b88c807SRodney W. Grimes case 'R': 1294b88c807SRodney W. Grimes case 'r': /* Compatibility. */ 1304b88c807SRodney W. Grimes rflag = 1; 1314b88c807SRodney W. Grimes break; 132bfbdd545SMichael Haro case 'v': 133bfbdd545SMichael Haro vflag = 1; 134bfbdd545SMichael Haro break; 135777d1f82SMichael Haro case 'W': 136777d1f82SMichael Haro Wflag = 1; 137777d1f82SMichael Haro break; 1384b88c807SRodney W. Grimes default: 1394b88c807SRodney W. Grimes usage(); 1404b88c807SRodney W. Grimes } 1414b88c807SRodney W. Grimes argc -= optind; 1424b88c807SRodney W. Grimes argv += optind; 1434b88c807SRodney W. Grimes 1440e8f2d6cSJordan K. Hubbard if (argc < 1) { 1450e8f2d6cSJordan K. Hubbard if (fflag) 146f3761deeSGuido van Rooij return (0); 1474b88c807SRodney W. Grimes usage(); 1480e8f2d6cSJordan K. Hubbard } 1494b88c807SRodney W. Grimes 1504b88c807SRodney W. Grimes checkdot(argv); 1513f91ab92SDag-Erling Smørgrav if (getenv("POSIXLY_CORRECT") == NULL) 15268ef5f71SDag-Erling Smørgrav checkslash(argv); 1536b419813SAndrey A. Chernov uid = geteuid(); 1544b88c807SRodney W. Grimes 155b5260db6SWarner Losh (void)signal(SIGINFO, siginfo); 1561f64b5c9SSteve Price if (*argv) { 1571f64b5c9SSteve Price stdin_ok = isatty(STDIN_FILENO); 1581f64b5c9SSteve Price 15924c0f738SXin LI if (Iflag) { 16024c0f738SXin LI if (check2(argv) == 0) 16124c0f738SXin LI exit (1); 16224c0f738SXin LI } 1634b88c807SRodney W. Grimes if (rflag) 1644b88c807SRodney W. Grimes rm_tree(argv); 1654b88c807SRodney W. Grimes else 1664b88c807SRodney W. Grimes rm_file(argv); 1671f64b5c9SSteve Price } 1681f64b5c9SSteve Price 1694b88c807SRodney W. Grimes exit (eval); 1704b88c807SRodney W. Grimes } 1714b88c807SRodney W. Grimes 1724b88c807SRodney W. Grimes void 17346251ddeSWarner Losh rm_tree(char **argv) 1744b88c807SRodney W. Grimes { 1754b88c807SRodney W. Grimes FTS *fts; 1764b88c807SRodney W. Grimes FTSENT *p; 1774b88c807SRodney W. Grimes int needstat; 1781f64b5c9SSteve Price int flags; 1796b419813SAndrey A. Chernov int rval; 1804b88c807SRodney W. Grimes 1814b88c807SRodney W. Grimes /* 1824b88c807SRodney W. Grimes * Remove a file hierarchy. If forcing removal (-f), or interactive 1834b88c807SRodney W. Grimes * (-i) or can't ask anyway (stdin_ok), don't stat the file. 1844b88c807SRodney W. Grimes */ 1851f64b5c9SSteve Price needstat = !uid || (!fflag && !iflag && stdin_ok); 1864b88c807SRodney W. Grimes 1874b88c807SRodney W. Grimes /* 1884b88c807SRodney W. Grimes * If the -i option is specified, the user can skip on the pre-order 1894b88c807SRodney W. Grimes * visit. The fts_number field flags skipped directories. 1904b88c807SRodney W. Grimes */ 1914b88c807SRodney W. Grimes #define SKIPPED 1 1924b88c807SRodney W. Grimes 1934a086b52SBruce Evans flags = FTS_PHYSICAL; 1941f64b5c9SSteve Price if (!needstat) 1951f64b5c9SSteve Price flags |= FTS_NOSTAT; 1961f64b5c9SSteve Price if (Wflag) 1971f64b5c9SSteve Price flags |= FTS_WHITEOUT; 198de3abdfaSJordan K. Hubbard if (!(fts = fts_open(argv, flags, NULL))) { 199de3abdfaSJordan K. Hubbard if (fflag && errno == ENOENT) 200de3abdfaSJordan K. Hubbard return; 2015ad9e45fSMatthew Dillon err(1, "fts_open"); 202de3abdfaSJordan K. Hubbard } 2034b88c807SRodney W. Grimes while ((p = fts_read(fts)) != NULL) { 2044b88c807SRodney W. Grimes switch (p->fts_info) { 2054b88c807SRodney W. Grimes case FTS_DNR: 2064b88c807SRodney W. Grimes if (!fflag || p->fts_errno != ENOENT) { 2074b88c807SRodney W. Grimes warnx("%s: %s", 2084b88c807SRodney W. Grimes p->fts_path, strerror(p->fts_errno)); 2094b88c807SRodney W. Grimes eval = 1; 2104b88c807SRodney W. Grimes } 2114b88c807SRodney W. Grimes continue; 2124b88c807SRodney W. Grimes case FTS_ERR: 2134b88c807SRodney W. Grimes errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno)); 2144b88c807SRodney W. Grimes case FTS_NS: 2154b88c807SRodney W. Grimes /* 216b800f53dSJun Kuriyama * Assume that since fts_read() couldn't stat the 217b800f53dSJun Kuriyama * file, it can't be unlinked. 2184b88c807SRodney W. Grimes */ 2194b88c807SRodney W. Grimes if (!needstat) 2204b88c807SRodney W. Grimes break; 2214b88c807SRodney W. Grimes if (!fflag || p->fts_errno != ENOENT) { 2224b88c807SRodney W. Grimes warnx("%s: %s", 2234b88c807SRodney W. Grimes p->fts_path, strerror(p->fts_errno)); 2244b88c807SRodney W. Grimes eval = 1; 2254b88c807SRodney W. Grimes } 2264b88c807SRodney W. Grimes continue; 2274b88c807SRodney W. Grimes case FTS_D: 2284b88c807SRodney W. Grimes /* Pre-order: give user chance to skip. */ 2291f64b5c9SSteve Price if (!fflag && !check(p->fts_path, p->fts_accpath, 2304b88c807SRodney W. Grimes p->fts_statp)) { 2314b88c807SRodney W. Grimes (void)fts_set(fts, p, FTS_SKIP); 2324b88c807SRodney W. Grimes p->fts_number = SKIPPED; 2334b88c807SRodney W. Grimes } 2346b419813SAndrey A. Chernov else if (!uid && 2356b419813SAndrey A. Chernov (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 2366b419813SAndrey A. Chernov !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 2376911f596SJilles Tjoelker lchflags(p->fts_accpath, 2386b419813SAndrey A. Chernov p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0) 2396b419813SAndrey A. Chernov goto err; 2404b88c807SRodney W. Grimes continue; 2414b88c807SRodney W. Grimes case FTS_DP: 2424b88c807SRodney W. Grimes /* Post-order: see if user skipped. */ 2434b88c807SRodney W. Grimes if (p->fts_number == SKIPPED) 2444b88c807SRodney W. Grimes continue; 2454b88c807SRodney W. Grimes break; 2461f64b5c9SSteve Price default: 2474b88c807SRodney W. Grimes if (!fflag && 2484b88c807SRodney W. Grimes !check(p->fts_path, p->fts_accpath, p->fts_statp)) 2494b88c807SRodney W. Grimes continue; 2501f64b5c9SSteve Price } 2514b88c807SRodney W. Grimes 2526b419813SAndrey A. Chernov rval = 0; 2536b419813SAndrey A. Chernov if (!uid && 2546b419813SAndrey A. Chernov (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 2556b419813SAndrey A. Chernov !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE))) 2566911f596SJilles Tjoelker rval = lchflags(p->fts_accpath, 2576b419813SAndrey A. Chernov p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)); 2580efa2040SMichael Haro if (rval == 0) { 2594b88c807SRodney W. Grimes /* 2604b88c807SRodney W. Grimes * If we can't read or search the directory, may still be 2614b88c807SRodney W. Grimes * able to remove it. Don't print out the un{read,search}able 2624b88c807SRodney W. Grimes * message unless the remove fails. 2634b88c807SRodney W. Grimes */ 2641f64b5c9SSteve Price switch (p->fts_info) { 2651f64b5c9SSteve Price case FTS_DP: 2661f64b5c9SSteve Price case FTS_DNR: 2670efa2040SMichael Haro rval = rmdir(p->fts_accpath); 2680efa2040SMichael Haro if (rval == 0 || (fflag && errno == ENOENT)) { 2690efa2040SMichael Haro if (rval == 0 && vflag) 270777d1f82SMichael Haro (void)printf("%s\n", 271b6f80a8eSDavid E. O'Brien p->fts_path); 272b5260db6SWarner Losh if (rval == 0 && info) { 273b5260db6SWarner Losh info = 0; 274b5260db6SWarner Losh (void)printf("%s\n", 275b5260db6SWarner Losh p->fts_path); 276b5260db6SWarner Losh } 2774b88c807SRodney W. Grimes continue; 278bfbdd545SMichael Haro } 2791f64b5c9SSteve Price break; 2801f64b5c9SSteve Price 2811f64b5c9SSteve Price case FTS_W: 2820efa2040SMichael Haro rval = undelete(p->fts_accpath); 2830efa2040SMichael Haro if (rval == 0 && (fflag && errno == ENOENT)) { 2840efa2040SMichael Haro if (vflag) 285777d1f82SMichael Haro (void)printf("%s\n", 286b6f80a8eSDavid E. O'Brien p->fts_path); 287b5260db6SWarner Losh if (info) { 288b5260db6SWarner Losh info = 0; 289b5260db6SWarner Losh (void)printf("%s\n", 290b5260db6SWarner Losh p->fts_path); 291b5260db6SWarner Losh } 2924b88c807SRodney W. Grimes continue; 293bfbdd545SMichael Haro } 2941f64b5c9SSteve Price break; 2951f64b5c9SSteve Price 296b800f53dSJun Kuriyama case FTS_NS: 297b800f53dSJun Kuriyama /* 298b800f53dSJun Kuriyama * Assume that since fts_read() couldn't stat 299b800f53dSJun Kuriyama * the file, it can't be unlinked. 300b800f53dSJun Kuriyama */ 301b800f53dSJun Kuriyama if (fflag) 302b800f53dSJun Kuriyama continue; 303b800f53dSJun Kuriyama /* FALLTHROUGH */ 3041f64b5c9SSteve Price default: 3054b88c807SRodney W. Grimes if (Pflag) 306f3761deeSGuido van Rooij if (!rm_overwrite(p->fts_accpath, NULL)) 307f3761deeSGuido van Rooij continue; 3080efa2040SMichael Haro rval = unlink(p->fts_accpath); 3090efa2040SMichael Haro if (rval == 0 || (fflag && errno == ENOENT)) { 3100efa2040SMichael Haro if (rval == 0 && vflag) 311777d1f82SMichael Haro (void)printf("%s\n", 312b6f80a8eSDavid E. O'Brien p->fts_path); 313b5260db6SWarner Losh if (rval == 0 && info) { 314b5260db6SWarner Losh info = 0; 315b5260db6SWarner Losh (void)printf("%s\n", 316b5260db6SWarner Losh p->fts_path); 317b5260db6SWarner Losh } 3184b88c807SRodney W. Grimes continue; 3194b88c807SRodney W. Grimes } 3206b419813SAndrey A. Chernov } 321bfbdd545SMichael Haro } 3226b419813SAndrey A. Chernov err: 3234b88c807SRodney W. Grimes warn("%s", p->fts_path); 3244b88c807SRodney W. Grimes eval = 1; 3254b88c807SRodney W. Grimes } 3264b88c807SRodney W. Grimes if (errno) 3274b88c807SRodney W. Grimes err(1, "fts_read"); 32808941824SMaxim Konovalov fts_close(fts); 3294b88c807SRodney W. Grimes } 3304b88c807SRodney W. Grimes 3314b88c807SRodney W. Grimes void 33246251ddeSWarner Losh rm_file(char **argv) 3334b88c807SRodney W. Grimes { 3344b88c807SRodney W. Grimes struct stat sb; 3351f64b5c9SSteve Price int rval; 3364b88c807SRodney W. Grimes char *f; 3374b88c807SRodney W. Grimes 3384b88c807SRodney W. Grimes /* 3394b88c807SRodney W. Grimes * Remove a file. POSIX 1003.2 states that, by default, attempting 3404b88c807SRodney W. Grimes * to remove a directory is an error, so must always stat the file. 3414b88c807SRodney W. Grimes */ 3424b88c807SRodney W. Grimes while ((f = *argv++) != NULL) { 3434b88c807SRodney W. Grimes /* Assume if can't stat the file, can't unlink it. */ 3444b88c807SRodney W. Grimes if (lstat(f, &sb)) { 3451f64b5c9SSteve Price if (Wflag) { 3461f64b5c9SSteve Price sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR; 3471f64b5c9SSteve Price } else { 3484b88c807SRodney W. Grimes if (!fflag || errno != ENOENT) { 3494b88c807SRodney W. Grimes warn("%s", f); 3504b88c807SRodney W. Grimes eval = 1; 3514b88c807SRodney W. Grimes } 3524b88c807SRodney W. Grimes continue; 3534b88c807SRodney W. Grimes } 3541f64b5c9SSteve Price } else if (Wflag) { 3551f64b5c9SSteve Price warnx("%s: %s", f, strerror(EEXIST)); 3561f64b5c9SSteve Price eval = 1; 3571f64b5c9SSteve Price continue; 3581f64b5c9SSteve Price } 3591f64b5c9SSteve Price 3601f64b5c9SSteve Price if (S_ISDIR(sb.st_mode) && !dflag) { 3614b88c807SRodney W. Grimes warnx("%s: is a directory", f); 3624b88c807SRodney W. Grimes eval = 1; 3634b88c807SRodney W. Grimes continue; 3644b88c807SRodney W. Grimes } 3651f64b5c9SSteve Price if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb)) 3664b88c807SRodney W. Grimes continue; 3676b419813SAndrey A. Chernov rval = 0; 368da29c656SMaxim Konovalov if (!uid && !S_ISWHT(sb.st_mode) && 3696b419813SAndrey A. Chernov (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) && 3706b419813SAndrey A. Chernov !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE))) 3716911f596SJilles Tjoelker rval = lchflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE)); 3720efa2040SMichael Haro if (rval == 0) { 3731f64b5c9SSteve Price if (S_ISWHT(sb.st_mode)) 3741f64b5c9SSteve Price rval = undelete(f); 3751f64b5c9SSteve Price else if (S_ISDIR(sb.st_mode)) 3764b88c807SRodney W. Grimes rval = rmdir(f); 3774b88c807SRodney W. Grimes else { 3784b88c807SRodney W. Grimes if (Pflag) 379f3761deeSGuido van Rooij if (!rm_overwrite(f, &sb)) 380f3761deeSGuido van Rooij continue; 3814b88c807SRodney W. Grimes rval = unlink(f); 3824b88c807SRodney W. Grimes } 3836b419813SAndrey A. Chernov } 3844b88c807SRodney W. Grimes if (rval && (!fflag || errno != ENOENT)) { 3854b88c807SRodney W. Grimes warn("%s", f); 3864b88c807SRodney W. Grimes eval = 1; 3874b88c807SRodney W. Grimes } 3880efa2040SMichael Haro if (vflag && rval == 0) 389bfbdd545SMichael Haro (void)printf("%s\n", f); 390b5260db6SWarner Losh if (info && rval == 0) { 391b5260db6SWarner Losh info = 0; 392b5260db6SWarner Losh (void)printf("%s\n", f); 393b5260db6SWarner Losh } 3944b88c807SRodney W. Grimes } 3954b88c807SRodney W. Grimes } 3964b88c807SRodney W. Grimes 3974b88c807SRodney W. Grimes /* 3984b88c807SRodney W. Grimes * rm_overwrite -- 3994b88c807SRodney W. Grimes * Overwrite the file 3 times with varying bit patterns. 4004b88c807SRodney W. Grimes * 4014b88c807SRodney W. Grimes * XXX 4024b88c807SRodney W. Grimes * This is a cheap way to *really* delete files. Note that only regular 4034b88c807SRodney W. Grimes * files are deleted, directories (and therefore names) will remain. 4044b88c807SRodney W. Grimes * Also, this assumes a fixed-block file system (like FFS, or a V7 or a 4054b88c807SRodney W. Grimes * System V file system). In a logging file system, you'll have to have 4064b88c807SRodney W. Grimes * kernel support. 4074b88c807SRodney W. Grimes */ 408f3761deeSGuido van Rooij int 40946251ddeSWarner Losh rm_overwrite(char *file, struct stat *sbp) 4104b88c807SRodney W. Grimes { 4114b88c807SRodney W. Grimes struct stat sb; 412f80db2b8SKris Kennaway struct statfs fsb; 4134b88c807SRodney W. Grimes off_t len; 414f80db2b8SKris Kennaway int bsize, fd, wlen; 415f80db2b8SKris Kennaway char *buf = NULL; 4164b88c807SRodney W. Grimes 4174b88c807SRodney W. Grimes fd = -1; 4184b88c807SRodney W. Grimes if (sbp == NULL) { 4194b88c807SRodney W. Grimes if (lstat(file, &sb)) 4204b88c807SRodney W. Grimes goto err; 4214b88c807SRodney W. Grimes sbp = &sb; 4224b88c807SRodney W. Grimes } 4234b88c807SRodney W. Grimes if (!S_ISREG(sbp->st_mode)) 424f3761deeSGuido van Rooij return (1); 42586da4a5eSXin LI if (sbp->st_nlink > 1 && !fflag) { 4260b6f55b7SXin LI warnx("%s (inode %u): not overwritten due to multiple links", 4270b6f55b7SXin LI file, sbp->st_ino); 42886da4a5eSXin LI return (0); 4290b6f55b7SXin LI } 4304b88c807SRodney W. Grimes if ((fd = open(file, O_WRONLY, 0)) == -1) 4314b88c807SRodney W. Grimes goto err; 432f80db2b8SKris Kennaway if (fstatfs(fd, &fsb) == -1) 433f80db2b8SKris Kennaway goto err; 434f80db2b8SKris Kennaway bsize = MAX(fsb.f_iosize, 1024); 435f80db2b8SKris Kennaway if ((buf = malloc(bsize)) == NULL) 4368f1f4338SBruce Evans err(1, "%s: malloc", file); 4374b88c807SRodney W. Grimes 4384b88c807SRodney W. Grimes #define PASS(byte) { \ 439f80db2b8SKris Kennaway memset(buf, byte, bsize); \ 4404b88c807SRodney W. Grimes for (len = sbp->st_size; len > 0; len -= wlen) { \ 441f80db2b8SKris Kennaway wlen = len < bsize ? len : bsize; \ 4424b88c807SRodney W. Grimes if (write(fd, buf, wlen) != wlen) \ 4434b88c807SRodney W. Grimes goto err; \ 4444b88c807SRodney W. Grimes } \ 4454b88c807SRodney W. Grimes } 4464b88c807SRodney W. Grimes PASS(0xff); 4474b88c807SRodney W. Grimes if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) 4484b88c807SRodney W. Grimes goto err; 4494b88c807SRodney W. Grimes PASS(0x00); 4504b88c807SRodney W. Grimes if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) 4514b88c807SRodney W. Grimes goto err; 4524b88c807SRodney W. Grimes PASS(0xff); 453f80db2b8SKris Kennaway if (!fsync(fd) && !close(fd)) { 454f80db2b8SKris Kennaway free(buf); 455f3761deeSGuido van Rooij return (1); 456f80db2b8SKris Kennaway } 4574b88c807SRodney W. Grimes 4584b88c807SRodney W. Grimes err: eval = 1; 459f80db2b8SKris Kennaway if (buf) 460f80db2b8SKris Kennaway free(buf); 4618f1f4338SBruce Evans if (fd != -1) 4628f1f4338SBruce Evans close(fd); 4634b88c807SRodney W. Grimes warn("%s", file); 464f3761deeSGuido van Rooij return (0); 4654b88c807SRodney W. Grimes } 4664b88c807SRodney W. Grimes 4674b88c807SRodney W. Grimes 4684b88c807SRodney W. Grimes int 46946251ddeSWarner Losh check(char *path, char *name, struct stat *sp) 4704b88c807SRodney W. Grimes { 4714b88c807SRodney W. Grimes int ch, first; 472141d77b8SJosef Karthauser char modep[15], *flagsp; 4734b88c807SRodney W. Grimes 4744b88c807SRodney W. Grimes /* Check -i first. */ 4754b88c807SRodney W. Grimes if (iflag) 4764b88c807SRodney W. Grimes (void)fprintf(stderr, "remove %s? ", path); 4774b88c807SRodney W. Grimes else { 4784b88c807SRodney W. Grimes /* 4794b88c807SRodney W. Grimes * If it's not a symbolic link and it's unwritable and we're 4804b88c807SRodney W. Grimes * talking to a terminal, ask. Symbolic links are excluded 4814b88c807SRodney W. Grimes * because their permissions are meaningless. Check stdin_ok 4824b88c807SRodney W. Grimes * first because we may not have stat'ed the file. 4834b88c807SRodney W. Grimes */ 484a5f62950SDoug Barton if (!stdin_ok || S_ISLNK(sp->st_mode) || 4851f64b5c9SSteve Price (!access(name, W_OK) && 4866b419813SAndrey A. Chernov !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 4871f64b5c9SSteve Price (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid))) 4884b88c807SRodney W. Grimes return (1); 4894b88c807SRodney W. Grimes strmode(sp->st_mode, modep); 490141d77b8SJosef Karthauser if ((flagsp = fflagstostr(sp->st_flags)) == NULL) 4915ad9e45fSMatthew Dillon err(1, "fflagstostr"); 492a5f62950SDoug Barton if (Pflag) 493a5f62950SDoug Barton errx(1, 494a5f62950SDoug Barton "%s: -P was specified, but file is not writable", 495a5f62950SDoug Barton path); 496141d77b8SJosef Karthauser (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ", 4974b88c807SRodney W. Grimes modep + 1, modep[9] == ' ' ? "" : " ", 4984b88c807SRodney W. Grimes user_from_uid(sp->st_uid, 0), 4996b419813SAndrey A. Chernov group_from_gid(sp->st_gid, 0), 500141d77b8SJosef Karthauser *flagsp ? flagsp : "", *flagsp ? " " : "", 5016b419813SAndrey A. Chernov path); 502141d77b8SJosef Karthauser free(flagsp); 5034b88c807SRodney W. Grimes } 5044b88c807SRodney W. Grimes (void)fflush(stderr); 5054b88c807SRodney W. Grimes 5064b88c807SRodney W. Grimes first = ch = getchar(); 5074b88c807SRodney W. Grimes while (ch != '\n' && ch != EOF) 5084b88c807SRodney W. Grimes ch = getchar(); 509b0205affSWolfram Schneider return (first == 'y' || first == 'Y'); 5104b88c807SRodney W. Grimes } 5114b88c807SRodney W. Grimes 51268ef5f71SDag-Erling Smørgrav #define ISSLASH(a) ((a)[0] == '/' && (a)[1] == '\0') 51368ef5f71SDag-Erling Smørgrav void 51468ef5f71SDag-Erling Smørgrav checkslash(char **argv) 51568ef5f71SDag-Erling Smørgrav { 51668ef5f71SDag-Erling Smørgrav char **t, **u; 51768ef5f71SDag-Erling Smørgrav int complained; 51868ef5f71SDag-Erling Smørgrav 51968ef5f71SDag-Erling Smørgrav complained = 0; 52068ef5f71SDag-Erling Smørgrav for (t = argv; *t;) { 52168ef5f71SDag-Erling Smørgrav if (ISSLASH(*t)) { 52268ef5f71SDag-Erling Smørgrav if (!complained++) 52368ef5f71SDag-Erling Smørgrav warnx("\"/\" may not be removed"); 52468ef5f71SDag-Erling Smørgrav eval = 1; 52568ef5f71SDag-Erling Smørgrav for (u = t; u[0] != NULL; ++u) 52668ef5f71SDag-Erling Smørgrav u[0] = u[1]; 52768ef5f71SDag-Erling Smørgrav } else { 52868ef5f71SDag-Erling Smørgrav ++t; 52968ef5f71SDag-Erling Smørgrav } 53068ef5f71SDag-Erling Smørgrav } 53168ef5f71SDag-Erling Smørgrav } 53268ef5f71SDag-Erling Smørgrav 53324c0f738SXin LI int 53424c0f738SXin LI check2(char **argv) 53524c0f738SXin LI { 53624c0f738SXin LI struct stat st; 53724c0f738SXin LI int first; 53824c0f738SXin LI int ch; 53924c0f738SXin LI int fcount = 0; 54024c0f738SXin LI int dcount = 0; 54124c0f738SXin LI int i; 54224c0f738SXin LI const char *dname = NULL; 54324c0f738SXin LI 54424c0f738SXin LI for (i = 0; argv[i]; ++i) { 54524c0f738SXin LI if (lstat(argv[i], &st) == 0) { 54624c0f738SXin LI if (S_ISDIR(st.st_mode)) { 54724c0f738SXin LI ++dcount; 54824c0f738SXin LI dname = argv[i]; /* only used if 1 dir */ 54924c0f738SXin LI } else { 55024c0f738SXin LI ++fcount; 55124c0f738SXin LI } 55224c0f738SXin LI } 55324c0f738SXin LI } 55424c0f738SXin LI first = 0; 55524c0f738SXin LI while (first != 'n' && first != 'N' && first != 'y' && first != 'Y') { 55624c0f738SXin LI if (dcount && rflag) { 55724c0f738SXin LI fprintf(stderr, "recursively remove"); 55824c0f738SXin LI if (dcount == 1) 55924c0f738SXin LI fprintf(stderr, " %s", dname); 56024c0f738SXin LI else 56124c0f738SXin LI fprintf(stderr, " %d dirs", dcount); 56224c0f738SXin LI if (fcount == 1) 56324c0f738SXin LI fprintf(stderr, " and 1 file"); 56424c0f738SXin LI else if (fcount > 1) 56524c0f738SXin LI fprintf(stderr, " and %d files", fcount); 56624c0f738SXin LI } else if (dcount + fcount > 3) { 56724c0f738SXin LI fprintf(stderr, "remove %d files", dcount + fcount); 56824c0f738SXin LI } else { 56924c0f738SXin LI return(1); 57024c0f738SXin LI } 57124c0f738SXin LI fprintf(stderr, "? "); 57224c0f738SXin LI fflush(stderr); 57324c0f738SXin LI 57424c0f738SXin LI first = ch = getchar(); 57524c0f738SXin LI while (ch != '\n' && ch != EOF) 57624c0f738SXin LI ch = getchar(); 57724c0f738SXin LI if (ch == EOF) 57824c0f738SXin LI break; 57924c0f738SXin LI } 58024c0f738SXin LI return (first == 'y' || first == 'Y'); 58124c0f738SXin LI } 58224c0f738SXin LI 58367a3d3a8SPoul-Henning Kamp #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2]))) 5844b88c807SRodney W. Grimes void 58546251ddeSWarner Losh checkdot(char **argv) 5864b88c807SRodney W. Grimes { 5874b88c807SRodney W. Grimes char *p, **save, **t; 5884b88c807SRodney W. Grimes int complained; 5894b88c807SRodney W. Grimes 5904b88c807SRodney W. Grimes complained = 0; 5914b88c807SRodney W. Grimes for (t = argv; *t;) { 5924b88c807SRodney W. Grimes if ((p = strrchr(*t, '/')) != NULL) 5934b88c807SRodney W. Grimes ++p; 5944b88c807SRodney W. Grimes else 5954b88c807SRodney W. Grimes p = *t; 5964b88c807SRodney W. Grimes if (ISDOT(p)) { 5974b88c807SRodney W. Grimes if (!complained++) 5984b88c807SRodney W. Grimes warnx("\".\" and \"..\" may not be removed"); 5994b88c807SRodney W. Grimes eval = 1; 6001f64b5c9SSteve Price for (save = t; (t[0] = t[1]) != NULL; ++t) 6011f64b5c9SSteve Price continue; 6024b88c807SRodney W. Grimes t = save; 6034b88c807SRodney W. Grimes } else 6044b88c807SRodney W. Grimes ++t; 6054b88c807SRodney W. Grimes } 6064b88c807SRodney W. Grimes } 6074b88c807SRodney W. Grimes 6084b88c807SRodney W. Grimes void 60946251ddeSWarner Losh usage(void) 6104b88c807SRodney W. Grimes { 6110efa2040SMichael Haro 612d71e172aSSheldon Hearn (void)fprintf(stderr, "%s\n%s\n", 61324c0f738SXin LI "usage: rm [-f | -i] [-dIPRrvW] file ...", 614d71e172aSSheldon Hearn " unlink file"); 615bfbdd545SMichael Haro exit(EX_USAGE); 6164b88c807SRodney W. Grimes } 617b5260db6SWarner Losh 618b5260db6SWarner Losh static void 619b5260db6SWarner Losh siginfo(int sig __unused) 620b5260db6SWarner Losh { 621b5260db6SWarner Losh 622b5260db6SWarner Losh info = 1; 623b5260db6SWarner Losh } 624