14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
44b88c807SRodney W. Grimes * Copyright (c) 1990, 1993, 1994
54b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved.
64b88c807SRodney W. Grimes *
74b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
84b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions
94b88c807SRodney W. Grimes * are met:
104b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
114b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
124b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
134b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
144b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
164b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software
174b88c807SRodney W. Grimes * without specific prior written permission.
184b88c807SRodney W. Grimes *
194b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
204b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
214b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
224b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
234b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
244b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
254b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
264b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
274b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
284b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
294b88c807SRodney W. Grimes * SUCH DAMAGE.
304b88c807SRodney W. Grimes */
314b88c807SRodney W. Grimes
324b88c807SRodney W. Grimes #include <sys/stat.h>
33f80db2b8SKris Kennaway #include <sys/param.h>
34f80db2b8SKris Kennaway #include <sys/mount.h>
354b88c807SRodney W. Grimes
364b88c807SRodney W. Grimes #include <err.h>
374b88c807SRodney W. Grimes #include <errno.h>
384b88c807SRodney W. Grimes #include <fcntl.h>
394b88c807SRodney W. Grimes #include <fts.h>
40fc69394fSWarner Losh #include <grp.h>
41b81cd107SXin LI #include <locale.h>
42fc69394fSWarner Losh #include <pwd.h>
434b88c807SRodney W. Grimes #include <stdio.h>
444b88c807SRodney W. Grimes #include <stdlib.h>
454b88c807SRodney W. Grimes #include <string.h>
46bfbdd545SMichael Haro #include <sysexits.h>
474b88c807SRodney W. Grimes #include <unistd.h>
484b88c807SRodney W. Grimes
49900c4ed3SConrad Meyer static int dflag, eval, fflag, iflag, vflag, Wflag, stdin_ok;
50d4319e74SEitan Adler static int rflag, Iflag, xflag;
51f9d4afb4SEd Schouten static uid_t uid;
52f9d4afb4SEd Schouten static volatile sig_atomic_t info;
534b88c807SRodney W. Grimes
549d6d3f96SEitan Adler static int check(const char *, const char *, struct stat *);
559d6d3f96SEitan Adler static int check2(char **);
569d6d3f96SEitan Adler static void checkdot(char **);
579d6d3f96SEitan Adler static void checkslash(char **);
589d6d3f96SEitan Adler static void rm_file(char **);
599d6d3f96SEitan Adler static void rm_tree(char **);
60b5260db6SWarner Losh static void siginfo(int __unused);
619d6d3f96SEitan Adler static void usage(void);
624b88c807SRodney W. Grimes
634b88c807SRodney W. Grimes /*
644b88c807SRodney W. Grimes * rm --
654b88c807SRodney W. Grimes * This rm is different from historic rm's, but is expected to match
664b88c807SRodney W. Grimes * POSIX 1003.2 behavior. The most visible difference is that -f
674b88c807SRodney W. Grimes * has two specific effects now, ignore non-existent files and force
684b88c807SRodney W. Grimes * file removal.
694b88c807SRodney W. Grimes */
704b88c807SRodney W. Grimes int
main(int argc,char * argv[])7146251ddeSWarner Losh main(int argc, char *argv[])
724b88c807SRodney W. Grimes {
7324c0f738SXin LI int ch;
74d71e172aSSheldon Hearn char *p;
75d71e172aSSheldon Hearn
76b81cd107SXin LI (void)setlocale(LC_ALL, "");
77b81cd107SXin LI
78d71e172aSSheldon Hearn /*
79d71e172aSSheldon Hearn * Test for the special case where the utility is called as
80d71e172aSSheldon Hearn * "unlink", for which the functionality provided is greatly
81d71e172aSSheldon Hearn * simplified.
82d71e172aSSheldon Hearn */
830cf90cd1SJilles Tjoelker if ((p = strrchr(argv[0], '/')) == NULL)
84d71e172aSSheldon Hearn p = argv[0];
85d71e172aSSheldon Hearn else
86d71e172aSSheldon Hearn ++p;
87d71e172aSSheldon Hearn if (strcmp(p, "unlink") == 0) {
8819b4f0dcSEd Maste if (argc == 2)
8919b4f0dcSEd Maste rm_file(&argv[1]);
9019b4f0dcSEd Maste else if (argc == 3 && strcmp(argv[1], "--") == 0)
9119b4f0dcSEd Maste rm_file(&argv[2]);
9219b4f0dcSEd Maste else
93d71e172aSSheldon Hearn usage();
94e9393a92STim J. Robbins exit(eval);
95d71e172aSSheldon Hearn }
964b88c807SRodney W. Grimes
97900c4ed3SConrad Meyer rflag = xflag = 0;
98d4319e74SEitan Adler while ((ch = getopt(argc, argv, "dfiIPRrvWx")) != -1)
994b88c807SRodney W. Grimes switch(ch) {
1004b88c807SRodney W. Grimes case 'd':
1014b88c807SRodney W. Grimes dflag = 1;
1024b88c807SRodney W. Grimes break;
1034b88c807SRodney W. Grimes case 'f':
1044b88c807SRodney W. Grimes fflag = 1;
1054b88c807SRodney W. Grimes iflag = 0;
1064b88c807SRodney W. Grimes break;
1074b88c807SRodney W. Grimes case 'i':
1084b88c807SRodney W. Grimes fflag = 0;
1094b88c807SRodney W. Grimes iflag = 1;
1104b88c807SRodney W. Grimes break;
11124c0f738SXin LI case 'I':
11224c0f738SXin LI Iflag = 1;
11324c0f738SXin LI break;
1144b88c807SRodney W. Grimes case 'P':
115900c4ed3SConrad Meyer /* Compatibility no-op. */
1164b88c807SRodney W. Grimes break;
1174b88c807SRodney W. Grimes case 'R':
1184b88c807SRodney W. Grimes case 'r': /* Compatibility. */
1194b88c807SRodney W. Grimes rflag = 1;
1204b88c807SRodney W. Grimes break;
121bfbdd545SMichael Haro case 'v':
122bfbdd545SMichael Haro vflag = 1;
123bfbdd545SMichael Haro break;
124777d1f82SMichael Haro case 'W':
125777d1f82SMichael Haro Wflag = 1;
126777d1f82SMichael Haro break;
127d4319e74SEitan Adler case 'x':
128d4319e74SEitan Adler xflag = 1;
129d4319e74SEitan Adler break;
1304b88c807SRodney W. Grimes default:
1314b88c807SRodney W. Grimes usage();
1324b88c807SRodney W. Grimes }
1334b88c807SRodney W. Grimes argc -= optind;
1344b88c807SRodney W. Grimes argv += optind;
1354b88c807SRodney W. Grimes
1360e8f2d6cSJordan K. Hubbard if (argc < 1) {
1370e8f2d6cSJordan K. Hubbard if (fflag)
138f3761deeSGuido van Rooij return (0);
1394b88c807SRodney W. Grimes usage();
1400e8f2d6cSJordan K. Hubbard }
1414b88c807SRodney W. Grimes
1424b88c807SRodney W. Grimes checkdot(argv);
14368ef5f71SDag-Erling Smørgrav checkslash(argv);
1446b419813SAndrey A. Chernov uid = geteuid();
1454b88c807SRodney W. Grimes
146b5260db6SWarner Losh (void)signal(SIGINFO, siginfo);
1471f64b5c9SSteve Price if (*argv) {
1481f64b5c9SSteve Price stdin_ok = isatty(STDIN_FILENO);
1491f64b5c9SSteve Price
15024c0f738SXin LI if (Iflag) {
15124c0f738SXin LI if (check2(argv) == 0)
15224c0f738SXin LI exit (1);
15324c0f738SXin LI }
1544b88c807SRodney W. Grimes if (rflag)
1554b88c807SRodney W. Grimes rm_tree(argv);
1564b88c807SRodney W. Grimes else
1574b88c807SRodney W. Grimes rm_file(argv);
1581f64b5c9SSteve Price }
1591f64b5c9SSteve Price
1604b88c807SRodney W. Grimes exit (eval);
1614b88c807SRodney W. Grimes }
1624b88c807SRodney W. Grimes
1639d6d3f96SEitan Adler static void
rm_tree(char ** argv)16446251ddeSWarner Losh rm_tree(char **argv)
1654b88c807SRodney W. Grimes {
1664b88c807SRodney W. Grimes FTS *fts;
1674b88c807SRodney W. Grimes FTSENT *p;
1684b88c807SRodney W. Grimes int needstat;
1691f64b5c9SSteve Price int flags;
1706b419813SAndrey A. Chernov int rval;
1714b88c807SRodney W. Grimes
1724b88c807SRodney W. Grimes /*
1734b88c807SRodney W. Grimes * Remove a file hierarchy. If forcing removal (-f), or interactive
1744b88c807SRodney W. Grimes * (-i) or can't ask anyway (stdin_ok), don't stat the file.
1754b88c807SRodney W. Grimes */
1761f64b5c9SSteve Price needstat = !uid || (!fflag && !iflag && stdin_ok);
1774b88c807SRodney W. Grimes
1784b88c807SRodney W. Grimes /*
1794b88c807SRodney W. Grimes * If the -i option is specified, the user can skip on the pre-order
1804b88c807SRodney W. Grimes * visit. The fts_number field flags skipped directories.
1814b88c807SRodney W. Grimes */
1824b88c807SRodney W. Grimes #define SKIPPED 1
1834b88c807SRodney W. Grimes
1844a086b52SBruce Evans flags = FTS_PHYSICAL;
1851f64b5c9SSteve Price if (!needstat)
1861f64b5c9SSteve Price flags |= FTS_NOSTAT;
1871f64b5c9SSteve Price if (Wflag)
1881f64b5c9SSteve Price flags |= FTS_WHITEOUT;
189d4319e74SEitan Adler if (xflag)
190d4319e74SEitan Adler flags |= FTS_XDEV;
191de3abdfaSJordan K. Hubbard if (!(fts = fts_open(argv, flags, NULL))) {
192de3abdfaSJordan K. Hubbard if (fflag && errno == ENOENT)
193de3abdfaSJordan K. Hubbard return;
1945ad9e45fSMatthew Dillon err(1, "fts_open");
195de3abdfaSJordan K. Hubbard }
196*2dfa4b66SBryan Drewery while (errno = 0, (p = fts_read(fts)) != NULL) {
1974b88c807SRodney W. Grimes switch (p->fts_info) {
1984b88c807SRodney W. Grimes case FTS_DNR:
1994b88c807SRodney W. Grimes if (!fflag || p->fts_errno != ENOENT) {
2004b88c807SRodney W. Grimes warnx("%s: %s",
2014b88c807SRodney W. Grimes p->fts_path, strerror(p->fts_errno));
2024b88c807SRodney W. Grimes eval = 1;
2034b88c807SRodney W. Grimes }
2044b88c807SRodney W. Grimes continue;
2054b88c807SRodney W. Grimes case FTS_ERR:
2064b88c807SRodney W. Grimes errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
2074b88c807SRodney W. Grimes case FTS_NS:
2084b88c807SRodney W. Grimes /*
209b800f53dSJun Kuriyama * Assume that since fts_read() couldn't stat the
210b800f53dSJun Kuriyama * file, it can't be unlinked.
2114b88c807SRodney W. Grimes */
2124b88c807SRodney W. Grimes if (!needstat)
2134b88c807SRodney W. Grimes break;
2144b88c807SRodney W. Grimes if (!fflag || p->fts_errno != ENOENT) {
2154b88c807SRodney W. Grimes warnx("%s: %s",
2164b88c807SRodney W. Grimes p->fts_path, strerror(p->fts_errno));
2174b88c807SRodney W. Grimes eval = 1;
2184b88c807SRodney W. Grimes }
2194b88c807SRodney W. Grimes continue;
2204b88c807SRodney W. Grimes case FTS_D:
2214b88c807SRodney W. Grimes /* Pre-order: give user chance to skip. */
2221f64b5c9SSteve Price if (!fflag && !check(p->fts_path, p->fts_accpath,
2234b88c807SRodney W. Grimes p->fts_statp)) {
2244b88c807SRodney W. Grimes (void)fts_set(fts, p, FTS_SKIP);
2254b88c807SRodney W. Grimes p->fts_number = SKIPPED;
2264b88c807SRodney W. Grimes }
2276b419813SAndrey A. Chernov else if (!uid &&
2286b419813SAndrey A. Chernov (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
2296b419813SAndrey A. Chernov !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
2306911f596SJilles Tjoelker lchflags(p->fts_accpath,
2316b419813SAndrey A. Chernov p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0)
2326b419813SAndrey A. Chernov goto err;
2334b88c807SRodney W. Grimes continue;
2344b88c807SRodney W. Grimes case FTS_DP:
2354b88c807SRodney W. Grimes /* Post-order: see if user skipped. */
2364b88c807SRodney W. Grimes if (p->fts_number == SKIPPED)
2374b88c807SRodney W. Grimes continue;
2384b88c807SRodney W. Grimes break;
2391f64b5c9SSteve Price default:
2404b88c807SRodney W. Grimes if (!fflag &&
2414b88c807SRodney W. Grimes !check(p->fts_path, p->fts_accpath, p->fts_statp))
2424b88c807SRodney W. Grimes continue;
2431f64b5c9SSteve Price }
2444b88c807SRodney W. Grimes
2456b419813SAndrey A. Chernov rval = 0;
2466b419813SAndrey A. Chernov if (!uid &&
2476b419813SAndrey A. Chernov (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
2486b419813SAndrey A. Chernov !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
2496911f596SJilles Tjoelker rval = lchflags(p->fts_accpath,
2506b419813SAndrey A. Chernov p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
2510efa2040SMichael Haro if (rval == 0) {
2524b88c807SRodney W. Grimes /*
2534b88c807SRodney W. Grimes * If we can't read or search the directory, may still be
2544b88c807SRodney W. Grimes * able to remove it. Don't print out the un{read,search}able
2554b88c807SRodney W. Grimes * message unless the remove fails.
2564b88c807SRodney W. Grimes */
2571f64b5c9SSteve Price switch (p->fts_info) {
2581f64b5c9SSteve Price case FTS_DP:
2591f64b5c9SSteve Price case FTS_DNR:
2600efa2040SMichael Haro rval = rmdir(p->fts_accpath);
2610efa2040SMichael Haro if (rval == 0 || (fflag && errno == ENOENT)) {
2620efa2040SMichael Haro if (rval == 0 && vflag)
263777d1f82SMichael Haro (void)printf("%s\n",
264b6f80a8eSDavid E. O'Brien p->fts_path);
265b5260db6SWarner Losh if (rval == 0 && info) {
266b5260db6SWarner Losh info = 0;
267b5260db6SWarner Losh (void)printf("%s\n",
268b5260db6SWarner Losh p->fts_path);
269b5260db6SWarner Losh }
2704b88c807SRodney W. Grimes continue;
271bfbdd545SMichael Haro }
2721f64b5c9SSteve Price break;
2731f64b5c9SSteve Price
2741f64b5c9SSteve Price case FTS_W:
2750efa2040SMichael Haro rval = undelete(p->fts_accpath);
2760efa2040SMichael Haro if (rval == 0 && (fflag && errno == ENOENT)) {
2770efa2040SMichael Haro if (vflag)
278777d1f82SMichael Haro (void)printf("%s\n",
279b6f80a8eSDavid E. O'Brien p->fts_path);
280b5260db6SWarner Losh if (info) {
281b5260db6SWarner Losh info = 0;
282b5260db6SWarner Losh (void)printf("%s\n",
283b5260db6SWarner Losh p->fts_path);
284b5260db6SWarner Losh }
2854b88c807SRodney W. Grimes continue;
286bfbdd545SMichael Haro }
2871f64b5c9SSteve Price break;
2881f64b5c9SSteve Price
289b800f53dSJun Kuriyama case FTS_NS:
290b800f53dSJun Kuriyama /*
291b800f53dSJun Kuriyama * Assume that since fts_read() couldn't stat
292b800f53dSJun Kuriyama * the file, it can't be unlinked.
293b800f53dSJun Kuriyama */
294b800f53dSJun Kuriyama if (fflag)
295b800f53dSJun Kuriyama continue;
296b800f53dSJun Kuriyama /* FALLTHROUGH */
297930e3238SXin LI
298930e3238SXin LI case FTS_F:
299930e3238SXin LI case FTS_NSOK:
300930e3238SXin LI default:
3010efa2040SMichael Haro rval = unlink(p->fts_accpath);
3020efa2040SMichael Haro if (rval == 0 || (fflag && errno == ENOENT)) {
3030efa2040SMichael Haro if (rval == 0 && vflag)
304777d1f82SMichael Haro (void)printf("%s\n",
305b6f80a8eSDavid E. O'Brien p->fts_path);
306b5260db6SWarner Losh if (rval == 0 && info) {
307b5260db6SWarner Losh info = 0;
308b5260db6SWarner Losh (void)printf("%s\n",
309b5260db6SWarner Losh p->fts_path);
310b5260db6SWarner Losh }
3114b88c807SRodney W. Grimes continue;
3124b88c807SRodney W. Grimes }
3136b419813SAndrey A. Chernov }
314bfbdd545SMichael Haro }
3156b419813SAndrey A. Chernov err:
3164b88c807SRodney W. Grimes warn("%s", p->fts_path);
3174b88c807SRodney W. Grimes eval = 1;
3184b88c807SRodney W. Grimes }
319413a368cSWarner Losh if (!fflag && errno)
3204b88c807SRodney W. Grimes err(1, "fts_read");
32108941824SMaxim Konovalov fts_close(fts);
3224b88c807SRodney W. Grimes }
3234b88c807SRodney W. Grimes
3244c99904bSEitan Adler static void
rm_file(char ** argv)32546251ddeSWarner Losh rm_file(char **argv)
3264b88c807SRodney W. Grimes {
3274b88c807SRodney W. Grimes struct stat sb;
3281f64b5c9SSteve Price int rval;
3294b88c807SRodney W. Grimes char *f;
3304b88c807SRodney W. Grimes
3314b88c807SRodney W. Grimes /*
3324b88c807SRodney W. Grimes * Remove a file. POSIX 1003.2 states that, by default, attempting
3334b88c807SRodney W. Grimes * to remove a directory is an error, so must always stat the file.
3344b88c807SRodney W. Grimes */
3354b88c807SRodney W. Grimes while ((f = *argv++) != NULL) {
3364b88c807SRodney W. Grimes /* Assume if can't stat the file, can't unlink it. */
3374b88c807SRodney W. Grimes if (lstat(f, &sb)) {
3381f64b5c9SSteve Price if (Wflag) {
3391f64b5c9SSteve Price sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
3401f64b5c9SSteve Price } else {
3414b88c807SRodney W. Grimes if (!fflag || errno != ENOENT) {
3424b88c807SRodney W. Grimes warn("%s", f);
3434b88c807SRodney W. Grimes eval = 1;
3444b88c807SRodney W. Grimes }
3454b88c807SRodney W. Grimes continue;
3464b88c807SRodney W. Grimes }
3471f64b5c9SSteve Price } else if (Wflag) {
3481f64b5c9SSteve Price warnx("%s: %s", f, strerror(EEXIST));
3491f64b5c9SSteve Price eval = 1;
3501f64b5c9SSteve Price continue;
3511f64b5c9SSteve Price }
3521f64b5c9SSteve Price
3531f64b5c9SSteve Price if (S_ISDIR(sb.st_mode) && !dflag) {
3544b88c807SRodney W. Grimes warnx("%s: is a directory", f);
3554b88c807SRodney W. Grimes eval = 1;
3564b88c807SRodney W. Grimes continue;
3574b88c807SRodney W. Grimes }
3581f64b5c9SSteve Price if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
3594b88c807SRodney W. Grimes continue;
3606b419813SAndrey A. Chernov rval = 0;
361da29c656SMaxim Konovalov if (!uid && !S_ISWHT(sb.st_mode) &&
3626b419813SAndrey A. Chernov (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
3636b419813SAndrey A. Chernov !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
3646911f596SJilles Tjoelker rval = lchflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
3650efa2040SMichael Haro if (rval == 0) {
3661f64b5c9SSteve Price if (S_ISWHT(sb.st_mode))
3671f64b5c9SSteve Price rval = undelete(f);
3681f64b5c9SSteve Price else if (S_ISDIR(sb.st_mode))
3694b88c807SRodney W. Grimes rval = rmdir(f);
370900c4ed3SConrad Meyer else
3714b88c807SRodney W. Grimes rval = unlink(f);
3724b88c807SRodney W. Grimes }
3734b88c807SRodney W. Grimes if (rval && (!fflag || errno != ENOENT)) {
3744b88c807SRodney W. Grimes warn("%s", f);
3754b88c807SRodney W. Grimes eval = 1;
3764b88c807SRodney W. Grimes }
3770efa2040SMichael Haro if (vflag && rval == 0)
378bfbdd545SMichael Haro (void)printf("%s\n", f);
379b5260db6SWarner Losh if (info && rval == 0) {
380b5260db6SWarner Losh info = 0;
381b5260db6SWarner Losh (void)printf("%s\n", f);
382b5260db6SWarner Losh }
3834b88c807SRodney W. Grimes }
3844b88c807SRodney W. Grimes }
3854b88c807SRodney W. Grimes
3869d6d3f96SEitan Adler static int
check(const char * path,const char * name,struct stat * sp)3879d6d3f96SEitan Adler check(const char *path, const char *name, struct stat *sp)
3884b88c807SRodney W. Grimes {
3894b88c807SRodney W. Grimes int ch, first;
390141d77b8SJosef Karthauser char modep[15], *flagsp;
3914b88c807SRodney W. Grimes
3924b88c807SRodney W. Grimes /* Check -i first. */
3934b88c807SRodney W. Grimes if (iflag)
3944b88c807SRodney W. Grimes (void)fprintf(stderr, "remove %s? ", path);
3954b88c807SRodney W. Grimes else {
3964b88c807SRodney W. Grimes /*
3974b88c807SRodney W. Grimes * If it's not a symbolic link and it's unwritable and we're
3984b88c807SRodney W. Grimes * talking to a terminal, ask. Symbolic links are excluded
3994b88c807SRodney W. Grimes * because their permissions are meaningless. Check stdin_ok
4004b88c807SRodney W. Grimes * first because we may not have stat'ed the file.
4014b88c807SRodney W. Grimes */
402a5f62950SDoug Barton if (!stdin_ok || S_ISLNK(sp->st_mode) ||
4031f64b5c9SSteve Price (!access(name, W_OK) &&
4046b419813SAndrey A. Chernov !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
4051f64b5c9SSteve Price (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid)))
4064b88c807SRodney W. Grimes return (1);
4074b88c807SRodney W. Grimes strmode(sp->st_mode, modep);
408141d77b8SJosef Karthauser if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
4095ad9e45fSMatthew Dillon err(1, "fflagstostr");
410141d77b8SJosef Karthauser (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
4116f74a1c9SEnji Cooper modep + 1, modep[10] == ' ' ? "" : " ",
4124b88c807SRodney W. Grimes user_from_uid(sp->st_uid, 0),
4136b419813SAndrey A. Chernov group_from_gid(sp->st_gid, 0),
414141d77b8SJosef Karthauser *flagsp ? flagsp : "", *flagsp ? " " : "",
4156b419813SAndrey A. Chernov path);
416141d77b8SJosef Karthauser free(flagsp);
4174b88c807SRodney W. Grimes }
4184b88c807SRodney W. Grimes (void)fflush(stderr);
4194b88c807SRodney W. Grimes
4204b88c807SRodney W. Grimes first = ch = getchar();
4214b88c807SRodney W. Grimes while (ch != '\n' && ch != EOF)
4224b88c807SRodney W. Grimes ch = getchar();
423b0205affSWolfram Schneider return (first == 'y' || first == 'Y');
4244b88c807SRodney W. Grimes }
4254b88c807SRodney W. Grimes
42668ef5f71SDag-Erling Smørgrav #define ISSLASH(a) ((a)[0] == '/' && (a)[1] == '\0')
4279d6d3f96SEitan Adler static void
checkslash(char ** argv)42868ef5f71SDag-Erling Smørgrav checkslash(char **argv)
42968ef5f71SDag-Erling Smørgrav {
43068ef5f71SDag-Erling Smørgrav char **t, **u;
43168ef5f71SDag-Erling Smørgrav int complained;
43268ef5f71SDag-Erling Smørgrav
43368ef5f71SDag-Erling Smørgrav complained = 0;
43468ef5f71SDag-Erling Smørgrav for (t = argv; *t;) {
43568ef5f71SDag-Erling Smørgrav if (ISSLASH(*t)) {
43668ef5f71SDag-Erling Smørgrav if (!complained++)
43768ef5f71SDag-Erling Smørgrav warnx("\"/\" may not be removed");
43868ef5f71SDag-Erling Smørgrav eval = 1;
43968ef5f71SDag-Erling Smørgrav for (u = t; u[0] != NULL; ++u)
44068ef5f71SDag-Erling Smørgrav u[0] = u[1];
44168ef5f71SDag-Erling Smørgrav } else {
44268ef5f71SDag-Erling Smørgrav ++t;
44368ef5f71SDag-Erling Smørgrav }
44468ef5f71SDag-Erling Smørgrav }
44568ef5f71SDag-Erling Smørgrav }
44668ef5f71SDag-Erling Smørgrav
4479d6d3f96SEitan Adler static int
check2(char ** argv)44824c0f738SXin LI check2(char **argv)
44924c0f738SXin LI {
45024c0f738SXin LI struct stat st;
45124c0f738SXin LI int first;
45224c0f738SXin LI int ch;
45324c0f738SXin LI int fcount = 0;
45424c0f738SXin LI int dcount = 0;
45524c0f738SXin LI int i;
45624c0f738SXin LI const char *dname = NULL;
45724c0f738SXin LI
45824c0f738SXin LI for (i = 0; argv[i]; ++i) {
45924c0f738SXin LI if (lstat(argv[i], &st) == 0) {
46024c0f738SXin LI if (S_ISDIR(st.st_mode)) {
46124c0f738SXin LI ++dcount;
46224c0f738SXin LI dname = argv[i]; /* only used if 1 dir */
46324c0f738SXin LI } else {
46424c0f738SXin LI ++fcount;
46524c0f738SXin LI }
46624c0f738SXin LI }
46724c0f738SXin LI }
46824c0f738SXin LI first = 0;
46924c0f738SXin LI while (first != 'n' && first != 'N' && first != 'y' && first != 'Y') {
47024c0f738SXin LI if (dcount && rflag) {
47124c0f738SXin LI fprintf(stderr, "recursively remove");
47224c0f738SXin LI if (dcount == 1)
47324c0f738SXin LI fprintf(stderr, " %s", dname);
47424c0f738SXin LI else
47524c0f738SXin LI fprintf(stderr, " %d dirs", dcount);
47624c0f738SXin LI if (fcount == 1)
47724c0f738SXin LI fprintf(stderr, " and 1 file");
47824c0f738SXin LI else if (fcount > 1)
47924c0f738SXin LI fprintf(stderr, " and %d files", fcount);
48024c0f738SXin LI } else if (dcount + fcount > 3) {
48124c0f738SXin LI fprintf(stderr, "remove %d files", dcount + fcount);
48224c0f738SXin LI } else {
48324c0f738SXin LI return(1);
48424c0f738SXin LI }
48524c0f738SXin LI fprintf(stderr, "? ");
48624c0f738SXin LI fflush(stderr);
48724c0f738SXin LI
48824c0f738SXin LI first = ch = getchar();
48924c0f738SXin LI while (ch != '\n' && ch != EOF)
49024c0f738SXin LI ch = getchar();
49124c0f738SXin LI if (ch == EOF)
49224c0f738SXin LI break;
49324c0f738SXin LI }
49424c0f738SXin LI return (first == 'y' || first == 'Y');
49524c0f738SXin LI }
49624c0f738SXin LI
49767a3d3a8SPoul-Henning Kamp #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
4989d6d3f96SEitan Adler static void
checkdot(char ** argv)49946251ddeSWarner Losh checkdot(char **argv)
5004b88c807SRodney W. Grimes {
5014b88c807SRodney W. Grimes char *p, **save, **t;
5024b88c807SRodney W. Grimes int complained;
5034b88c807SRodney W. Grimes
5044b88c807SRodney W. Grimes complained = 0;
5054b88c807SRodney W. Grimes for (t = argv; *t;) {
5064b88c807SRodney W. Grimes if ((p = strrchr(*t, '/')) != NULL)
5074b88c807SRodney W. Grimes ++p;
5084b88c807SRodney W. Grimes else
5094b88c807SRodney W. Grimes p = *t;
5104b88c807SRodney W. Grimes if (ISDOT(p)) {
5114b88c807SRodney W. Grimes if (!complained++)
5124b88c807SRodney W. Grimes warnx("\".\" and \"..\" may not be removed");
5134b88c807SRodney W. Grimes eval = 1;
5141f64b5c9SSteve Price for (save = t; (t[0] = t[1]) != NULL; ++t)
5151f64b5c9SSteve Price continue;
5164b88c807SRodney W. Grimes t = save;
5174b88c807SRodney W. Grimes } else
5184b88c807SRodney W. Grimes ++t;
5194b88c807SRodney W. Grimes }
5204b88c807SRodney W. Grimes }
5214b88c807SRodney W. Grimes
5229d6d3f96SEitan Adler static void
usage(void)52346251ddeSWarner Losh usage(void)
5244b88c807SRodney W. Grimes {
5250efa2040SMichael Haro
526d71e172aSSheldon Hearn (void)fprintf(stderr, "%s\n%s\n",
527d4319e74SEitan Adler "usage: rm [-f | -i] [-dIPRrvWx] file ...",
52819b4f0dcSEd Maste " unlink [--] file");
529bfbdd545SMichael Haro exit(EX_USAGE);
5304b88c807SRodney W. Grimes }
531b5260db6SWarner Losh
532b5260db6SWarner Losh static void
siginfo(int sig __unused)533b5260db6SWarner Losh siginfo(int sig __unused)
534b5260db6SWarner Losh {
535b5260db6SWarner Losh
536b5260db6SWarner Losh info = 1;
537b5260db6SWarner Losh }
538