rm.c revision 1.5
1/* $OpenBSD: rm.c,v 1.5 2015/11/17 19:11:11 tedu Exp $ */ 2/* $NetBSD: rm.c,v 1.19 1995/09/07 06:48:50 jtc Exp $ */ 3 4/*- 5 * Copyright (c) 1990, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33#include <sys/types.h> 34#include <sys/stat.h> 35#include <sys/mount.h> 36 37#include <err.h> 38#include <errno.h> 39#include <fcntl.h> 40#include <fts.h> 41#include <stdio.h> 42#include <stdlib.h> 43#include <string.h> 44#include <unistd.h> 45#include <limits.h> 46#include <pwd.h> 47#include <grp.h> 48 49#define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b)) 50 51extern char *__progname; 52 53static int eval, stdin_ok; 54 55static int check(char *, char *, struct stat *); 56static void checkdot(char **); 57static void rm_file(char **); 58static void rm_tree(char **); 59 60static void __dead 61usage(void) 62{ 63 (void)fprintf(stderr, "usage: %s [-dfiPRr] file ...\n", __progname); 64 exit(1); 65} 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 */ 74int 75rmmain(int argc, char *argv[]) 76{ 77 if (pledge("stdio rpath cpath", NULL) == -1) 78 err(1, "pledge"); 79 80 checkdot(argv); 81 82 if (*argv) { 83 stdin_ok = isatty(STDIN_FILENO); 84 85 rm_tree(argv); 86 } 87 88 return (eval); 89} 90 91static void 92rm_tree(char **argv) 93{ 94 FTS *fts; 95 FTSENT *p; 96 int flags; 97 98 /* 99 * If the -i option is specified, the user can skip on the pre-order 100 * visit. The fts_number field flags skipped directories. 101 */ 102#define SKIPPED 1 103 104 flags = FTS_PHYSICAL; 105 flags |= FTS_NOSTAT; 106 if (!(fts = fts_open(argv, flags, NULL))) 107 err(1, NULL); 108 while ((p = fts_read(fts)) != NULL) { 109 switch (p->fts_info) { 110 case FTS_DNR: 111 if (p->fts_errno != ENOENT) { 112 warnx("%s: %s", 113 p->fts_path, strerror(p->fts_errno)); 114 eval = 1; 115 } 116 continue; 117 case FTS_ERR: 118 errc(1, p->fts_errno, "%s", p->fts_path); 119 case FTS_NS: 120 /* 121 * FTS_NS: assume that if can't stat the file, it 122 * can't be unlinked. 123 */ 124 break; 125 case FTS_D: 126 /* Pre-order: give user chance to skip. */ 127 continue; 128 case FTS_DP: 129 /* Post-order: see if user skipped. */ 130 if (p->fts_number == SKIPPED) 131 continue; 132 break; 133 default: 134 break; 135 } 136 137 /* 138 * If we can't read or search the directory, may still be 139 * able to remove it. Don't print out the un{read,search}able 140 * message unless the remove fails. 141 */ 142 switch (p->fts_info) { 143 case FTS_DP: 144 case FTS_DNR: 145 if (!rmdir(p->fts_accpath) || 146 (errno == ENOENT)) 147 continue; 148 break; 149 150 case FTS_F: 151 case FTS_NSOK: 152 default: 153 if (!unlink(p->fts_accpath) || 154 (errno == ENOENT)) 155 continue; 156 } 157 warn("%s", p->fts_path); 158 eval = 1; 159 } 160 if (errno) 161 err(1, "fts_read"); 162 fts_close(fts); 163} 164 165static void 166rm_file(char **argv) 167{ 168 struct stat sb; 169 int rval; 170 char *f; 171 172 /* 173 * Remove a file. POSIX 1003.2 states that, by default, attempting 174 * to remove a directory is an error, so must always stat the file. 175 */ 176 while ((f = *argv++) != NULL) { 177 /* Assume if can't stat the file, can't unlink it. */ 178 if (lstat(f, &sb)) { 179 if (errno != ENOENT) { 180 warn("%s", f); 181 eval = 1; 182 } 183 continue; 184 } 185 186 if (S_ISDIR(sb.st_mode)) { 187 warnx("%s: is a directory", f); 188 eval = 1; 189 continue; 190 } 191 if (S_ISDIR(sb.st_mode)) 192 rval = rmdir(f); 193 else { 194 rval = unlink(f); 195 } 196 if (rval && (errno != ENOENT)) { 197 warn("%s", f); 198 eval = 1; 199 } 200 } 201} 202 203static int 204check(char *path, char *name, struct stat *sp) 205{ 206 int ch, first; 207 char modep[15]; 208 209 /* 210 * If it's not a symbolic link and it's unwritable and we're 211 * talking to a terminal, ask. Symbolic links are excluded 212 * because their permissions are meaningless. Check stdin_ok 213 * first because we may not have stat'ed the file. 214 */ 215 if (!stdin_ok || S_ISLNK(sp->st_mode) || !access(name, W_OK) || 216 errno != EACCES) 217 return (1); 218 strmode(sp->st_mode, modep); 219 (void)fprintf(stderr, "override %s%s%s/%s for %s? ", 220 modep + 1, modep[9] == ' ' ? "" : " ", 221 user_from_uid(sp->st_uid, 0), 222 group_from_gid(sp->st_gid, 0), path); 223 (void)fflush(stderr); 224 225 first = ch = getchar(); 226 while (ch != '\n' && ch != EOF) 227 ch = getchar(); 228 return (first == 'y' || first == 'Y'); 229} 230 231/* 232 * POSIX.2 requires that if "." or ".." are specified as the basename 233 * portion of an operand, a diagnostic message be written to standard 234 * error and nothing more be done with such operands. 235 * 236 * Since POSIX.2 defines basename as the final portion of a path after 237 * trailing slashes have been removed, we'll remove them here. 238 */ 239#define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2]))) 240static void 241checkdot(char **argv) 242{ 243 char *p, **save, **t; 244 int complained; 245 246 complained = 0; 247 for (t = argv; *t;) { 248 /* strip trailing slashes */ 249 p = strrchr (*t, '\0'); 250 while (--p > *t && *p == '/') 251 *p = '\0'; 252 253 /* extract basename */ 254 if ((p = strrchr(*t, '/')) != NULL) 255 ++p; 256 else 257 p = *t; 258 259 if (ISDOT(p)) { 260 if (!complained++) 261 warnx("\".\" and \"..\" may not be removed"); 262 eval = 1; 263 for (save = t; (t[0] = t[1]) != NULL; ++t) 264 continue; 265 t = save; 266 } else 267 ++t; 268 } 269} 270