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