rm.c revision 1.8
1/* $OpenBSD: rm.c,v 1.8 2016/10/10 18:07:03 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 void checkdot(char **); 56static void rm_tree(char **); 57 58static void __dead 59usage(void) 60{ 61 (void)fprintf(stderr, "usage: %s [-dfiPRr] file ...\n", __progname); 62 exit(1); 63} 64 65/* 66 * rm -- 67 * This rm is different from historic rm's, but is expected to match 68 * POSIX 1003.2 behavior. The most visible difference is that -f 69 * has two specific effects now, ignore non-existent files and force 70 * file removal. 71 */ 72int 73rmmain(int argc, char *argv[]) 74{ 75 76 checkdot(argv); 77 78 if (*argv) { 79 stdin_ok = isatty(STDIN_FILENO); 80 81 rm_tree(argv); 82 } 83 84 return (eval); 85} 86 87static void 88rm_tree(char **argv) 89{ 90 FTS *fts; 91 FTSENT *p; 92 int flags; 93 94 /* 95 * If the -i option is specified, the user can skip on the pre-order 96 * visit. The fts_number field flags skipped directories. 97 */ 98#define SKIPPED 1 99 100 flags = FTS_PHYSICAL; 101 flags |= FTS_NOSTAT; 102 if (!(fts = fts_open(argv, flags, NULL))) 103 err(1, NULL); 104 while ((p = fts_read(fts)) != NULL) { 105 switch (p->fts_info) { 106 case FTS_DNR: 107 if (p->fts_errno != ENOENT) { 108 warnx("%s: %s", 109 p->fts_path, strerror(p->fts_errno)); 110 eval = 1; 111 } 112 continue; 113 case FTS_ERR: 114 errc(1, p->fts_errno, "%s", p->fts_path); 115 case FTS_NS: 116 /* 117 * FTS_NS: assume that if can't stat the file, it 118 * can't be unlinked. 119 */ 120 break; 121 case FTS_D: 122 /* Pre-order: give user chance to skip. */ 123 continue; 124 case FTS_DP: 125 /* Post-order: see if user skipped. */ 126 if (p->fts_number == SKIPPED) 127 continue; 128 break; 129 default: 130 break; 131 } 132 133 /* 134 * If we can't read or search the directory, may still be 135 * able to remove it. Don't print out the un{read,search}able 136 * message unless the remove fails. 137 */ 138 switch (p->fts_info) { 139 case FTS_DP: 140 case FTS_DNR: 141 if (!rmdir(p->fts_accpath) || 142 (errno == ENOENT)) 143 continue; 144 break; 145 146 case FTS_F: 147 case FTS_NSOK: 148 default: 149 if (!unlink(p->fts_accpath) || 150 (errno == ENOENT)) 151 continue; 152 } 153 warn("%s", p->fts_path); 154 eval = 1; 155 } 156 if (errno) 157 err(1, "fts_read"); 158 fts_close(fts); 159} 160 161/* 162 * POSIX.2 requires that if "." or ".." are specified as the basename 163 * portion of an operand, a diagnostic message be written to standard 164 * error and nothing more be done with such operands. 165 * 166 * Since POSIX.2 defines basename as the final portion of a path after 167 * trailing slashes have been removed, we'll remove them here. 168 */ 169#define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2]))) 170static void 171checkdot(char **argv) 172{ 173 char *p, **save, **t; 174 int complained; 175 176 complained = 0; 177 for (t = argv; *t;) { 178 /* strip trailing slashes */ 179 p = strrchr (*t, '\0'); 180 while (--p > *t && *p == '/') 181 *p = '\0'; 182 183 /* extract basename */ 184 if ((p = strrchr(*t, '/')) != NULL) 185 ++p; 186 else 187 p = *t; 188 189 if (ISDOT(p)) { 190 if (!complained++) 191 warnx("\".\" and \"..\" may not be removed"); 192 eval = 1; 193 for (save = t; (t[0] = t[1]) != NULL; ++t) 194 continue; 195 t = save; 196 } else 197 ++t; 198 } 199} 200