1 /*- 2 * Copyright (c) 1990, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * $Id$ 34 */ 35 36 #ifndef lint 37 static char copyright[] = 38 "@(#) Copyright (c) 1990, 1993, 1994\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 static char sccsid[] = "@(#)rm.c 8.5 (Berkeley) 4/18/94"; 44 #endif /* not lint */ 45 46 #include <sys/types.h> 47 #include <sys/stat.h> 48 49 #include <err.h> 50 #include <errno.h> 51 #include <fcntl.h> 52 #include <fts.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <unistd.h> 57 58 int dflag, eval, fflag, iflag, Pflag, stdin_ok; 59 60 int check __P((char *, char *, struct stat *)); 61 void checkdot __P((char **)); 62 void rm_file __P((char **)); 63 void rm_overwrite __P((char *, struct stat *)); 64 void rm_tree __P((char **)); 65 void usage __P((void)); 66 67 /* 68 * rm -- 69 * This rm is different from historic rm's, but is expected to match 70 * POSIX 1003.2 behavior. The most visible difference is that -f 71 * has two specific effects now, ignore non-existent files and force 72 * file removal. 73 */ 74 int 75 main(argc, argv) 76 int argc; 77 char *argv[]; 78 { 79 int ch, rflag; 80 81 Pflag = rflag = 0; 82 while ((ch = getopt(argc, argv, "dfiPRr")) != EOF) 83 switch(ch) { 84 case 'd': 85 dflag = 1; 86 break; 87 case 'f': 88 fflag = 1; 89 iflag = 0; 90 break; 91 case 'i': 92 fflag = 0; 93 iflag = 1; 94 break; 95 case 'P': 96 Pflag = 1; 97 break; 98 case 'R': 99 case 'r': /* Compatibility. */ 100 rflag = 1; 101 break; 102 case '?': 103 default: 104 usage(); 105 } 106 argc -= optind; 107 argv += optind; 108 109 if (argc < 1) 110 usage(); 111 112 checkdot(argv); 113 if (!*argv) 114 exit (eval); 115 116 stdin_ok = isatty(STDIN_FILENO); 117 118 if (rflag) 119 rm_tree(argv); 120 else 121 rm_file(argv); 122 exit (eval); 123 } 124 125 void 126 rm_tree(argv) 127 char **argv; 128 { 129 FTS *fts; 130 FTSENT *p; 131 int needstat; 132 133 /* 134 * Remove a file hierarchy. If forcing removal (-f), or interactive 135 * (-i) or can't ask anyway (stdin_ok), don't stat the file. 136 */ 137 needstat = !fflag && !iflag && stdin_ok; 138 139 /* 140 * If the -i option is specified, the user can skip on the pre-order 141 * visit. The fts_number field flags skipped directories. 142 */ 143 #define SKIPPED 1 144 145 if (!(fts = fts_open(argv, 146 needstat ? FTS_PHYSICAL|FTS_NOCHDIR : 147 FTS_PHYSICAL|FTS_NOSTAT|FTS_NOCHDIR, (int (*)())NULL))) 148 err(1, NULL); 149 while ((p = fts_read(fts)) != NULL) { 150 switch (p->fts_info) { 151 case FTS_DNR: 152 if (!fflag || p->fts_errno != ENOENT) { 153 warnx("%s: %s", 154 p->fts_path, strerror(p->fts_errno)); 155 eval = 1; 156 } 157 continue; 158 case FTS_ERR: 159 errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno)); 160 case FTS_NS: 161 /* 162 * FTS_NS: assume that if can't stat the file, it 163 * can't be unlinked. 164 */ 165 if (!needstat) 166 break; 167 if (!fflag || p->fts_errno != ENOENT) { 168 warnx("%s: %s", 169 p->fts_path, strerror(p->fts_errno)); 170 eval = 1; 171 } 172 continue; 173 case FTS_D: 174 /* Pre-order: give user chance to skip. */ 175 if (iflag && !check(p->fts_path, p->fts_accpath, 176 p->fts_statp)) { 177 (void)fts_set(fts, p, FTS_SKIP); 178 p->fts_number = SKIPPED; 179 } 180 continue; 181 case FTS_DP: 182 /* Post-order: see if user skipped. */ 183 if (p->fts_number == SKIPPED) 184 continue; 185 break; 186 } 187 if (!fflag && 188 !check(p->fts_path, p->fts_accpath, p->fts_statp)) 189 continue; 190 191 /* 192 * If we can't read or search the directory, may still be 193 * able to remove it. Don't print out the un{read,search}able 194 * message unless the remove fails. 195 */ 196 if (p->fts_info == FTS_DP || p->fts_info == FTS_DNR) { 197 if (!rmdir(p->fts_accpath)) 198 continue; 199 if (errno == ENOENT) { 200 if (fflag) 201 continue; 202 } else if (p->fts_info != FTS_DP) 203 warnx("%s: unable to read", p->fts_path); 204 } else { 205 if (Pflag) 206 rm_overwrite(p->fts_accpath, NULL); 207 if (!unlink(p->fts_accpath) || (fflag && errno == ENOENT)) 208 continue; 209 } 210 warn("%s", p->fts_path); 211 eval = 1; 212 } 213 if (errno) 214 err(1, "fts_read"); 215 } 216 217 void 218 rm_file(argv) 219 char **argv; 220 { 221 struct stat sb; 222 int df, rval; 223 char *f; 224 225 df = dflag; 226 /* 227 * Remove a file. POSIX 1003.2 states that, by default, attempting 228 * to remove a directory is an error, so must always stat the file. 229 */ 230 while ((f = *argv++) != NULL) { 231 /* Assume if can't stat the file, can't unlink it. */ 232 if (lstat(f, &sb)) { 233 if (!fflag || errno != ENOENT) { 234 warn("%s", f); 235 eval = 1; 236 } 237 continue; 238 } 239 if (S_ISDIR(sb.st_mode) && !df) { 240 warnx("%s: is a directory", f); 241 eval = 1; 242 continue; 243 } 244 if (!fflag && !check(f, f, &sb)) 245 continue; 246 if (S_ISDIR(sb.st_mode)) 247 rval = rmdir(f); 248 else { 249 if (Pflag) 250 rm_overwrite(f, &sb); 251 rval = unlink(f); 252 } 253 if (rval && (!fflag || errno != ENOENT)) { 254 warn("%s", f); 255 eval = 1; 256 } 257 } 258 } 259 260 /* 261 * rm_overwrite -- 262 * Overwrite the file 3 times with varying bit patterns. 263 * 264 * XXX 265 * This is a cheap way to *really* delete files. Note that only regular 266 * files are deleted, directories (and therefore names) will remain. 267 * Also, this assumes a fixed-block file system (like FFS, or a V7 or a 268 * System V file system). In a logging file system, you'll have to have 269 * kernel support. 270 */ 271 void 272 rm_overwrite(file, sbp) 273 char *file; 274 struct stat *sbp; 275 { 276 struct stat sb; 277 off_t len; 278 int fd, wlen; 279 char buf[8 * 1024]; 280 281 fd = -1; 282 if (sbp == NULL) { 283 if (lstat(file, &sb)) 284 goto err; 285 sbp = &sb; 286 } 287 if (!S_ISREG(sbp->st_mode)) 288 return; 289 if ((fd = open(file, O_WRONLY, 0)) == -1) 290 goto err; 291 292 #define PASS(byte) { \ 293 memset(buf, byte, sizeof(buf)); \ 294 for (len = sbp->st_size; len > 0; len -= wlen) { \ 295 wlen = len < sizeof(buf) ? len : sizeof(buf); \ 296 if (write(fd, buf, wlen) != wlen) \ 297 goto err; \ 298 } \ 299 } 300 PASS(0xff); 301 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) 302 goto err; 303 PASS(0x00); 304 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) 305 goto err; 306 PASS(0xff); 307 if (!fsync(fd) && !close(fd)) 308 return; 309 310 err: eval = 1; 311 warn("%s", file); 312 } 313 314 315 int 316 check(path, name, sp) 317 char *path, *name; 318 struct stat *sp; 319 { 320 int ch, first; 321 char modep[15]; 322 323 /* Check -i first. */ 324 if (iflag) 325 (void)fprintf(stderr, "remove %s? ", path); 326 else { 327 /* 328 * If it's not a symbolic link and it's unwritable and we're 329 * talking to a terminal, ask. Symbolic links are excluded 330 * because their permissions are meaningless. Check stdin_ok 331 * first because we may not have stat'ed the file. 332 */ 333 if (!stdin_ok || S_ISLNK(sp->st_mode) || !access(name, W_OK)) 334 return (1); 335 strmode(sp->st_mode, modep); 336 (void)fprintf(stderr, "override %s%s%s/%s for %s? ", 337 modep + 1, modep[9] == ' ' ? "" : " ", 338 user_from_uid(sp->st_uid, 0), 339 group_from_gid(sp->st_gid, 0), path); 340 } 341 (void)fflush(stderr); 342 343 first = ch = getchar(); 344 while (ch != '\n' && ch != EOF) 345 ch = getchar(); 346 return (first == 'y'); 347 } 348 349 #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2]))) 350 void 351 checkdot(argv) 352 char **argv; 353 { 354 char *p, **save, **t; 355 int complained; 356 357 complained = 0; 358 for (t = argv; *t;) { 359 if ((p = strrchr(*t, '/')) != NULL) 360 ++p; 361 else 362 p = *t; 363 if (ISDOT(p)) { 364 if (!complained++) 365 warnx("\".\" and \"..\" may not be removed"); 366 eval = 1; 367 for (save = t; (t[0] = t[1]) != NULL; ++t); 368 t = save; 369 } else 370 ++t; 371 } 372 } 373 374 void 375 usage() 376 { 377 378 (void)fprintf(stderr, "usage: rm [-dfiRr] file ...\n"); 379 exit(1); 380 } 381