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