rm.c revision 1.14 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.5 mycroft /*static char sccsid[] = "from: @(#)rm.c 4.26 (Berkeley) 3/10/91";*/
42 1.14 jtc static char rcsid[] = "$Id: rm.c,v 1.14 1994/03/16 17:49:40 jtc Exp $";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.7 jtc #include <stdio.h>
46 1.7 jtc #include <string.h>
47 1.7 jtc #include <stdlib.h>
48 1.7 jtc #include <locale.h>
49 1.7 jtc #include <errno.h>
50 1.1 cgd #include <sys/types.h>
51 1.1 cgd #include <sys/stat.h>
52 1.7 jtc #include <unistd.h>
53 1.1 cgd #include <fts.h>
54 1.7 jtc
55 1.7 jtc int check __P((char *, char *, struct stat *));
56 1.7 jtc void checkdot __P((char **));
57 1.7 jtc void error __P((char *, int));
58 1.7 jtc void rmtree __P((char **));
59 1.7 jtc void rmfile __P((char **));
60 1.7 jtc void usage __P((void));
61 1.1 cgd
62 1.1 cgd int dflag, fflag, iflag, retval, stdin_ok;
63 1.1 cgd
64 1.1 cgd /*
65 1.1 cgd * rm --
66 1.1 cgd * This rm is different from historic rm's, but is expected to match
67 1.1 cgd * POSIX 1003.2 behavior. The most visible difference is that -f
68 1.1 cgd * has two specific effects now, ignore non-existent files and force
69 1.1 cgd * file removal.
70 1.1 cgd */
71 1.1 cgd
72 1.7 jtc int
73 1.1 cgd main(argc, argv)
74 1.1 cgd int argc;
75 1.1 cgd char **argv;
76 1.1 cgd {
77 1.1 cgd int ch, rflag;
78 1.1 cgd
79 1.7 jtc setlocale(LC_ALL, "");
80 1.7 jtc
81 1.1 cgd rflag = 0;
82 1.11 jtc while ((ch = getopt(argc, argv, "dfiRr")) != -1)
83 1.1 cgd switch(ch) {
84 1.1 cgd case 'd':
85 1.1 cgd dflag = 1;
86 1.1 cgd break;
87 1.1 cgd case 'f':
88 1.1 cgd fflag = 1;
89 1.1 cgd iflag = 0;
90 1.1 cgd break;
91 1.1 cgd case 'i':
92 1.1 cgd fflag = 0;
93 1.1 cgd iflag = 1;
94 1.1 cgd break;
95 1.1 cgd case 'R':
96 1.1 cgd case 'r': /* compatibility */
97 1.1 cgd rflag = 1;
98 1.1 cgd break;
99 1.1 cgd case '?':
100 1.1 cgd default:
101 1.1 cgd usage();
102 1.1 cgd }
103 1.1 cgd argc -= optind;
104 1.1 cgd argv += optind;
105 1.1 cgd
106 1.1 cgd if (argc < 1)
107 1.1 cgd usage();
108 1.1 cgd
109 1.1 cgd checkdot(argv);
110 1.1 cgd
111 1.11 jtc if (*argv) {
112 1.11 jtc stdin_ok = isatty(STDIN_FILENO);
113 1.1 cgd
114 1.11 jtc if (rflag)
115 1.11 jtc rmtree(argv);
116 1.11 jtc else
117 1.11 jtc rmfile(argv);
118 1.11 jtc }
119 1.9 jtc
120 1.9 jtc exit(retval);
121 1.1 cgd }
122 1.1 cgd
123 1.7 jtc void
124 1.1 cgd rmtree(argv)
125 1.1 cgd char **argv;
126 1.1 cgd {
127 1.1 cgd register FTS *fts;
128 1.1 cgd register FTSENT *p;
129 1.1 cgd register int needstat;
130 1.1 cgd
131 1.1 cgd /*
132 1.1 cgd * Remove a file hierarchy. If forcing removal (-f), or interactive
133 1.1 cgd * (-i) or can't ask anyway (stdin_ok), don't stat the file.
134 1.1 cgd */
135 1.1 cgd needstat = !fflag && !iflag && stdin_ok;
136 1.1 cgd
137 1.1 cgd /*
138 1.1 cgd * If the -i option is specified, the user can skip on the pre-order
139 1.1 cgd * visit. The fts_number field flags skipped directories.
140 1.1 cgd */
141 1.1 cgd #define SKIPPED 1
142 1.1 cgd
143 1.1 cgd if (!(fts = fts_open(argv,
144 1.1 cgd needstat ? FTS_PHYSICAL : FTS_PHYSICAL|FTS_NOSTAT,
145 1.9 jtc (int (*)())NULL))) {
146 1.13 jtc (void)fprintf(stderr, "rm: %s.\n", strerror(errno));
147 1.9 jtc exit (1);
148 1.1 cgd }
149 1.9 jtc
150 1.7 jtc while ((p = fts_read(fts)) != NULL) {
151 1.1 cgd switch(p->fts_info) {
152 1.1 cgd case FTS_DNR:
153 1.1 cgd case FTS_ERR:
154 1.1 cgd error(p->fts_path, errno);
155 1.11 jtc continue;
156 1.1 cgd /*
157 1.1 cgd * FTS_NS: assume that if can't stat the file, it can't be
158 1.1 cgd * unlinked.
159 1.1 cgd */
160 1.1 cgd case FTS_NS:
161 1.1 cgd if (!needstat)
162 1.1 cgd break;
163 1.1 cgd if (!fflag || errno != ENOENT)
164 1.1 cgd error(p->fts_path, errno);
165 1.1 cgd continue;
166 1.1 cgd /* Pre-order: give user chance to skip. */
167 1.1 cgd case FTS_D:
168 1.8 jtc if (!fflag && !check(p->fts_path, p->fts_accpath,
169 1.6 deraadt p->fts_statp)) {
170 1.1 cgd (void)fts_set(fts, p, FTS_SKIP);
171 1.1 cgd p->fts_number = SKIPPED;
172 1.1 cgd }
173 1.1 cgd continue;
174 1.1 cgd /* Post-order: see if user skipped. */
175 1.1 cgd case FTS_DP:
176 1.1 cgd if (p->fts_number == SKIPPED)
177 1.1 cgd continue;
178 1.1 cgd break;
179 1.9 jtc
180 1.9 jtc default:
181 1.9 jtc if (!fflag && !check(p->fts_path, p->fts_accpath,
182 1.9 jtc p->fts_statp))
183 1.9 jtc continue;
184 1.1 cgd }
185 1.1 cgd
186 1.1 cgd /*
187 1.1 cgd * If we can't read or search the directory, may still be
188 1.1 cgd * able to remove it. Don't print out the un{read,search}able
189 1.1 cgd * message unless the remove fails.
190 1.1 cgd */
191 1.1 cgd if (p->fts_info == FTS_DP || p->fts_info == FTS_DNR) {
192 1.13 jtc if (!rmdir(p->fts_accpath) || fflag && errno == ENOENT)
193 1.1 cgd continue;
194 1.14 jtc } else {
195 1.14 jtc if (!unlink(p->fts_accpath) || fflag && errno == ENOENT)
196 1.14 jtc continue;
197 1.14 jtc }
198 1.1 cgd error(p->fts_path, errno);
199 1.1 cgd }
200 1.1 cgd }
201 1.1 cgd
202 1.7 jtc void
203 1.1 cgd rmfile(argv)
204 1.1 cgd char **argv;
205 1.1 cgd {
206 1.1 cgd register char *f;
207 1.1 cgd struct stat sb;
208 1.1 cgd
209 1.1 cgd /*
210 1.1 cgd * Remove a file. POSIX 1003.2 states that, by default, attempting
211 1.1 cgd * to remove a directory is an error, so must always stat the file.
212 1.1 cgd */
213 1.7 jtc while ((f = *argv++) != NULL) {
214 1.7 jtc /* If the file does not exist:
215 1.7 jtc * If the -f option was not specified, write a diagnostic
216 1.7 jtc * to the standard error...
217 1.7 jtc */
218 1.1 cgd if (lstat(f, &sb)) {
219 1.9 jtc if (!fflag || errno != ENOENT) {
220 1.9 jtc error(f, errno);
221 1.9 jtc }
222 1.1 cgd continue;
223 1.1 cgd }
224 1.7 jtc
225 1.7 jtc /* If the file is of type directory and neither the -r or -R
226 1.7 jtc * (or -d) options are specified, write a diagnostic to the
227 1.7 jtc * standard error...
228 1.7 jtc */
229 1.7 jtc if (S_ISDIR(sb.st_mode) && !dflag) {
230 1.7 jtc error (f, EISDIR);
231 1.1 cgd continue;
232 1.1 cgd }
233 1.7 jtc
234 1.7 jtc if (!fflag && !check(f, f, &sb)) {
235 1.1 cgd continue;
236 1.7 jtc }
237 1.7 jtc
238 1.7 jtc /*
239 1.7 jtc * rmdir() directories, unlink() files...
240 1.7 jtc */
241 1.7 jtc if ((S_ISDIR(sb.st_mode) ? rmdir(f) : unlink(f))) {
242 1.1 cgd error(f, errno);
243 1.7 jtc }
244 1.1 cgd }
245 1.1 cgd }
246 1.1 cgd
247 1.7 jtc int
248 1.1 cgd check(path, name, sp)
249 1.1 cgd char *path, *name;
250 1.1 cgd struct stat *sp;
251 1.1 cgd {
252 1.1 cgd register int first, ch;
253 1.1 cgd char modep[15], *user_from_uid(), *group_from_gid();
254 1.1 cgd
255 1.1 cgd /* Check -i first. */
256 1.1 cgd if (iflag)
257 1.1 cgd (void)fprintf(stderr, "remove %s? ", path);
258 1.1 cgd else {
259 1.1 cgd /*
260 1.1 cgd * If it's not a symbolic link and it's unwritable and we're
261 1.1 cgd * talking to a terminal, ask. Symbolic links are excluded
262 1.1 cgd * because their permissions are meaningless.
263 1.1 cgd */
264 1.12 jtc if (!stdin_ok || S_ISLNK(sp->st_mode) || !access(name, W_OK))
265 1.1 cgd return(1);
266 1.1 cgd strmode(sp->st_mode, modep);
267 1.8 jtc (void)fprintf(stderr, "override %s%s%s/%s for %s? ",
268 1.8 jtc modep + 1, modep[9] == ' ' ? "" : " ",
269 1.8 jtc user_from_uid(sp->st_uid, 0),
270 1.8 jtc group_from_gid(sp->st_gid, 0), path);
271 1.1 cgd }
272 1.1 cgd (void)fflush(stderr);
273 1.1 cgd
274 1.1 cgd first = ch = getchar();
275 1.1 cgd while (ch != '\n' && ch != EOF)
276 1.1 cgd ch = getchar();
277 1.10 jtc return(first == 'y' || first == 'Y');
278 1.1 cgd }
279 1.1 cgd
280 1.1 cgd #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || (a)[1] == '.' && !(a)[2]))
281 1.7 jtc void
282 1.1 cgd checkdot(argv)
283 1.1 cgd char **argv;
284 1.1 cgd {
285 1.1 cgd register char *p, **t, **save;
286 1.1 cgd int complained;
287 1.1 cgd
288 1.1 cgd complained = 0;
289 1.1 cgd for (t = argv; *t;) {
290 1.7 jtc if ((p = rindex(*t, '/')) != NULL)
291 1.1 cgd ++p;
292 1.1 cgd else
293 1.1 cgd p = *t;
294 1.1 cgd if (ISDOT(p)) {
295 1.8 jtc if (!complained++)
296 1.1 cgd (void)fprintf(stderr,
297 1.1 cgd "rm: \".\" and \"..\" may not be removed.\n");
298 1.1 cgd retval = 1;
299 1.13 jtc for (save = t; t[0] = t[1]; ++t)
300 1.13 jtc continue;
301 1.1 cgd t = save;
302 1.1 cgd } else
303 1.1 cgd ++t;
304 1.1 cgd }
305 1.1 cgd }
306 1.1 cgd
307 1.7 jtc void
308 1.1 cgd error(name, val)
309 1.1 cgd char *name;
310 1.1 cgd int val;
311 1.1 cgd {
312 1.13 jtc (void)fprintf(stderr, "rm: %s: %s.\n", name, strerror(val));
313 1.1 cgd retval = 1;
314 1.1 cgd }
315 1.1 cgd
316 1.7 jtc void
317 1.1 cgd usage()
318 1.1 cgd {
319 1.8 jtc (void)fprintf(stderr, "usage: rm [-dfiRr] file ...\n");
320 1.1 cgd exit(1);
321 1.1 cgd }
322