Unsetting an undefined variable or function is not an error.
[dragonfly.git] / bin / rm / rm.c
CommitLineData
984263bc
MD
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.
1de703da
MD
32 *
33 * @(#) Copyright (c) 1990, 1993, 1994 The Regents of the University of California. All rights reserved.
34 * @(#)rm.c 8.5 (Berkeley) 4/18/94
35 * $FreeBSD: src/bin/rm/rm.c,v 1.29.2.5 2002/07/12 07:25:48 tjr Exp $
17b61719 36 * $DragonFly: src/bin/rm/rm.c,v 1.3 2004/08/30 19:27:21 eirikn Exp $
984263bc
MD
37 */
38
984263bc
MD
39#include <sys/stat.h>
40#include <sys/param.h>
41#include <sys/mount.h>
42
43#include <err.h>
44#include <errno.h>
45#include <fcntl.h>
46#include <fts.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#include <sysexits.h>
51#include <unistd.h>
52
53int dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok;
54uid_t uid;
55
56int check(char *, char *, struct stat *);
57void checkdot(char **);
58void rm_file(char **);
59void rm_overwrite(char *, struct stat *);
60void rm_tree(char **);
61void usage(void);
62
63/*
64 * rm --
65 * This rm is different from historic rm's, but is expected to match
66 * POSIX 1003.2 behavior. The most visible difference is that -f
67 * has two specific effects now, ignore non-existent files and force
68 * file removal.
69 */
70int
71main(int argc, char *argv[])
72{
73 int ch, rflag;
74 char *p;
75
76 /*
77 * Test for the special case where the utility is called as
78 * "unlink", for which the functionality provided is greatly
79 * simplified.
80 */
17b61719 81 if ((p = strrchr(argv[0], '/')) == NULL)
984263bc
MD
82 p = argv[0];
83 else
84 ++p;
85 if (strcmp(p, "unlink") == 0) {
86 while (getopt(argc, argv, "") != -1)
87 usage();
88 argc -= optind;
89 argv += optind;
90 if (argc != 1)
91 usage();
92 rm_file(&argv[0]);
93 exit(eval);
94 }
95
96 Pflag = rflag = 0;
97 while ((ch = getopt(argc, argv, "dfiPRrvW")) != -1)
98 switch(ch) {
99 case 'd':
100 dflag = 1;
101 break;
102 case 'f':
103 fflag = 1;
104 iflag = 0;
105 break;
106 case 'i':
107 fflag = 0;
108 iflag = 1;
109 break;
110 case 'P':
111 Pflag = 1;
112 break;
113 case 'R':
114 case 'r': /* Compatibility. */
115 rflag = 1;
116 break;
117 case 'v':
118 vflag = 1;
119 break;
120 case 'W':
121 Wflag = 1;
122 break;
123 default:
124 usage();
125 }
126 argc -= optind;
127 argv += optind;
128
129 if (argc < 1) {
130 if (fflag)
131 return 0;
132 usage();
133 }
134
135 checkdot(argv);
136 uid = geteuid();
137
138 if (*argv) {
139 stdin_ok = isatty(STDIN_FILENO);
140
141 if (rflag)
142 rm_tree(argv);
143 else
144 rm_file(argv);
145 }
146
147 exit (eval);
148}
149
150void
151rm_tree(char **argv)
152{
153 FTS *fts;
154 FTSENT *p;
155 int needstat;
156 int flags;
157 int rval;
158
159 /*
160 * Remove a file hierarchy. If forcing removal (-f), or interactive
161 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
162 */
163 needstat = !uid || (!fflag && !iflag && stdin_ok);
164
165 /*
166 * If the -i option is specified, the user can skip on the pre-order
167 * visit. The fts_number field flags skipped directories.
168 */
169#define SKIPPED 1
170
171 flags = FTS_PHYSICAL;
172 if (!needstat)
173 flags |= FTS_NOSTAT;
174 if (Wflag)
175 flags |= FTS_WHITEOUT;
176 if (!(fts = fts_open(argv, flags, NULL)))
177 err(1, NULL);
178 while ((p = fts_read(fts)) != NULL) {
179 switch (p->fts_info) {
180 case FTS_DNR:
181 if (!fflag || p->fts_errno != ENOENT) {
182 warnx("%s: %s",
183 p->fts_path, strerror(p->fts_errno));
184 eval = 1;
185 }
186 continue;
187 case FTS_ERR:
188 errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
189 case FTS_NS:
190 /*
191 * FTS_NS: assume that if can't stat the file, it
192 * can't be unlinked.
193 */
194 if (!needstat)
195 break;
196 if (!fflag || p->fts_errno != ENOENT) {
197 warnx("%s: %s",
198 p->fts_path, strerror(p->fts_errno));
199 eval = 1;
200 }
201 continue;
202 case FTS_D:
203 /* Pre-order: give user chance to skip. */
204 if (!fflag && !check(p->fts_path, p->fts_accpath,
205 p->fts_statp)) {
206 (void)fts_set(fts, p, FTS_SKIP);
207 p->fts_number = SKIPPED;
208 }
209 else if (!uid &&
210 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
211 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
212 chflags(p->fts_accpath,
213 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0)
214 goto err;
215 continue;
216 case FTS_DP:
217 /* Post-order: see if user skipped. */
218 if (p->fts_number == SKIPPED)
219 continue;
220 break;
221 default:
222 if (!fflag &&
223 !check(p->fts_path, p->fts_accpath, p->fts_statp))
224 continue;
225 }
226
227 rval = 0;
228 if (!uid &&
229 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
230 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
231 rval = chflags(p->fts_accpath,
232 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
233 if (rval == 0) {
234 /*
235 * If we can't read or search the directory, may still be
236 * able to remove it. Don't print out the un{read,search}able
237 * message unless the remove fails.
238 */
239 switch (p->fts_info) {
240 case FTS_DP:
241 case FTS_DNR:
242 rval = rmdir(p->fts_accpath);
243 if (rval == 0 || (fflag && errno == ENOENT)) {
244 if (rval == 0 && vflag)
245 (void)printf("%s\n",
246 p->fts_path);
247 continue;
248 }
249 break;
250
251 case FTS_W:
252 rval = undelete(p->fts_accpath);
253 if (rval == 0 && (fflag && errno == ENOENT)) {
254 if (vflag)
255 (void)printf("%s\n",
256 p->fts_path);
257 continue;
258 }
259 break;
260
261 default:
262 if (Pflag)
263 rm_overwrite(p->fts_accpath, NULL);
264 rval = unlink(p->fts_accpath);
265 if (rval == 0 || (fflag && errno == ENOENT)) {
266 if (rval == 0 && vflag)
267 (void)printf("%s\n",
268 p->fts_path);
269 continue;
270 }
271 }
272 }
273err:
274 warn("%s", p->fts_path);
275 eval = 1;
276 }
277 if (errno)
278 err(1, "fts_read");
279}
280
281void
282rm_file(char **argv)
283{
284 struct stat sb;
285 int rval;
286 char *f;
287
288 /*
289 * Remove a file. POSIX 1003.2 states that, by default, attempting
290 * to remove a directory is an error, so must always stat the file.
291 */
292 while ((f = *argv++) != NULL) {
293 /* Assume if can't stat the file, can't unlink it. */
294 if (lstat(f, &sb)) {
295 if (Wflag) {
296 sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
297 } else {
298 if (!fflag || errno != ENOENT) {
299 warn("%s", f);
300 eval = 1;
301 }
302 continue;
303 }
304 } else if (Wflag) {
305 warnx("%s: %s", f, strerror(EEXIST));
306 eval = 1;
307 continue;
308 }
309
310 if (S_ISDIR(sb.st_mode) && !dflag) {
311 warnx("%s: is a directory", f);
312 eval = 1;
313 continue;
314 }
315 if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
316 continue;
317 rval = 0;
318 if (!uid &&
319 (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
320 !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
321 rval = chflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
322 if (rval == 0) {
323 if (S_ISWHT(sb.st_mode))
324 rval = undelete(f);
325 else if (S_ISDIR(sb.st_mode))
326 rval = rmdir(f);
327 else {
328 if (Pflag)
329 rm_overwrite(f, &sb);
330 rval = unlink(f);
331 }
332 }
333 if (rval && (!fflag || errno != ENOENT)) {
334 warn("%s", f);
335 eval = 1;
336 }
337 if (vflag && rval == 0)
338 (void)printf("%s\n", f);
339 }
340}
341
342/*
343 * rm_overwrite --
344 * Overwrite the file 3 times with varying bit patterns.
345 *
346 * XXX
347 * This is a cheap way to *really* delete files. Note that only regular
348 * files are deleted, directories (and therefore names) will remain.
349 * Also, this assumes a fixed-block filesystem (like FFS, or a V7 or a
350 * System V filesystem). In a logging filesystem, you'll have to have
351 * kernel support.
352 */
353void
354rm_overwrite(char *file, struct stat *sbp)
355{
356 struct stat sb;
357 struct statfs fsb;
358 off_t len;
359 int bsize, fd, wlen;
360 char *buf = NULL;
361
362 fd = -1;
363 if (sbp == NULL) {
364 if (lstat(file, &sb))
365 goto err;
366 sbp = &sb;
367 }
368 if (!S_ISREG(sbp->st_mode))
369 return;
370 if ((fd = open(file, O_WRONLY, 0)) == -1)
371 goto err;
372 if (fstatfs(fd, &fsb) == -1)
373 goto err;
374 bsize = MAX(fsb.f_iosize, 1024);
375 if ((buf = malloc(bsize)) == NULL)
376 err(1, "malloc");
377
378#define PASS(byte) { \
379 memset(buf, byte, bsize); \
380 for (len = sbp->st_size; len > 0; len -= wlen) { \
381 wlen = len < bsize ? len : bsize; \
382 if (write(fd, buf, wlen) != wlen) \
383 goto err; \
384 } \
385}
386 PASS(0xff);
387 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
388 goto err;
389 PASS(0x00);
390 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
391 goto err;
392 PASS(0xff);
393 if (!fsync(fd) && !close(fd)) {
394 free(buf);
395 return;
396 }
397
398err: eval = 1;
399 if (buf)
400 free(buf);
401 warn("%s", file);
402}
403
404
405int
406check(char *path, char *name, struct stat *sp)
407{
408 int ch, first;
409 char modep[15], *flagsp;
410
411 /* Check -i first. */
412 if (iflag)
413 (void)fprintf(stderr, "remove %s? ", path);
414 else {
415 /*
416 * If it's not a symbolic link and it's unwritable and we're
417 * talking to a terminal, ask. Symbolic links are excluded
418 * because their permissions are meaningless. Check stdin_ok
419 * first because we may not have stat'ed the file.
420 */
421 if (!stdin_ok || S_ISLNK(sp->st_mode) ||
422 (!access(name, W_OK) &&
423 !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
424 (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid)))
425 return (1);
426 strmode(sp->st_mode, modep);
427 if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
428 err(1, NULL);
429 (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
430 modep + 1, modep[9] == ' ' ? "" : " ",
431 user_from_uid(sp->st_uid, 0),
432 group_from_gid(sp->st_gid, 0),
433 *flagsp ? flagsp : "", *flagsp ? " " : "",
434 path);
435 free(flagsp);
436 }
437 (void)fflush(stderr);
438
439 first = ch = getchar();
440 while (ch != '\n' && ch != EOF)
441 ch = getchar();
442 return (first == 'y' || first == 'Y');
443}
444
445#define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
446void
447checkdot(char **argv)
448{
449 char *p, **save, **t;
450 int complained;
451
452 complained = 0;
453 for (t = argv; *t;) {
454 if ((p = strrchr(*t, '/')) != NULL)
455 ++p;
456 else
457 p = *t;
458 if (ISDOT(p)) {
459 if (!complained++)
460 warnx("\".\" and \"..\" may not be removed");
461 eval = 1;
462 for (save = t; (t[0] = t[1]) != NULL; ++t)
463 continue;
464 t = save;
465 } else
466 ++t;
467 }
468}
469
470void
471usage(void)
472{
473
474 (void)fprintf(stderr, "%s\n%s\n",
475 "usage: rm [-f | -i] [-dPRrvW] file ...",
476 " unlink file");
477 exit(EX_USAGE);
478}