1*4d503dc0Stedu /* $OpenBSD: rm.c,v 1.11 2016/10/10 18:13:21 tedu Exp $ */
2e580cfdfStedu /* $NetBSD: rm.c,v 1.19 1995/09/07 06:48:50 jtc Exp $ */
3e580cfdfStedu
4e580cfdfStedu /*-
5e580cfdfStedu * Copyright (c) 1990, 1993, 1994
6e580cfdfStedu * The Regents of the University of California. All rights reserved.
7e580cfdfStedu *
8e580cfdfStedu * Redistribution and use in source and binary forms, with or without
9e580cfdfStedu * modification, are permitted provided that the following conditions
10e580cfdfStedu * are met:
11e580cfdfStedu * 1. Redistributions of source code must retain the above copyright
12e580cfdfStedu * notice, this list of conditions and the following disclaimer.
13e580cfdfStedu * 2. Redistributions in binary form must reproduce the above copyright
14e580cfdfStedu * notice, this list of conditions and the following disclaimer in the
15e580cfdfStedu * documentation and/or other materials provided with the distribution.
16e580cfdfStedu * 3. Neither the name of the University nor the names of its contributors
17e580cfdfStedu * may be used to endorse or promote products derived from this software
18e580cfdfStedu * without specific prior written permission.
19e580cfdfStedu *
20e580cfdfStedu * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21e580cfdfStedu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22e580cfdfStedu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23e580cfdfStedu * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24e580cfdfStedu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25e580cfdfStedu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26e580cfdfStedu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27e580cfdfStedu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28e580cfdfStedu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29e580cfdfStedu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30e580cfdfStedu * SUCH DAMAGE.
31e580cfdfStedu */
32e580cfdfStedu
33e580cfdfStedu #include <sys/types.h>
34e580cfdfStedu
35e580cfdfStedu #include <err.h>
36e580cfdfStedu #include <errno.h>
37e580cfdfStedu #include <fcntl.h>
38e580cfdfStedu #include <fts.h>
39e580cfdfStedu #include <stdio.h>
40e580cfdfStedu #include <stdlib.h>
41e580cfdfStedu #include <string.h>
42e580cfdfStedu #include <unistd.h>
43e580cfdfStedu
440462415eStedu static int eval;
45e580cfdfStedu
46e580cfdfStedu static void checkdot(char **);
47e580cfdfStedu static void rm_tree(char **);
48e580cfdfStedu
49e580cfdfStedu int
rmmain(int argc,char * argv[])50e580cfdfStedu rmmain(int argc, char *argv[])
51e580cfdfStedu {
52e580cfdfStedu
53e580cfdfStedu checkdot(argv);
54e580cfdfStedu
550462415eStedu if (*argv)
56e580cfdfStedu rm_tree(argv);
57e580cfdfStedu
58e580cfdfStedu return (eval);
59e580cfdfStedu }
60e580cfdfStedu
61e580cfdfStedu static void
rm_tree(char ** argv)62e580cfdfStedu rm_tree(char **argv)
63e580cfdfStedu {
64e580cfdfStedu FTS *fts;
65e580cfdfStedu FTSENT *p;
66e580cfdfStedu int flags;
67e580cfdfStedu
68e580cfdfStedu flags = FTS_PHYSICAL;
69e580cfdfStedu flags |= FTS_NOSTAT;
70e580cfdfStedu if (!(fts = fts_open(argv, flags, NULL)))
71e580cfdfStedu err(1, NULL);
72e580cfdfStedu while ((p = fts_read(fts)) != NULL) {
73e580cfdfStedu switch (p->fts_info) {
74e580cfdfStedu case FTS_DNR:
751eaf9cb5Stedu if (p->fts_errno != ENOENT) {
76e580cfdfStedu warnx("%s: %s",
77e580cfdfStedu p->fts_path, strerror(p->fts_errno));
78e580cfdfStedu eval = 1;
79e580cfdfStedu }
80e580cfdfStedu continue;
81e580cfdfStedu case FTS_ERR:
82e580cfdfStedu errc(1, p->fts_errno, "%s", p->fts_path);
83e580cfdfStedu case FTS_D:
84e580cfdfStedu continue;
85e580cfdfStedu default:
861eaf9cb5Stedu break;
87e580cfdfStedu }
88e580cfdfStedu
89e580cfdfStedu /*
90e580cfdfStedu * If we can't read or search the directory, may still be
91e580cfdfStedu * able to remove it. Don't print out the un{read,search}able
92e580cfdfStedu * message unless the remove fails.
93e580cfdfStedu */
94e580cfdfStedu switch (p->fts_info) {
95e580cfdfStedu case FTS_DP:
96e580cfdfStedu case FTS_DNR:
97e580cfdfStedu if (!rmdir(p->fts_accpath) ||
981eaf9cb5Stedu (errno == ENOENT))
99e580cfdfStedu continue;
100e580cfdfStedu break;
101e580cfdfStedu
102e580cfdfStedu case FTS_F:
103e580cfdfStedu case FTS_NSOK:
104e580cfdfStedu default:
105e580cfdfStedu if (!unlink(p->fts_accpath) ||
1061eaf9cb5Stedu (errno == ENOENT))
107e580cfdfStedu continue;
108e580cfdfStedu }
109e580cfdfStedu warn("%s", p->fts_path);
110e580cfdfStedu eval = 1;
111e580cfdfStedu }
112e580cfdfStedu if (errno)
113e580cfdfStedu err(1, "fts_read");
114e580cfdfStedu fts_close(fts);
115e580cfdfStedu }
116e580cfdfStedu
117e580cfdfStedu /*
118e580cfdfStedu * POSIX.2 requires that if "." or ".." are specified as the basename
119e580cfdfStedu * portion of an operand, a diagnostic message be written to standard
120e580cfdfStedu * error and nothing more be done with such operands.
121e580cfdfStedu *
122e580cfdfStedu * Since POSIX.2 defines basename as the final portion of a path after
123e580cfdfStedu * trailing slashes have been removed, we'll remove them here.
124e580cfdfStedu */
125e580cfdfStedu #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
126e580cfdfStedu static void
checkdot(char ** argv)127e580cfdfStedu checkdot(char **argv)
128e580cfdfStedu {
129e580cfdfStedu char *p, **save, **t;
130e580cfdfStedu int complained;
131e580cfdfStedu
132e580cfdfStedu complained = 0;
133e580cfdfStedu for (t = argv; *t;) {
134e580cfdfStedu /* strip trailing slashes */
135e580cfdfStedu p = strrchr (*t, '\0');
136e580cfdfStedu while (--p > *t && *p == '/')
137e580cfdfStedu *p = '\0';
138e580cfdfStedu
139e580cfdfStedu /* extract basename */
140e580cfdfStedu if ((p = strrchr(*t, '/')) != NULL)
141e580cfdfStedu ++p;
142e580cfdfStedu else
143e580cfdfStedu p = *t;
144e580cfdfStedu
145e580cfdfStedu if (ISDOT(p)) {
146e580cfdfStedu if (!complained++)
147e580cfdfStedu warnx("\".\" and \"..\" may not be removed");
148e580cfdfStedu eval = 1;
149e580cfdfStedu for (save = t; (t[0] = t[1]) != NULL; ++t)
150e580cfdfStedu continue;
151e580cfdfStedu t = save;
152e580cfdfStedu } else
153e580cfdfStedu ++t;
154e580cfdfStedu }
155e580cfdfStedu }
156