| 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 | * 4. Neither the name of the University nor the names of its contributors
|
|---|
| 14 | * may be used to endorse or promote products derived from this software
|
|---|
| 15 | * without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|---|
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|---|
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|---|
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|---|
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|---|
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|---|
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|---|
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|---|
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|---|
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|---|
| 27 | * SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | #if 0
|
|---|
| 31 | #ifndef lint
|
|---|
| 32 | static const char copyright[] =
|
|---|
| 33 | "@(#) Copyright (c) 1990, 1993, 1994\n\
|
|---|
| 34 | The Regents of the University of California. All rights reserved.\n";
|
|---|
| 35 | #endif /* not lint */
|
|---|
| 36 |
|
|---|
| 37 | #ifndef lint
|
|---|
| 38 | static char sccsid[] = "@(#)rm.c 8.5 (Berkeley) 4/18/94";
|
|---|
| 39 | #endif /* not lint */
|
|---|
| 40 | #endif
|
|---|
| 41 | #include <sys/cdefs.h>
|
|---|
| 42 | //__FBSDID("$FreeBSD: src/bin/rm/rm.c,v 1.47 2004/04/06 20:06:50 markm Exp $");
|
|---|
| 43 |
|
|---|
| 44 | #include <sys/stat.h>
|
|---|
| 45 | #include <sys/param.h>
|
|---|
| 46 | #include <sys/mount.h>
|
|---|
| 47 |
|
|---|
| 48 | #include <err.h>
|
|---|
| 49 | #include <errno.h>
|
|---|
| 50 | #include <fcntl.h>
|
|---|
| 51 | #include <fts.h>
|
|---|
| 52 | #include <grp.h>
|
|---|
| 53 | #include <pwd.h>
|
|---|
| 54 | #include <stdio.h>
|
|---|
| 55 | #include <stdlib.h>
|
|---|
| 56 | #include <string.h>
|
|---|
| 57 | #include <sysexits.h>
|
|---|
| 58 | #include <unistd.h>
|
|---|
| 59 |
|
|---|
| 60 | #ifdef __EMX__
|
|---|
| 61 | #undef S_IFWHT
|
|---|
| 62 | #undef S_ISWHT
|
|---|
| 63 | #endif
|
|---|
| 64 | #ifndef S_IFWHT
|
|---|
| 65 | #define S_IFWHT 0
|
|---|
| 66 | #define S_ISWHT(s) 0
|
|---|
| 67 | #define undelete(s) (-1)
|
|---|
| 68 | #endif
|
|---|
| 69 |
|
|---|
| 70 | #ifndef __FreeBSD__
|
|---|
| 71 | extern void strmode(mode_t mode, char *p);
|
|---|
| 72 | #endif
|
|---|
| 73 |
|
|---|
| 74 | static int dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok;
|
|---|
| 75 | static uid_t uid;
|
|---|
| 76 |
|
|---|
| 77 | static char *argv0;
|
|---|
| 78 |
|
|---|
| 79 | static int check(char *, char *, struct stat *);
|
|---|
| 80 | static void checkdot(char **);
|
|---|
| 81 | static void rm_file(char **);
|
|---|
| 82 | static int rm_overwrite(char *, struct stat *);
|
|---|
| 83 | static void rm_tree(char **);
|
|---|
| 84 | static int usage(void);
|
|---|
| 85 |
|
|---|
| 86 | /*
|
|---|
| 87 | * rm --
|
|---|
| 88 | * This rm is different from historic rm's, but is expected to match
|
|---|
| 89 | * POSIX 1003.2 behavior. The most visible difference is that -f
|
|---|
| 90 | * has two specific effects now, ignore non-existent files and force
|
|---|
| 91 | * file removal.
|
|---|
| 92 | */
|
|---|
| 93 | int
|
|---|
| 94 | kmk_builtin_rm(int argc, char *argv[])
|
|---|
| 95 | {
|
|---|
| 96 | int ch, rflag;
|
|---|
| 97 | char *p;
|
|---|
| 98 |
|
|---|
| 99 | argv0 = argv[0];
|
|---|
| 100 | dflag = eval = fflag = iflag = Pflag = vflag = Wflag = stdin_ok = 0;
|
|---|
| 101 | uid = 0;
|
|---|
| 102 | opterr = 1;
|
|---|
| 103 | optarg = NULL;
|
|---|
| 104 | optopt = 0;
|
|---|
| 105 | #if defined(__FreeBSD__) || defined(__EMX__)
|
|---|
| 106 | optreset = 1;
|
|---|
| 107 | optind = 1;
|
|---|
| 108 | #else
|
|---|
| 109 | optind = 0; /* init */
|
|---|
| 110 | #endif
|
|---|
| 111 |
|
|---|
| 112 | /*
|
|---|
| 113 | * Test for the special case where the utility is called as
|
|---|
| 114 | * "unlink", for which the functionality provided is greatly
|
|---|
| 115 | * simplified.
|
|---|
| 116 | */
|
|---|
| 117 | if ((p = rindex(argv[0], '/')) == NULL)
|
|---|
| 118 | p = argv[0];
|
|---|
| 119 | else
|
|---|
| 120 | ++p;
|
|---|
| 121 | if (strcmp(p, "unlink") == 0) {
|
|---|
| 122 | while (getopt(argc, argv, "") != -1)
|
|---|
| 123 | return usage();
|
|---|
| 124 | argc -= optind;
|
|---|
| 125 | argv += optind;
|
|---|
| 126 | if (argc != 1)
|
|---|
| 127 | return usage();
|
|---|
| 128 | rm_file(&argv[0]);
|
|---|
| 129 | return eval;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | Pflag = rflag = 0;
|
|---|
| 133 | while ((ch = getopt(argc, argv, "dfiPRrvW")) != -1)
|
|---|
| 134 | switch(ch) {
|
|---|
| 135 | case 'd':
|
|---|
| 136 | dflag = 1;
|
|---|
| 137 | break;
|
|---|
| 138 | case 'f':
|
|---|
| 139 | fflag = 1;
|
|---|
| 140 | iflag = 0;
|
|---|
| 141 | break;
|
|---|
| 142 | case 'i':
|
|---|
| 143 | fflag = 0;
|
|---|
| 144 | iflag = 1;
|
|---|
| 145 | break;
|
|---|
| 146 | case 'P':
|
|---|
| 147 | Pflag = 1;
|
|---|
| 148 | break;
|
|---|
| 149 | case 'R':
|
|---|
| 150 | case 'r': /* Compatibility. */
|
|---|
| 151 | rflag = 1;
|
|---|
| 152 | break;
|
|---|
| 153 | case 'v':
|
|---|
| 154 | vflag = 1;
|
|---|
| 155 | break;
|
|---|
| 156 | #ifdef FTS_WHITEOUT
|
|---|
| 157 | case 'W':
|
|---|
| 158 | Wflag = 1;
|
|---|
| 159 | break;
|
|---|
| 160 | #endif
|
|---|
| 161 | default:
|
|---|
| 162 | return usage();
|
|---|
| 163 | }
|
|---|
| 164 | argc -= optind;
|
|---|
| 165 | argv += optind;
|
|---|
| 166 |
|
|---|
| 167 | if (argc < 1) {
|
|---|
| 168 | if (fflag)
|
|---|
| 169 | return (0);
|
|---|
| 170 | return usage();
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | checkdot(argv);
|
|---|
| 174 | uid = geteuid();
|
|---|
| 175 |
|
|---|
| 176 | if (*argv) {
|
|---|
| 177 | stdin_ok = isatty(STDIN_FILENO);
|
|---|
| 178 |
|
|---|
| 179 | if (rflag)
|
|---|
| 180 | rm_tree(argv);
|
|---|
| 181 | else
|
|---|
| 182 | rm_file(argv);
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | return eval;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | static void
|
|---|
| 189 | rm_tree(char **argv)
|
|---|
| 190 | {
|
|---|
| 191 | FTS *fts;
|
|---|
| 192 | FTSENT *p;
|
|---|
| 193 | int needstat;
|
|---|
| 194 | int flags;
|
|---|
| 195 | int rval;
|
|---|
| 196 |
|
|---|
| 197 | /*
|
|---|
| 198 | * Remove a file hierarchy. If forcing removal (-f), or interactive
|
|---|
| 199 | * (-i) or can't ask anyway (stdin_ok), don't stat the file.
|
|---|
| 200 | */
|
|---|
| 201 | needstat = !uid || (!fflag && !iflag && stdin_ok);
|
|---|
| 202 |
|
|---|
| 203 | /*
|
|---|
| 204 | * If the -i option is specified, the user can skip on the pre-order
|
|---|
| 205 | * visit. The fts_number field flags skipped directories.
|
|---|
| 206 | */
|
|---|
| 207 | #define SKIPPED 1
|
|---|
| 208 |
|
|---|
| 209 | flags = FTS_PHYSICAL;
|
|---|
| 210 | if (!needstat)
|
|---|
| 211 | flags |= FTS_NOSTAT;
|
|---|
| 212 | #ifdef FTS_WHITEOUT
|
|---|
| 213 | if (Wflag)
|
|---|
| 214 | flags |= FTS_WHITEOUT;
|
|---|
| 215 | #endif
|
|---|
| 216 | if (!(fts = fts_open(argv, flags, NULL)))
|
|---|
| 217 | err(1, "fts_open");
|
|---|
| 218 | while ((p = fts_read(fts)) != NULL) {
|
|---|
| 219 | switch (p->fts_info) {
|
|---|
| 220 | case FTS_DNR:
|
|---|
| 221 | if (!fflag || p->fts_errno != ENOENT) {
|
|---|
| 222 | fprintf(stderr, "%s: %s: %s\n",
|
|---|
| 223 | argv0, p->fts_path, strerror(p->fts_errno));
|
|---|
| 224 | eval = 1;
|
|---|
| 225 | }
|
|---|
| 226 | continue;
|
|---|
| 227 | case FTS_ERR:
|
|---|
| 228 | errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
|
|---|
| 229 | case FTS_NS:
|
|---|
| 230 | /*
|
|---|
| 231 | * Assume that since fts_read() couldn't stat the
|
|---|
| 232 | * file, it can't be unlinked.
|
|---|
| 233 | */
|
|---|
| 234 | if (!needstat)
|
|---|
| 235 | break;
|
|---|
| 236 | if (!fflag || p->fts_errno != ENOENT) {
|
|---|
| 237 | fprintf(stderr, "%s: %s: %s\n",
|
|---|
| 238 | argv0, p->fts_path, strerror(p->fts_errno));
|
|---|
| 239 | eval = 1;
|
|---|
| 240 | }
|
|---|
| 241 | continue;
|
|---|
| 242 | case FTS_D:
|
|---|
| 243 | /* Pre-order: give user chance to skip. */
|
|---|
| 244 | if (!fflag && !check(p->fts_path, p->fts_accpath,
|
|---|
| 245 | p->fts_statp)) {
|
|---|
| 246 | (void)fts_set(fts, p, FTS_SKIP);
|
|---|
| 247 | p->fts_number = SKIPPED;
|
|---|
| 248 | }
|
|---|
| 249 | #ifdef UF_APPEND
|
|---|
| 250 | else if (!uid &&
|
|---|
| 251 | (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
|
|---|
| 252 | !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
|
|---|
| 253 | chflags(p->fts_accpath,
|
|---|
| 254 | p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0)
|
|---|
| 255 | goto err;
|
|---|
| 256 | #endif
|
|---|
| 257 | continue;
|
|---|
| 258 | case FTS_DP:
|
|---|
| 259 | /* Post-order: see if user skipped. */
|
|---|
| 260 | if (p->fts_number == SKIPPED)
|
|---|
| 261 | continue;
|
|---|
| 262 | break;
|
|---|
| 263 | default:
|
|---|
| 264 | if (!fflag &&
|
|---|
| 265 | !check(p->fts_path, p->fts_accpath, p->fts_statp))
|
|---|
| 266 | continue;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | rval = 0;
|
|---|
| 270 | #ifdef UF_APPEND
|
|---|
| 271 | if (!uid &&
|
|---|
| 272 | (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
|
|---|
| 273 | !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
|
|---|
| 274 | rval = chflags(p->fts_accpath,
|
|---|
| 275 | p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
|
|---|
| 276 | #endif
|
|---|
| 277 | if (rval == 0) {
|
|---|
| 278 | /*
|
|---|
| 279 | * If we can't read or search the directory, may still be
|
|---|
| 280 | * able to remove it. Don't print out the un{read,search}able
|
|---|
| 281 | * message unless the remove fails.
|
|---|
| 282 | */
|
|---|
| 283 | switch (p->fts_info) {
|
|---|
| 284 | case FTS_DP:
|
|---|
| 285 | case FTS_DNR:
|
|---|
| 286 | rval = rmdir(p->fts_accpath);
|
|---|
| 287 | if (rval == 0 || (fflag && errno == ENOENT)) {
|
|---|
| 288 | if (rval == 0 && vflag)
|
|---|
| 289 | (void)printf("%s\n",
|
|---|
| 290 | p->fts_path);
|
|---|
| 291 | continue;
|
|---|
| 292 | }
|
|---|
| 293 | break;
|
|---|
| 294 |
|
|---|
| 295 | #ifdef FTS_W
|
|---|
| 296 | case FTS_W:
|
|---|
| 297 | rval = undelete(p->fts_accpath);
|
|---|
| 298 | if (rval == 0 && (fflag && errno == ENOENT)) {
|
|---|
| 299 | if (vflag)
|
|---|
| 300 | (void)printf("%s\n",
|
|---|
| 301 | p->fts_path);
|
|---|
| 302 | continue;
|
|---|
| 303 | }
|
|---|
| 304 | break;
|
|---|
| 305 | #endif
|
|---|
| 306 |
|
|---|
| 307 | case FTS_NS:
|
|---|
| 308 | /*
|
|---|
| 309 | * Assume that since fts_read() couldn't stat
|
|---|
| 310 | * the file, it can't be unlinked.
|
|---|
| 311 | */
|
|---|
| 312 | if (fflag)
|
|---|
| 313 | continue;
|
|---|
| 314 | /* FALLTHROUGH */
|
|---|
| 315 | default:
|
|---|
| 316 | if (Pflag)
|
|---|
| 317 | if (!rm_overwrite(p->fts_accpath, NULL))
|
|---|
| 318 | continue;
|
|---|
| 319 | rval = unlink(p->fts_accpath);
|
|---|
| 320 | if (rval == 0 || (fflag && errno == ENOENT)) {
|
|---|
| 321 | if (rval == 0 && vflag)
|
|---|
| 322 | (void)printf("%s\n",
|
|---|
| 323 | p->fts_path);
|
|---|
| 324 | continue;
|
|---|
| 325 | }
|
|---|
| 326 | }
|
|---|
| 327 | }
|
|---|
| 328 | err:
|
|---|
| 329 | fprintf(stderr, "%s: %s: %s\n", argv0, p->fts_path, strerror(errno));
|
|---|
| 330 | eval = 1;
|
|---|
| 331 | }
|
|---|
| 332 | if (errno) {
|
|---|
| 333 | fprintf(stderr, "%s: fts_read: %s\n", argv0, strerror(errno));
|
|---|
| 334 | eval = 1;
|
|---|
| 335 | }
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | static void
|
|---|
| 339 | rm_file(char **argv)
|
|---|
| 340 | {
|
|---|
| 341 | struct stat sb;
|
|---|
| 342 | int rval;
|
|---|
| 343 | char *f;
|
|---|
| 344 |
|
|---|
| 345 | /*
|
|---|
| 346 | * Remove a file. POSIX 1003.2 states that, by default, attempting
|
|---|
| 347 | * to remove a directory is an error, so must always stat the file.
|
|---|
| 348 | */
|
|---|
| 349 | while ((f = *argv++) != NULL) {
|
|---|
| 350 | /* Assume if can't stat the file, can't unlink it. */
|
|---|
| 351 | if (lstat(f, &sb)) {
|
|---|
| 352 | #ifdef FTS_WHITEOUT
|
|---|
| 353 | if (Wflag) {
|
|---|
| 354 | sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
|
|---|
| 355 | } else {
|
|---|
| 356 | #else
|
|---|
| 357 | {
|
|---|
| 358 | #endif
|
|---|
| 359 | if (!fflag || errno != ENOENT) {
|
|---|
| 360 | fprintf(stderr, "%s: %s: %s\n", argv0, f, strerror(errno));
|
|---|
| 361 | eval = 1;
|
|---|
| 362 | }
|
|---|
| 363 | continue;
|
|---|
| 364 | }
|
|---|
| 365 | #ifdef FTS_WHITEOUT
|
|---|
| 366 | } else if (Wflag) {
|
|---|
| 367 | fprintf(stderr, "%s: %s: %s\n", argv0, f, strerror(EEXIST));
|
|---|
| 368 | eval = 1;
|
|---|
| 369 | continue;
|
|---|
| 370 | #endif
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | if (S_ISDIR(sb.st_mode) && !dflag) {
|
|---|
| 374 | fprintf(stderr, "%s: %s: is a directory\n", argv0, f);
|
|---|
| 375 | eval = 1;
|
|---|
| 376 | continue;
|
|---|
| 377 | }
|
|---|
| 378 | if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
|
|---|
| 379 | continue;
|
|---|
| 380 | rval = 0;
|
|---|
| 381 | #ifdef UF_APPEND
|
|---|
| 382 | if (!uid &&
|
|---|
| 383 | (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
|
|---|
| 384 | !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
|
|---|
| 385 | rval = chflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
|
|---|
| 386 | #endif
|
|---|
| 387 | if (rval == 0) {
|
|---|
| 388 | if (S_ISWHT(sb.st_mode))
|
|---|
| 389 | rval = undelete(f);
|
|---|
| 390 | else if (S_ISDIR(sb.st_mode))
|
|---|
| 391 | rval = rmdir(f);
|
|---|
| 392 | else {
|
|---|
| 393 | if (Pflag)
|
|---|
| 394 | if (!rm_overwrite(f, &sb))
|
|---|
| 395 | continue;
|
|---|
| 396 | rval = unlink(f);
|
|---|
| 397 | }
|
|---|
| 398 | }
|
|---|
| 399 | if (rval && (!fflag || errno != ENOENT)) {
|
|---|
| 400 | fprintf(stderr, "%s: %s: %s\n", argv0, f, strerror(errno));
|
|---|
| 401 | eval = 1;
|
|---|
| 402 | }
|
|---|
| 403 | if (vflag && rval == 0)
|
|---|
| 404 | (void)printf("%s\n", f);
|
|---|
| 405 | }
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | /*
|
|---|
| 409 | * rm_overwrite --
|
|---|
| 410 | * Overwrite the file 3 times with varying bit patterns.
|
|---|
| 411 | *
|
|---|
| 412 | * XXX
|
|---|
| 413 | * This is a cheap way to *really* delete files. Note that only regular
|
|---|
| 414 | * files are deleted, directories (and therefore names) will remain.
|
|---|
| 415 | * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
|
|---|
| 416 | * System V file system). In a logging file system, you'll have to have
|
|---|
| 417 | * kernel support.
|
|---|
| 418 | */
|
|---|
| 419 | static int
|
|---|
| 420 | rm_overwrite(char *file, struct stat *sbp)
|
|---|
| 421 | {
|
|---|
| 422 | struct stat sb;
|
|---|
| 423 | #ifdef HAVE_FSTATFS
|
|---|
| 424 | struct statfs fsb;
|
|---|
| 425 | #endif
|
|---|
| 426 | off_t len;
|
|---|
| 427 | int bsize, fd, wlen;
|
|---|
| 428 | char *buf = NULL;
|
|---|
| 429 |
|
|---|
| 430 | fd = -1;
|
|---|
| 431 | if (sbp == NULL) {
|
|---|
| 432 | if (lstat(file, &sb))
|
|---|
| 433 | goto err;
|
|---|
| 434 | sbp = &sb;
|
|---|
| 435 | }
|
|---|
| 436 | if (!S_ISREG(sbp->st_mode))
|
|---|
| 437 | return (1);
|
|---|
| 438 | if ((fd = open(file, O_WRONLY, 0)) == -1)
|
|---|
| 439 | goto err;
|
|---|
| 440 | #ifdef HAVE_FSTATFS
|
|---|
| 441 | if (fstatfs(fd, &fsb) == -1)
|
|---|
| 442 | goto err;
|
|---|
| 443 | bsize = MAX(fsb.f_iosize, 1024);
|
|---|
| 444 | #elif defined(HAVE_ST_BLKSIZE)
|
|---|
| 445 | bsize = MAX(sb.st_blksize, 1024);
|
|---|
| 446 | #else
|
|---|
| 447 | bsize = 1024;
|
|---|
| 448 | #endif
|
|---|
| 449 | if ((buf = malloc(bsize)) == NULL)
|
|---|
| 450 | err(1, "%s: malloc", file);
|
|---|
| 451 |
|
|---|
| 452 | #define PASS(byte) { \
|
|---|
| 453 | memset(buf, byte, bsize); \
|
|---|
| 454 | for (len = sbp->st_size; len > 0; len -= wlen) { \
|
|---|
| 455 | wlen = len < bsize ? len : bsize; \
|
|---|
| 456 | if (write(fd, buf, wlen) != wlen) \
|
|---|
| 457 | goto err; \
|
|---|
| 458 | } \
|
|---|
| 459 | }
|
|---|
| 460 | PASS(0xff);
|
|---|
| 461 | if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
|
|---|
| 462 | goto err;
|
|---|
| 463 | PASS(0x00);
|
|---|
| 464 | if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
|
|---|
| 465 | goto err;
|
|---|
| 466 | PASS(0xff);
|
|---|
| 467 | if (!fsync(fd) && !close(fd)) {
|
|---|
| 468 | free(buf);
|
|---|
| 469 | return (1);
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | err: eval = 1;
|
|---|
| 473 | if (buf)
|
|---|
| 474 | free(buf);
|
|---|
| 475 | if (fd != -1)
|
|---|
| 476 | close(fd);
|
|---|
| 477 | fprintf(stderr, "%s: %s: %s\n", argv0, file, strerror(errno));
|
|---|
| 478 | return (0);
|
|---|
| 479 | }
|
|---|
| 480 |
|
|---|
| 481 |
|
|---|
| 482 | static int
|
|---|
| 483 | check(char *path, char *name, struct stat *sp)
|
|---|
| 484 | {
|
|---|
| 485 | int ch, first;
|
|---|
| 486 | char modep[15], *flagsp;
|
|---|
| 487 |
|
|---|
| 488 | /* Check -i first. */
|
|---|
| 489 | if (iflag)
|
|---|
| 490 | (void)fprintf(stderr, "remove %s? ", path);
|
|---|
| 491 | else {
|
|---|
| 492 | /*
|
|---|
| 493 | * If it's not a symbolic link and it's unwritable and we're
|
|---|
| 494 | * talking to a terminal, ask. Symbolic links are excluded
|
|---|
| 495 | * because their permissions are meaningless. Check stdin_ok
|
|---|
| 496 | * first because we may not have stat'ed the file.
|
|---|
| 497 | * Also skip this check if the -P option was specified because
|
|---|
| 498 | * we will not be able to overwrite file contents and will
|
|---|
| 499 | * barf later.
|
|---|
| 500 | */
|
|---|
| 501 | if (!stdin_ok || S_ISLNK(sp->st_mode) || Pflag ||
|
|---|
| 502 | (!access(name, W_OK) &&
|
|---|
| 503 | #ifdef SF_APPEND
|
|---|
| 504 | !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
|
|---|
| 505 | (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid))
|
|---|
| 506 | #else
|
|---|
| 507 | 1)
|
|---|
| 508 | #endif
|
|---|
| 509 | )
|
|---|
| 510 | return (1);
|
|---|
| 511 | strmode(sp->st_mode, modep);
|
|---|
| 512 | #ifdef SF_APPEND
|
|---|
| 513 | if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
|
|---|
| 514 | err(1, "fflagstostr");
|
|---|
| 515 | (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
|
|---|
| 516 | modep + 1, modep[9] == ' ' ? "" : " ",
|
|---|
| 517 | user_from_uid(sp->st_uid, 0),
|
|---|
| 518 | group_from_gid(sp->st_gid, 0),
|
|---|
| 519 | *flagsp ? flagsp : "", *flagsp ? " " : "",
|
|---|
| 520 | path);
|
|---|
| 521 | free(flagsp);
|
|---|
| 522 | #else
|
|---|
| 523 | (void)fprintf(stderr, "override %s%s %d/%d for %s? ",
|
|---|
| 524 | modep + 1, modep[9] == ' ' ? "" : " ",
|
|---|
| 525 | sp->st_uid, sp->st_gid, path);
|
|---|
| 526 | #endif
|
|---|
| 527 | }
|
|---|
| 528 | (void)fflush(stderr);
|
|---|
| 529 |
|
|---|
| 530 | first = ch = getchar();
|
|---|
| 531 | while (ch != '\n' && ch != EOF)
|
|---|
| 532 | ch = getchar();
|
|---|
| 533 | return (first == 'y' || first == 'Y');
|
|---|
| 534 | }
|
|---|
| 535 |
|
|---|
| 536 | #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
|
|---|
| 537 | static void
|
|---|
| 538 | checkdot(char **argv)
|
|---|
| 539 | {
|
|---|
| 540 | char *p, **save, **t;
|
|---|
| 541 | int complained;
|
|---|
| 542 |
|
|---|
| 543 | complained = 0;
|
|---|
| 544 | for (t = argv; *t;) {
|
|---|
| 545 | if ((p = strrchr(*t, '/')) != NULL)
|
|---|
| 546 | ++p;
|
|---|
| 547 | else
|
|---|
| 548 | p = *t;
|
|---|
| 549 | if (ISDOT(p)) {
|
|---|
| 550 | if (!complained++)
|
|---|
| 551 | fprintf(stderr, "%s: \".\" and \"..\" may not be removed\n", argv0);
|
|---|
| 552 | eval = 1;
|
|---|
| 553 | for (save = t; (t[0] = t[1]) != NULL; ++t)
|
|---|
| 554 | continue;
|
|---|
| 555 | t = save;
|
|---|
| 556 | } else
|
|---|
| 557 | ++t;
|
|---|
| 558 | }
|
|---|
| 559 | }
|
|---|
| 560 |
|
|---|
| 561 | static int
|
|---|
| 562 | usage(void)
|
|---|
| 563 | {
|
|---|
| 564 |
|
|---|
| 565 | (void)fprintf(stderr, "%s\n%s\n",
|
|---|
| 566 | "usage: rm [-f | -i] [-dPRrvW] file ...\n",
|
|---|
| 567 | " unlink file");
|
|---|
| 568 | return EX_USAGE;
|
|---|
| 569 | }
|
|---|