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