rm.c revision 1.4 1 1.1 cgd /*-
2 1.1 cgd * Copyright (c) 1990 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.1 cgd char copyright[] =
36 1.1 cgd "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
37 1.1 cgd All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.1 cgd static char sccsid[] = "@(#)rm.c 4.26 (Berkeley) 3/10/91";
42 1.4 mycroft static char rcsid[] = "$Header: /tank/opengrok/rsync2/NetBSD/src/bin/rm/rm.c,v 1.4 1993/04/10 00:57:03 mycroft Exp $";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/types.h>
46 1.1 cgd #include <sys/stat.h>
47 1.1 cgd #include <sys/errno.h>
48 1.1 cgd #include <fts.h>
49 1.1 cgd #include <unistd.h>
50 1.1 cgd #include <stdio.h>
51 1.1 cgd #include <string.h>
52 1.1 cgd #include <stdlib.h>
53 1.1 cgd
54 1.1 cgd int dflag, fflag, iflag, retval, stdin_ok;
55 1.1 cgd
56 1.1 cgd /*
57 1.1 cgd * rm --
58 1.1 cgd * This rm is different from historic rm's, but is expected to match
59 1.1 cgd * POSIX 1003.2 behavior. The most visible difference is that -f
60 1.1 cgd * has two specific effects now, ignore non-existent files and force
61 1.1 cgd * file removal.
62 1.1 cgd */
63 1.1 cgd
64 1.1 cgd main(argc, argv)
65 1.1 cgd int argc;
66 1.1 cgd char **argv;
67 1.1 cgd {
68 1.1 cgd extern char *optarg;
69 1.1 cgd extern int optind;
70 1.1 cgd int ch, rflag;
71 1.1 cgd
72 1.1 cgd rflag = 0;
73 1.1 cgd while ((ch = getopt(argc, argv, "dfiRr")) != EOF)
74 1.1 cgd switch(ch) {
75 1.1 cgd case 'd':
76 1.1 cgd dflag = 1;
77 1.1 cgd break;
78 1.1 cgd case 'f':
79 1.1 cgd fflag = 1;
80 1.1 cgd iflag = 0;
81 1.1 cgd break;
82 1.1 cgd case 'i':
83 1.1 cgd fflag = 0;
84 1.1 cgd iflag = 1;
85 1.1 cgd break;
86 1.1 cgd case 'R':
87 1.1 cgd case 'r': /* compatibility */
88 1.1 cgd rflag = 1;
89 1.1 cgd break;
90 1.1 cgd case '?':
91 1.1 cgd default:
92 1.1 cgd usage();
93 1.1 cgd }
94 1.1 cgd argc -= optind;
95 1.1 cgd argv += optind;
96 1.1 cgd
97 1.1 cgd if (argc < 1)
98 1.1 cgd usage();
99 1.1 cgd
100 1.1 cgd checkdot(argv);
101 1.1 cgd if (!*argv)
102 1.4 mycroft exit(fflag ? 0 : retval);
103 1.1 cgd
104 1.1 cgd stdin_ok = isatty(STDIN_FILENO);
105 1.1 cgd
106 1.1 cgd if (rflag)
107 1.1 cgd rmtree(argv);
108 1.1 cgd else
109 1.1 cgd rmfile(argv);
110 1.4 mycroft exit(fflag ? 0 : retval);
111 1.1 cgd }
112 1.1 cgd
113 1.1 cgd rmtree(argv)
114 1.1 cgd char **argv;
115 1.1 cgd {
116 1.1 cgd register FTS *fts;
117 1.1 cgd register FTSENT *p;
118 1.1 cgd register int needstat;
119 1.1 cgd struct stat sb;
120 1.1 cgd
121 1.1 cgd /*
122 1.1 cgd * Remove a file hierarchy. If forcing removal (-f), or interactive
123 1.1 cgd * (-i) or can't ask anyway (stdin_ok), don't stat the file.
124 1.1 cgd */
125 1.1 cgd needstat = !fflag && !iflag && stdin_ok;
126 1.1 cgd
127 1.1 cgd /*
128 1.1 cgd * If the -i option is specified, the user can skip on the pre-order
129 1.1 cgd * visit. The fts_number field flags skipped directories.
130 1.1 cgd */
131 1.1 cgd #define SKIPPED 1
132 1.1 cgd
133 1.1 cgd if (!(fts = fts_open(argv,
134 1.1 cgd needstat ? FTS_PHYSICAL : FTS_PHYSICAL|FTS_NOSTAT,
135 1.4 mycroft (int (*)())NULL)) && !fflag) {
136 1.1 cgd (void)fprintf(stderr, "rm: %s.\n", strerror(errno));
137 1.1 cgd exit(1);
138 1.1 cgd }
139 1.1 cgd while (p = fts_read(fts)) {
140 1.1 cgd switch(p->fts_info) {
141 1.1 cgd case FTS_DNR:
142 1.1 cgd case FTS_ERR:
143 1.1 cgd error(p->fts_path, errno);
144 1.1 cgd exit(1);
145 1.1 cgd /*
146 1.1 cgd * FTS_NS: assume that if can't stat the file, it can't be
147 1.1 cgd * unlinked.
148 1.1 cgd */
149 1.1 cgd case FTS_NS:
150 1.1 cgd if (!needstat)
151 1.1 cgd break;
152 1.1 cgd if (!fflag || errno != ENOENT)
153 1.1 cgd error(p->fts_path, errno);
154 1.1 cgd continue;
155 1.1 cgd /* Pre-order: give user chance to skip. */
156 1.1 cgd case FTS_D:
157 1.1 cgd if (iflag && !check(p->fts_path, p->fts_accpath,
158 1.1 cgd &p->fts_statb)) {
159 1.1 cgd (void)fts_set(fts, p, FTS_SKIP);
160 1.1 cgd p->fts_number = SKIPPED;
161 1.1 cgd }
162 1.1 cgd continue;
163 1.1 cgd /* Post-order: see if user skipped. */
164 1.1 cgd case FTS_DP:
165 1.1 cgd if (p->fts_number == SKIPPED)
166 1.1 cgd continue;
167 1.1 cgd break;
168 1.1 cgd }
169 1.1 cgd
170 1.1 cgd if (!fflag &&
171 1.1 cgd !check(p->fts_path, p->fts_accpath, &p->fts_statb))
172 1.1 cgd continue;
173 1.1 cgd
174 1.1 cgd /*
175 1.1 cgd * If we can't read or search the directory, may still be
176 1.1 cgd * able to remove it. Don't print out the un{read,search}able
177 1.1 cgd * message unless the remove fails.
178 1.1 cgd */
179 1.1 cgd if (p->fts_info == FTS_DP || p->fts_info == FTS_DNR) {
180 1.1 cgd if (!rmdir(p->fts_accpath))
181 1.1 cgd continue;
182 1.1 cgd if (errno == ENOENT) {
183 1.1 cgd if (fflag)
184 1.1 cgd continue;
185 1.4 mycroft } else if (p->fts_info != FTS_DP && !fflag)
186 1.1 cgd (void)fprintf(stderr,
187 1.1 cgd "rm: unable to read %s.\n", p->fts_path);
188 1.1 cgd } else if (!unlink(p->fts_accpath) || fflag && errno == ENOENT)
189 1.1 cgd continue;
190 1.1 cgd error(p->fts_path, errno);
191 1.1 cgd }
192 1.1 cgd }
193 1.1 cgd
194 1.1 cgd rmfile(argv)
195 1.1 cgd char **argv;
196 1.1 cgd {
197 1.1 cgd register int df;
198 1.1 cgd register char *f;
199 1.1 cgd struct stat sb;
200 1.1 cgd
201 1.1 cgd df = dflag;
202 1.1 cgd /*
203 1.1 cgd * Remove a file. POSIX 1003.2 states that, by default, attempting
204 1.1 cgd * to remove a directory is an error, so must always stat the file.
205 1.1 cgd */
206 1.1 cgd while (f = *argv++) {
207 1.1 cgd /* Assume if can't stat the file, can't unlink it. */
208 1.1 cgd if (lstat(f, &sb)) {
209 1.1 cgd if (!fflag || errno != ENOENT)
210 1.1 cgd error(f, errno);
211 1.1 cgd continue;
212 1.1 cgd }
213 1.4 mycroft if (S_ISDIR(sb.st_mode) && !df && !fflag) {
214 1.1 cgd (void)fprintf(stderr, "rm: %s: is a directory\n", f);
215 1.1 cgd retval = 1;
216 1.1 cgd continue;
217 1.1 cgd }
218 1.1 cgd if (!fflag && !check(f, f, &sb))
219 1.1 cgd continue;
220 1.1 cgd if ((S_ISDIR(sb.st_mode) ? rmdir(f) : unlink(f)) &&
221 1.1 cgd (!fflag || errno != ENOENT))
222 1.1 cgd error(f, errno);
223 1.1 cgd }
224 1.1 cgd }
225 1.1 cgd
226 1.1 cgd check(path, name, sp)
227 1.1 cgd char *path, *name;
228 1.1 cgd struct stat *sp;
229 1.1 cgd {
230 1.1 cgd register int first, ch;
231 1.1 cgd char modep[15], *user_from_uid(), *group_from_gid();
232 1.1 cgd
233 1.1 cgd /* Check -i first. */
234 1.1 cgd if (iflag)
235 1.1 cgd (void)fprintf(stderr, "remove %s? ", path);
236 1.1 cgd else {
237 1.1 cgd /*
238 1.1 cgd * If it's not a symbolic link and it's unwritable and we're
239 1.1 cgd * talking to a terminal, ask. Symbolic links are excluded
240 1.1 cgd * because their permissions are meaningless.
241 1.1 cgd */
242 1.1 cgd if (S_ISLNK(sp->st_mode) || !stdin_ok || !access(name, W_OK))
243 1.1 cgd return(1);
244 1.1 cgd strmode(sp->st_mode, modep);
245 1.4 mycroft if (!fflag)
246 1.4 mycroft (void)fprintf(stderr, "override %s%s%s/%s for %s? ",
247 1.4 mycroft modep + 1, modep[9] == ' ' ? "" : " ",
248 1.4 mycroft user_from_uid(sp->st_uid, 0),
249 1.4 mycroft group_from_gid(sp->st_gid, 0), path);
250 1.1 cgd }
251 1.1 cgd (void)fflush(stderr);
252 1.1 cgd
253 1.1 cgd first = ch = getchar();
254 1.1 cgd while (ch != '\n' && ch != EOF)
255 1.1 cgd ch = getchar();
256 1.1 cgd return(first == 'y');
257 1.1 cgd }
258 1.1 cgd
259 1.1 cgd #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || (a)[1] == '.' && !(a)[2]))
260 1.1 cgd checkdot(argv)
261 1.1 cgd char **argv;
262 1.1 cgd {
263 1.1 cgd register char *p, **t, **save;
264 1.1 cgd int complained;
265 1.1 cgd
266 1.1 cgd complained = 0;
267 1.1 cgd for (t = argv; *t;) {
268 1.1 cgd if (p = rindex(*t, '/'))
269 1.1 cgd ++p;
270 1.1 cgd else
271 1.1 cgd p = *t;
272 1.1 cgd if (ISDOT(p)) {
273 1.4 mycroft if (!complained++ && !fflag)
274 1.1 cgd (void)fprintf(stderr,
275 1.1 cgd "rm: \".\" and \"..\" may not be removed.\n");
276 1.1 cgd retval = 1;
277 1.1 cgd for (save = t; t[0] = t[1]; ++t);
278 1.1 cgd t = save;
279 1.1 cgd } else
280 1.1 cgd ++t;
281 1.1 cgd }
282 1.1 cgd }
283 1.1 cgd
284 1.1 cgd error(name, val)
285 1.1 cgd char *name;
286 1.1 cgd int val;
287 1.1 cgd {
288 1.4 mycroft if (!fflag)
289 1.4 mycroft (void)fprintf(stderr, "rm: %s: %s.\n", name, strerror(val));
290 1.1 cgd retval = 1;
291 1.1 cgd }
292 1.1 cgd
293 1.1 cgd usage()
294 1.1 cgd {
295 1.4 mycroft if (!fflag)
296 1.4 mycroft (void)fprintf(stderr, "usage: rm [-dfiRr] file ...\n");
297 1.1 cgd exit(1);
298 1.1 cgd }
299