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 * 3. All advertising materials mentioning features or use of this software 144b88c807SRodney W. Grimes * must display the following acknowledgement: 154b88c807SRodney W. Grimes * This product includes software developed by the University of 164b88c807SRodney W. Grimes * California, Berkeley and its contributors. 174b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 184b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 194b88c807SRodney W. Grimes * without specific prior written permission. 204b88c807SRodney W. Grimes * 214b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 224b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 234b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 244b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 254b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 264b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 274b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 284b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 294b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 304b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 314b88c807SRodney W. Grimes * SUCH DAMAGE. 324b88c807SRodney W. Grimes */ 334b88c807SRodney W. Grimes 344b88c807SRodney W. Grimes #ifndef lint 3541cc862cSSteve Price static const char copyright[] = 364b88c807SRodney W. Grimes "@(#) Copyright (c) 1990, 1993, 1994\n\ 374b88c807SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 38395f4bf0SSteve Price #endif /* not lint */ 39395f4bf0SSteve Price 40395f4bf0SSteve Price #ifndef lint 41395f4bf0SSteve Price #if 0 42395f4bf0SSteve Price static char sccsid[] = "@(#)rm.c 8.5 (Berkeley) 4/18/94"; 43395f4bf0SSteve Price #else 4441cc862cSSteve Price static const char rcsid[] = 452a456239SPeter Wemm "$FreeBSD$"; 46395f4bf0SSteve Price #endif 474b88c807SRodney W. Grimes #endif /* not lint */ 484b88c807SRodney W. Grimes 494b88c807SRodney W. Grimes #include <sys/stat.h> 50f80db2b8SKris Kennaway #include <sys/param.h> 51f80db2b8SKris Kennaway #include <sys/mount.h> 524b88c807SRodney W. Grimes 534b88c807SRodney W. Grimes #include <err.h> 544b88c807SRodney W. Grimes #include <errno.h> 554b88c807SRodney W. Grimes #include <fcntl.h> 564b88c807SRodney W. Grimes #include <fts.h> 57fc69394fSWarner Losh #include <grp.h> 58fc69394fSWarner Losh #include <pwd.h> 594b88c807SRodney W. Grimes #include <stdio.h> 604b88c807SRodney W. Grimes #include <stdlib.h> 614b88c807SRodney W. Grimes #include <string.h> 62bfbdd545SMichael Haro #include <sysexits.h> 634b88c807SRodney W. Grimes #include <unistd.h> 644b88c807SRodney W. Grimes 65777d1f82SMichael Haro int dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok; 666b419813SAndrey A. Chernov uid_t uid; 674b88c807SRodney W. Grimes 6846251ddeSWarner Losh int check(char *, char *, struct stat *); 6946251ddeSWarner Losh void checkdot(char **); 7046251ddeSWarner Losh void rm_file(char **); 7146251ddeSWarner Losh void rm_overwrite(char *, struct stat *); 7246251ddeSWarner Losh void rm_tree(char **); 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 { 854b88c807SRodney W. Grimes int ch, rflag; 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; 102e9393a92STim J. Robbins if (argc == 0) 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; 109777d1f82SMichael Haro while ((ch = getopt(argc, argv, "dfiPRrvW")) != -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; 1224b88c807SRodney W. Grimes case 'P': 1234b88c807SRodney W. Grimes Pflag = 1; 1244b88c807SRodney W. Grimes break; 1254b88c807SRodney W. Grimes case 'R': 1264b88c807SRodney W. Grimes case 'r': /* Compatibility. */ 1274b88c807SRodney W. Grimes rflag = 1; 1284b88c807SRodney W. Grimes break; 129bfbdd545SMichael Haro case 'v': 130bfbdd545SMichael Haro vflag = 1; 131bfbdd545SMichael Haro break; 132777d1f82SMichael Haro case 'W': 133777d1f82SMichael Haro Wflag = 1; 134777d1f82SMichael Haro break; 1354b88c807SRodney W. Grimes default: 1364b88c807SRodney W. Grimes usage(); 1374b88c807SRodney W. Grimes } 1384b88c807SRodney W. Grimes argc -= optind; 1394b88c807SRodney W. Grimes argv += optind; 1404b88c807SRodney W. Grimes 1410e8f2d6cSJordan K. Hubbard if (argc < 1) { 1420e8f2d6cSJordan K. Hubbard if (fflag) 1430e8f2d6cSJordan K. Hubbard return 0; 1444b88c807SRodney W. Grimes usage(); 1450e8f2d6cSJordan K. Hubbard } 1464b88c807SRodney W. Grimes 1474b88c807SRodney W. Grimes checkdot(argv); 1486b419813SAndrey A. Chernov uid = geteuid(); 1494b88c807SRodney W. Grimes 1501f64b5c9SSteve Price if (*argv) { 1511f64b5c9SSteve Price stdin_ok = isatty(STDIN_FILENO); 1521f64b5c9SSteve Price 1534b88c807SRodney W. Grimes if (rflag) 1544b88c807SRodney W. Grimes rm_tree(argv); 1554b88c807SRodney W. Grimes else 1564b88c807SRodney W. Grimes rm_file(argv); 1571f64b5c9SSteve Price } 1581f64b5c9SSteve Price 1594b88c807SRodney W. Grimes exit (eval); 1604b88c807SRodney W. Grimes } 1614b88c807SRodney W. Grimes 1624b88c807SRodney W. Grimes void 16346251ddeSWarner Losh rm_tree(char **argv) 1644b88c807SRodney W. Grimes { 1654b88c807SRodney W. Grimes FTS *fts; 1664b88c807SRodney W. Grimes FTSENT *p; 1674b88c807SRodney W. Grimes int needstat; 1681f64b5c9SSteve Price int flags; 1696b419813SAndrey A. Chernov int rval; 1704b88c807SRodney W. Grimes 1714b88c807SRodney W. Grimes /* 1724b88c807SRodney W. Grimes * Remove a file hierarchy. If forcing removal (-f), or interactive 1734b88c807SRodney W. Grimes * (-i) or can't ask anyway (stdin_ok), don't stat the file. 1744b88c807SRodney W. Grimes */ 1751f64b5c9SSteve Price needstat = !uid || (!fflag && !iflag && stdin_ok); 1764b88c807SRodney W. Grimes 1774b88c807SRodney W. Grimes /* 1784b88c807SRodney W. Grimes * If the -i option is specified, the user can skip on the pre-order 1794b88c807SRodney W. Grimes * visit. The fts_number field flags skipped directories. 1804b88c807SRodney W. Grimes */ 1814b88c807SRodney W. Grimes #define SKIPPED 1 1824b88c807SRodney W. Grimes 1834a086b52SBruce Evans flags = FTS_PHYSICAL; 1841f64b5c9SSteve Price if (!needstat) 1851f64b5c9SSteve Price flags |= FTS_NOSTAT; 1861f64b5c9SSteve Price if (Wflag) 1871f64b5c9SSteve Price flags |= FTS_WHITEOUT; 188754e501cSRuslan Ermilov if (!(fts = fts_open(argv, flags, NULL))) 1894b88c807SRodney W. Grimes err(1, NULL); 1904b88c807SRodney W. Grimes while ((p = fts_read(fts)) != NULL) { 1914b88c807SRodney W. Grimes switch (p->fts_info) { 1924b88c807SRodney W. Grimes case FTS_DNR: 1934b88c807SRodney W. Grimes if (!fflag || p->fts_errno != ENOENT) { 1944b88c807SRodney W. Grimes warnx("%s: %s", 1954b88c807SRodney W. Grimes p->fts_path, strerror(p->fts_errno)); 1964b88c807SRodney W. Grimes eval = 1; 1974b88c807SRodney W. Grimes } 1984b88c807SRodney W. Grimes continue; 1994b88c807SRodney W. Grimes case FTS_ERR: 2004b88c807SRodney W. Grimes errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno)); 2014b88c807SRodney W. Grimes case FTS_NS: 2024b88c807SRodney W. Grimes /* 2034b88c807SRodney W. Grimes * FTS_NS: assume that if can't stat the file, it 2044b88c807SRodney W. Grimes * can't be unlinked. 2054b88c807SRodney W. Grimes */ 2064b88c807SRodney W. Grimes if (!needstat) 2074b88c807SRodney W. Grimes break; 2084b88c807SRodney W. Grimes if (!fflag || p->fts_errno != ENOENT) { 2094b88c807SRodney W. Grimes warnx("%s: %s", 2104b88c807SRodney W. Grimes p->fts_path, strerror(p->fts_errno)); 2114b88c807SRodney W. Grimes eval = 1; 2124b88c807SRodney W. Grimes } 2134b88c807SRodney W. Grimes continue; 2144b88c807SRodney W. Grimes case FTS_D: 2154b88c807SRodney W. Grimes /* Pre-order: give user chance to skip. */ 2161f64b5c9SSteve Price if (!fflag && !check(p->fts_path, p->fts_accpath, 2174b88c807SRodney W. Grimes p->fts_statp)) { 2184b88c807SRodney W. Grimes (void)fts_set(fts, p, FTS_SKIP); 2194b88c807SRodney W. Grimes p->fts_number = SKIPPED; 2204b88c807SRodney W. Grimes } 2216b419813SAndrey A. Chernov else if (!uid && 2226b419813SAndrey A. Chernov (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 2236b419813SAndrey A. Chernov !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 2246b419813SAndrey A. Chernov chflags(p->fts_accpath, 2256b419813SAndrey A. Chernov p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0) 2266b419813SAndrey A. Chernov goto err; 2274b88c807SRodney W. Grimes continue; 2284b88c807SRodney W. Grimes case FTS_DP: 2294b88c807SRodney W. Grimes /* Post-order: see if user skipped. */ 2304b88c807SRodney W. Grimes if (p->fts_number == SKIPPED) 2314b88c807SRodney W. Grimes continue; 2324b88c807SRodney W. Grimes break; 2331f64b5c9SSteve Price default: 2344b88c807SRodney W. Grimes if (!fflag && 2354b88c807SRodney W. Grimes !check(p->fts_path, p->fts_accpath, p->fts_statp)) 2364b88c807SRodney W. Grimes continue; 2371f64b5c9SSteve Price } 2384b88c807SRodney W. Grimes 2396b419813SAndrey A. Chernov rval = 0; 2406b419813SAndrey A. Chernov if (!uid && 2416b419813SAndrey A. Chernov (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 2426b419813SAndrey A. Chernov !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE))) 2436b419813SAndrey A. Chernov rval = chflags(p->fts_accpath, 2446b419813SAndrey A. Chernov p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)); 2450efa2040SMichael Haro if (rval == 0) { 2464b88c807SRodney W. Grimes /* 2474b88c807SRodney W. Grimes * If we can't read or search the directory, may still be 2484b88c807SRodney W. Grimes * able to remove it. Don't print out the un{read,search}able 2494b88c807SRodney W. Grimes * message unless the remove fails. 2504b88c807SRodney W. Grimes */ 2511f64b5c9SSteve Price switch (p->fts_info) { 2521f64b5c9SSteve Price case FTS_DP: 2531f64b5c9SSteve Price case FTS_DNR: 2540efa2040SMichael Haro rval = rmdir(p->fts_accpath); 2550efa2040SMichael Haro if (rval == 0 || (fflag && errno == ENOENT)) { 2560efa2040SMichael Haro if (rval == 0 && vflag) 257777d1f82SMichael Haro (void)printf("%s\n", 258b6f80a8eSDavid E. O'Brien p->fts_path); 2594b88c807SRodney W. Grimes continue; 260bfbdd545SMichael Haro } 2611f64b5c9SSteve Price break; 2621f64b5c9SSteve Price 2631f64b5c9SSteve Price case FTS_W: 2640efa2040SMichael Haro rval = undelete(p->fts_accpath); 2650efa2040SMichael Haro if (rval == 0 && (fflag && errno == ENOENT)) { 2660efa2040SMichael Haro if (vflag) 267777d1f82SMichael Haro (void)printf("%s\n", 268b6f80a8eSDavid E. O'Brien p->fts_path); 2694b88c807SRodney W. Grimes continue; 270bfbdd545SMichael Haro } 2711f64b5c9SSteve Price break; 2721f64b5c9SSteve Price 2731f64b5c9SSteve Price default: 2744b88c807SRodney W. Grimes if (Pflag) 2754b88c807SRodney W. Grimes rm_overwrite(p->fts_accpath, NULL); 2760efa2040SMichael Haro rval = unlink(p->fts_accpath); 2770efa2040SMichael Haro if (rval == 0 || (fflag && errno == ENOENT)) { 2780efa2040SMichael Haro if (rval == 0 && vflag) 279777d1f82SMichael Haro (void)printf("%s\n", 280b6f80a8eSDavid E. O'Brien p->fts_path); 2814b88c807SRodney W. Grimes continue; 2824b88c807SRodney W. Grimes } 2836b419813SAndrey A. Chernov } 284bfbdd545SMichael Haro } 2856b419813SAndrey A. Chernov err: 2864b88c807SRodney W. Grimes warn("%s", p->fts_path); 2874b88c807SRodney W. Grimes eval = 1; 2884b88c807SRodney W. Grimes } 2894b88c807SRodney W. Grimes if (errno) 2904b88c807SRodney W. Grimes err(1, "fts_read"); 2914b88c807SRodney W. Grimes } 2924b88c807SRodney W. Grimes 2934b88c807SRodney W. Grimes void 29446251ddeSWarner Losh rm_file(char **argv) 2954b88c807SRodney W. Grimes { 2964b88c807SRodney W. Grimes struct stat sb; 2971f64b5c9SSteve Price int rval; 2984b88c807SRodney W. Grimes char *f; 2994b88c807SRodney W. Grimes 3004b88c807SRodney W. Grimes /* 3014b88c807SRodney W. Grimes * Remove a file. POSIX 1003.2 states that, by default, attempting 3024b88c807SRodney W. Grimes * to remove a directory is an error, so must always stat the file. 3034b88c807SRodney W. Grimes */ 3044b88c807SRodney W. Grimes while ((f = *argv++) != NULL) { 3054b88c807SRodney W. Grimes /* Assume if can't stat the file, can't unlink it. */ 3064b88c807SRodney W. Grimes if (lstat(f, &sb)) { 3071f64b5c9SSteve Price if (Wflag) { 3081f64b5c9SSteve Price sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR; 3091f64b5c9SSteve Price } else { 3104b88c807SRodney W. Grimes if (!fflag || errno != ENOENT) { 3114b88c807SRodney W. Grimes warn("%s", f); 3124b88c807SRodney W. Grimes eval = 1; 3134b88c807SRodney W. Grimes } 3144b88c807SRodney W. Grimes continue; 3154b88c807SRodney W. Grimes } 3161f64b5c9SSteve Price } else if (Wflag) { 3171f64b5c9SSteve Price warnx("%s: %s", f, strerror(EEXIST)); 3181f64b5c9SSteve Price eval = 1; 3191f64b5c9SSteve Price continue; 3201f64b5c9SSteve Price } 3211f64b5c9SSteve Price 3221f64b5c9SSteve Price if (S_ISDIR(sb.st_mode) && !dflag) { 3234b88c807SRodney W. Grimes warnx("%s: is a directory", f); 3244b88c807SRodney W. Grimes eval = 1; 3254b88c807SRodney W. Grimes continue; 3264b88c807SRodney W. Grimes } 3271f64b5c9SSteve Price if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb)) 3284b88c807SRodney W. Grimes continue; 3296b419813SAndrey A. Chernov rval = 0; 3306b419813SAndrey A. Chernov if (!uid && 3316b419813SAndrey A. Chernov (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) && 3326b419813SAndrey A. Chernov !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE))) 3336b419813SAndrey A. Chernov rval = chflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE)); 3340efa2040SMichael Haro if (rval == 0) { 3351f64b5c9SSteve Price if (S_ISWHT(sb.st_mode)) 3361f64b5c9SSteve Price rval = undelete(f); 3371f64b5c9SSteve Price else if (S_ISDIR(sb.st_mode)) 3384b88c807SRodney W. Grimes rval = rmdir(f); 3394b88c807SRodney W. Grimes else { 3404b88c807SRodney W. Grimes if (Pflag) 3414b88c807SRodney W. Grimes rm_overwrite(f, &sb); 3424b88c807SRodney W. Grimes rval = unlink(f); 3434b88c807SRodney W. Grimes } 3446b419813SAndrey A. Chernov } 3454b88c807SRodney W. Grimes if (rval && (!fflag || errno != ENOENT)) { 3464b88c807SRodney W. Grimes warn("%s", f); 3474b88c807SRodney W. Grimes eval = 1; 3484b88c807SRodney W. Grimes } 3490efa2040SMichael Haro if (vflag && rval == 0) 350bfbdd545SMichael Haro (void)printf("%s\n", f); 3514b88c807SRodney W. Grimes } 3524b88c807SRodney W. Grimes } 3534b88c807SRodney W. Grimes 3544b88c807SRodney W. Grimes /* 3554b88c807SRodney W. Grimes * rm_overwrite -- 3564b88c807SRodney W. Grimes * Overwrite the file 3 times with varying bit patterns. 3574b88c807SRodney W. Grimes * 3584b88c807SRodney W. Grimes * XXX 3594b88c807SRodney W. Grimes * This is a cheap way to *really* delete files. Note that only regular 3604b88c807SRodney W. Grimes * files are deleted, directories (and therefore names) will remain. 3614b88c807SRodney W. Grimes * Also, this assumes a fixed-block filesystem (like FFS, or a V7 or a 3624b88c807SRodney W. Grimes * System V filesystem). In a logging filesystem, you'll have to have 3634b88c807SRodney W. Grimes * kernel support. 3644b88c807SRodney W. Grimes */ 3654b88c807SRodney W. Grimes void 36646251ddeSWarner Losh rm_overwrite(char *file, struct stat *sbp) 3674b88c807SRodney W. Grimes { 3684b88c807SRodney W. Grimes struct stat sb; 369f80db2b8SKris Kennaway struct statfs fsb; 3704b88c807SRodney W. Grimes off_t len; 371f80db2b8SKris Kennaway int bsize, fd, wlen; 372f80db2b8SKris Kennaway char *buf = NULL; 3734b88c807SRodney W. Grimes 3744b88c807SRodney W. Grimes fd = -1; 3754b88c807SRodney W. Grimes if (sbp == NULL) { 3764b88c807SRodney W. Grimes if (lstat(file, &sb)) 3774b88c807SRodney W. Grimes goto err; 3784b88c807SRodney W. Grimes sbp = &sb; 3794b88c807SRodney W. Grimes } 3804b88c807SRodney W. Grimes if (!S_ISREG(sbp->st_mode)) 3814b88c807SRodney W. Grimes return; 3824b88c807SRodney W. Grimes if ((fd = open(file, O_WRONLY, 0)) == -1) 3834b88c807SRodney W. Grimes goto err; 384f80db2b8SKris Kennaway if (fstatfs(fd, &fsb) == -1) 385f80db2b8SKris Kennaway goto err; 386f80db2b8SKris Kennaway bsize = MAX(fsb.f_iosize, 1024); 387f80db2b8SKris Kennaway if ((buf = malloc(bsize)) == NULL) 388f80db2b8SKris Kennaway err(1, "malloc"); 3894b88c807SRodney W. Grimes 3904b88c807SRodney W. Grimes #define PASS(byte) { \ 391f80db2b8SKris Kennaway memset(buf, byte, bsize); \ 3924b88c807SRodney W. Grimes for (len = sbp->st_size; len > 0; len -= wlen) { \ 393f80db2b8SKris Kennaway wlen = len < bsize ? len : bsize; \ 3944b88c807SRodney W. Grimes if (write(fd, buf, wlen) != wlen) \ 3954b88c807SRodney W. Grimes goto err; \ 3964b88c807SRodney W. Grimes } \ 3974b88c807SRodney W. Grimes } 3984b88c807SRodney W. Grimes PASS(0xff); 3994b88c807SRodney W. Grimes if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) 4004b88c807SRodney W. Grimes goto err; 4014b88c807SRodney W. Grimes PASS(0x00); 4024b88c807SRodney W. Grimes if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) 4034b88c807SRodney W. Grimes goto err; 4044b88c807SRodney W. Grimes PASS(0xff); 405f80db2b8SKris Kennaway if (!fsync(fd) && !close(fd)) { 406f80db2b8SKris Kennaway free(buf); 4074b88c807SRodney W. Grimes return; 408f80db2b8SKris Kennaway } 4094b88c807SRodney W. Grimes 4104b88c807SRodney W. Grimes err: eval = 1; 411f80db2b8SKris Kennaway if (buf) 412f80db2b8SKris Kennaway free(buf); 4134b88c807SRodney W. Grimes warn("%s", file); 4144b88c807SRodney W. Grimes } 4154b88c807SRodney W. Grimes 4164b88c807SRodney W. Grimes 4174b88c807SRodney W. Grimes int 41846251ddeSWarner Losh check(char *path, char *name, struct stat *sp) 4194b88c807SRodney W. Grimes { 4204b88c807SRodney W. Grimes int ch, first; 421141d77b8SJosef Karthauser char modep[15], *flagsp; 4224b88c807SRodney W. Grimes 4234b88c807SRodney W. Grimes /* Check -i first. */ 4244b88c807SRodney W. Grimes if (iflag) 4254b88c807SRodney W. Grimes (void)fprintf(stderr, "remove %s? ", path); 4264b88c807SRodney W. Grimes else { 4274b88c807SRodney W. Grimes /* 4284b88c807SRodney W. Grimes * If it's not a symbolic link and it's unwritable and we're 4294b88c807SRodney W. Grimes * talking to a terminal, ask. Symbolic links are excluded 4304b88c807SRodney W. Grimes * because their permissions are meaningless. Check stdin_ok 4314b88c807SRodney W. Grimes * first because we may not have stat'ed the file. 4324b88c807SRodney W. Grimes */ 4336b419813SAndrey A. Chernov if (!stdin_ok || S_ISLNK(sp->st_mode) || 4341f64b5c9SSteve Price (!access(name, W_OK) && 4356b419813SAndrey A. Chernov !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 4361f64b5c9SSteve Price (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid))) 4374b88c807SRodney W. Grimes return (1); 4384b88c807SRodney W. Grimes strmode(sp->st_mode, modep); 439141d77b8SJosef Karthauser if ((flagsp = fflagstostr(sp->st_flags)) == NULL) 440141d77b8SJosef Karthauser err(1, NULL); 441141d77b8SJosef Karthauser (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ", 4424b88c807SRodney W. Grimes modep + 1, modep[9] == ' ' ? "" : " ", 4434b88c807SRodney W. Grimes user_from_uid(sp->st_uid, 0), 4446b419813SAndrey A. Chernov group_from_gid(sp->st_gid, 0), 445141d77b8SJosef Karthauser *flagsp ? flagsp : "", *flagsp ? " " : "", 4466b419813SAndrey A. Chernov path); 447141d77b8SJosef Karthauser free(flagsp); 4484b88c807SRodney W. Grimes } 4494b88c807SRodney W. Grimes (void)fflush(stderr); 4504b88c807SRodney W. Grimes 4514b88c807SRodney W. Grimes first = ch = getchar(); 4524b88c807SRodney W. Grimes while (ch != '\n' && ch != EOF) 4534b88c807SRodney W. Grimes ch = getchar(); 454b0205affSWolfram Schneider return (first == 'y' || first == 'Y'); 4554b88c807SRodney W. Grimes } 4564b88c807SRodney W. Grimes 45767a3d3a8SPoul-Henning Kamp #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2]))) 4584b88c807SRodney W. Grimes void 45946251ddeSWarner Losh checkdot(char **argv) 4604b88c807SRodney W. Grimes { 4614b88c807SRodney W. Grimes char *p, **save, **t; 4624b88c807SRodney W. Grimes int complained; 4634b88c807SRodney W. Grimes 4644b88c807SRodney W. Grimes complained = 0; 4654b88c807SRodney W. Grimes for (t = argv; *t;) { 4664b88c807SRodney W. Grimes if ((p = strrchr(*t, '/')) != NULL) 4674b88c807SRodney W. Grimes ++p; 4684b88c807SRodney W. Grimes else 4694b88c807SRodney W. Grimes p = *t; 4704b88c807SRodney W. Grimes if (ISDOT(p)) { 4714b88c807SRodney W. Grimes if (!complained++) 4724b88c807SRodney W. Grimes warnx("\".\" and \"..\" may not be removed"); 4734b88c807SRodney W. Grimes eval = 1; 4741f64b5c9SSteve Price for (save = t; (t[0] = t[1]) != NULL; ++t) 4751f64b5c9SSteve Price continue; 4764b88c807SRodney W. Grimes t = save; 4774b88c807SRodney W. Grimes } else 4784b88c807SRodney W. Grimes ++t; 4794b88c807SRodney W. Grimes } 4804b88c807SRodney W. Grimes } 4814b88c807SRodney W. Grimes 4824b88c807SRodney W. Grimes void 48346251ddeSWarner Losh usage(void) 4844b88c807SRodney W. Grimes { 4850efa2040SMichael Haro 486d71e172aSSheldon Hearn (void)fprintf(stderr, "%s\n%s\n", 487d71e172aSSheldon Hearn "usage: rm [-f | -i] [-dPRrvW] file ...", 488d71e172aSSheldon Hearn " unlink file"); 489bfbdd545SMichael Haro exit(EX_USAGE); 4904b88c807SRodney W. Grimes } 491