rm.c revision 1.16 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.
32 */
33
34 #ifndef lint
35 static char copyright[] =
36 "@(#) Copyright (c) 1990, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)rm.c 8.5 (Berkeley) 4/18/94";*/
42 static char *rcsid = "$Id: rm.c,v 1.16 1994/11/02 16:17:14 jtc Exp $";
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47
48 #include <locale.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <fts.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57
58 int dflag, eval, fflag, iflag, Pflag, stdin_ok;
59
60 int check __P((char *, char *, struct stat *));
61 void checkdot __P((char **));
62 void rm_file __P((char **));
63 void rm_overwrite __P((char *, struct stat *));
64 void rm_tree __P((char **));
65 void usage __P((void));
66
67 /*
68 * rm --
69 * This rm is different from historic rm's, but is expected to match
70 * POSIX 1003.2 behavior. The most visible difference is that -f
71 * has two specific effects now, ignore non-existent files and force
72 * file removal.
73 */
74 int
75 main(argc, argv)
76 int argc;
77 char *argv[];
78 {
79 int ch, rflag;
80
81 setlocale(LC_ALL, "");
82
83 Pflag = rflag = 0;
84 while ((ch = getopt(argc, argv, "dfiPRr")) != -1)
85 switch(ch) {
86 case 'd':
87 dflag = 1;
88 break;
89 case 'f':
90 fflag = 1;
91 iflag = 0;
92 break;
93 case 'i':
94 fflag = 0;
95 iflag = 1;
96 break;
97 case 'P':
98 Pflag = 1;
99 break;
100 case 'R':
101 case 'r': /* Compatibility. */
102 rflag = 1;
103 break;
104 case '?':
105 default:
106 usage();
107 }
108 argc -= optind;
109 argv += optind;
110
111 if (argc < 1)
112 usage();
113
114 checkdot(argv);
115
116 if (*argv) {
117 stdin_ok = isatty(STDIN_FILENO);
118
119 if (rflag)
120 rm_tree(argv);
121 else
122 rm_file(argv);
123 }
124
125 exit (eval);
126 }
127
128 void
129 rm_tree(argv)
130 char **argv;
131 {
132 FTS *fts;
133 FTSENT *p;
134 int needstat;
135
136 /*
137 * Remove a file hierarchy. If forcing removal (-f), or interactive
138 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
139 */
140 needstat = !fflag && !iflag && stdin_ok;
141
142 /*
143 * If the -i option is specified, the user can skip on the pre-order
144 * visit. The fts_number field flags skipped directories.
145 */
146 #define SKIPPED 1
147
148 if (!(fts = fts_open(argv,
149 needstat ? FTS_PHYSICAL : FTS_PHYSICAL|FTS_NOSTAT,
150 (int (*)())NULL)))
151 err(1, NULL);
152 while ((p = fts_read(fts)) != NULL) {
153 switch (p->fts_info) {
154 case FTS_DNR:
155 if (!fflag || p->fts_errno != ENOENT) {
156 warnx("%s: %s",
157 p->fts_path, strerror(p->fts_errno));
158 eval = 1;
159 }
160 continue;
161 case FTS_ERR:
162 errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
163 case FTS_NS:
164 /*
165 * FTS_NS: assume that if can't stat the file, it
166 * can't be unlinked.
167 */
168 if (!needstat)
169 break;
170 if (!fflag || p->fts_errno != ENOENT) {
171 warnx("%s: %s",
172 p->fts_path, strerror(p->fts_errno));
173 eval = 1;
174 }
175 continue;
176 case FTS_D:
177 /* Pre-order: give user chance to skip. */
178 if (!fflag && !check(p->fts_path, p->fts_accpath,
179 p->fts_statp)) {
180 (void)fts_set(fts, p, FTS_SKIP);
181 p->fts_number = SKIPPED;
182 }
183 continue;
184 case FTS_DP:
185 /* Post-order: see if user skipped. */
186 if (p->fts_number == SKIPPED)
187 continue;
188 break;
189 default:
190 if (!fflag &&
191 !check(p->fts_path, p->fts_accpath, p->fts_statp))
192 continue;
193 }
194
195 /*
196 * If we can't read or search the directory, may still be
197 * able to remove it. Don't print out the un{read,search}able
198 * message unless the remove fails.
199 */
200 if (p->fts_info == FTS_DP || p->fts_info == FTS_DNR) {
201 if (!rmdir(p->fts_accpath) || fflag && errno == ENOENT)
202 continue;
203 } else {
204 if (Pflag)
205 rm_overwrite(p->fts_accpath, NULL);
206 if (!unlink(p->fts_accpath) || fflag && errno == ENOENT)
207 continue;
208 }
209 warn("%s", p->fts_path);
210 eval = 1;
211 }
212 if (errno)
213 err(1, "fts_read");
214 }
215
216 void
217 rm_file(argv)
218 char **argv;
219 {
220 struct stat sb;
221 int rval;
222 char *f;
223
224 /*
225 * Remove a file. POSIX 1003.2 states that, by default, attempting
226 * to remove a directory is an error, so must always stat the file.
227 */
228 while ((f = *argv++) != NULL) {
229 /* Assume if can't stat the file, can't unlink it. */
230 if (lstat(f, &sb)) {
231 if (!fflag || errno != ENOENT) {
232 warn("%s", f);
233 eval = 1;
234 }
235 continue;
236 }
237 if (S_ISDIR(sb.st_mode) && !dflag) {
238 warnx("%s: is a directory", f);
239 eval = 1;
240 continue;
241 }
242 if (!fflag && !check(f, f, &sb))
243 continue;
244 if (S_ISDIR(sb.st_mode))
245 rval = rmdir(f);
246 else {
247 if (Pflag)
248 rm_overwrite(f, &sb);
249 rval = unlink(f);
250 }
251 if (rval && (!fflag || errno != ENOENT)) {
252 warn("%s", f);
253 eval = 1;
254 }
255 }
256 }
257
258 /*
259 * rm_overwrite --
260 * Overwrite the file 3 times with varying bit patterns.
261 *
262 * XXX
263 * This is a cheap way to *really* delete files. Note that only regular
264 * files are deleted, directories (and therefore names) will remain.
265 * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
266 * System V file system). In a logging file system, you'll have to have
267 * kernel support.
268 */
269 void
270 rm_overwrite(file, sbp)
271 char *file;
272 struct stat *sbp;
273 {
274 struct stat sb;
275 off_t len;
276 int fd, wlen;
277 char buf[8 * 1024];
278
279 fd = -1;
280 if (sbp == NULL) {
281 if (lstat(file, &sb))
282 goto err;
283 sbp = &sb;
284 }
285 if (!S_ISREG(sbp->st_mode))
286 return;
287 if ((fd = open(file, O_WRONLY, 0)) == -1)
288 goto err;
289
290 #define PASS(byte) { \
291 memset(buf, byte, sizeof(buf)); \
292 for (len = sbp->st_size; len > 0; len -= wlen) { \
293 wlen = len < sizeof(buf) ? len : sizeof(buf); \
294 if (write(fd, buf, wlen) != wlen) \
295 goto err; \
296 } \
297 }
298 PASS(0xff);
299 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
300 goto err;
301 PASS(0x00);
302 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
303 goto err;
304 PASS(0xff);
305 if (!fsync(fd) && !close(fd))
306 return;
307
308 err: eval = 1;
309 warn("%s", file);
310 }
311
312
313 int
314 check(path, name, sp)
315 char *path, *name;
316 struct stat *sp;
317 {
318 int ch, first;
319 char modep[15];
320
321 /* Check -i first. */
322 if (iflag)
323 (void)fprintf(stderr, "remove %s? ", path);
324 else {
325 /*
326 * If it's not a symbolic link and it's unwritable and we're
327 * talking to a terminal, ask. Symbolic links are excluded
328 * because their permissions are meaningless. Check stdin_ok
329 * first because we may not have stat'ed the file.
330 */
331 if (!stdin_ok || S_ISLNK(sp->st_mode) || !access(name, W_OK))
332 return (1);
333 strmode(sp->st_mode, modep);
334 (void)fprintf(stderr, "override %s%s%s/%s for %s? ",
335 modep + 1, modep[9] == ' ' ? "" : " ",
336 user_from_uid(sp->st_uid, 0),
337 group_from_gid(sp->st_gid, 0), path);
338 }
339 (void)fflush(stderr);
340
341 first = ch = getchar();
342 while (ch != '\n' && ch != EOF)
343 ch = getchar();
344 return (first == 'y' || first == 'Y');
345 }
346
347
348 /*
349 * POSIX.2 requires that if "." or ".." are specified as the basename
350 * portion of an operand, a diagnostic message be written to standard
351 * error and nothing more be done with such operands.
352 *
353 * Since POSIX.2 defines basename as the final portion of a path after
354 * trailing slashes have been removed, we'll remove them here.
355 */
356
357 #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || (a)[1] == '.' && !(a)[2]))
358 void
359 checkdot(argv)
360 char **argv;
361 {
362 char *p, **save, **t;
363 int complained;
364
365 complained = 0;
366 for (t = argv; *t;) {
367 /* strip trailing slashes */
368 p = strrchr (*t, '\0');
369 while (--p > *t && *p == '/')
370 *p = '\0';
371
372 /* extract basename */
373 if ((p = strrchr(*t, '/')) != NULL)
374 ++p;
375 else
376 p = *t;
377
378 if (ISDOT(p)) {
379 if (!complained++)
380 warnx("\".\" and \"..\" may not be removed");
381 eval = 1;
382 for (save = t; (t[0] = t[1]) != NULL; ++t)
383 continue;
384 t = save;
385 } else
386 ++t;
387 }
388 }
389
390 void
391 usage()
392 {
393
394 (void)fprintf(stderr, "usage: rm [-dfiPRr] file ...\n");
395 exit(1);
396 }
397