1 /* `rm' file deletion utility for GNU.
2 Copyright (C) 1988, 1990, 1991, 1994, 1995 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by Paul Rubin, David MacKenzie, and Richard Stallman. */
23 #include <sys/types.h>
29 #ifdef D_INO_IN_DIRENT
30 # define D_INO(dp) ((dp)->d_ino)
32 /* Some systems don't have inodes, so fake them to avoid lots of ifdefs. */
36 /* An element in a stack of pointers into `pathname'.
37 `pathp' points to where in `pathname' the terminating '\0' goes
38 for this level's directory name. */
41 struct pathstack *next;
52 void strip_trailing_slashes ();
54 static int clear_directory __P ((struct stat *statp));
55 static int duplicate_entry __P ((struct pathstack *stack, ino_t inum));
56 static int remove_dir __P ((struct stat *statp));
57 static int remove_file __P ((struct stat *statp));
58 static int rm __P ((void));
59 static void usage __P ((int status));
61 /* Name this program was run with. */
64 /* Linked list of pathnames of directories in progress in recursive rm.
65 The entries actually contain pointers into `pathname'.
66 `pathstack' is the current deepest level. */
67 static struct pathstack *pathstack = NULL;
69 /* Path of file now being processed; extended as necessary. */
70 static char *pathname;
72 /* Number of bytes currently allocated for `pathname';
73 made larger when necessary, but never smaller. */
76 /* If nonzero, display the name of each file removed. */
79 /* If nonzero, ignore nonexistant files. */
80 static int ignore_missing_files;
82 /* If nonzero, recursively remove directories. */
85 /* If nonzero, query the user about whether to remove each file. */
86 static int interactive;
88 /* If nonzero, remove directories with unlink instead of rmdir, and don't
89 require a directory to be empty before trying to unlink it.
90 Only works for the super-user. */
91 static int unlink_dirs;
93 /* If nonzero, stdin is a tty. */
96 /* If nonzero, display usage information and exit. */
99 /* If nonzero, print the version on standard output and exit. */
100 static int show_version;
102 static struct option const long_opts[] =
104 {"directory", no_argument, &unlink_dirs, 1},
105 {"force", no_argument, NULL, 'f'},
106 {"interactive", no_argument, NULL, 'i'},
107 {"recursive", no_argument, &recursive, 1},
108 {"verbose", no_argument, &verbose, 1},
109 {"help", no_argument, &show_help, 1},
110 {"version", no_argument, &show_version, 1},
115 main (int argc, char **argv)
120 program_name = argv[0];
121 setlocale (LC_ALL, "");
122 bindtextdomain (PACKAGE, LOCALEDIR);
123 textdomain (PACKAGE);
125 verbose = ignore_missing_files = recursive = interactive
128 pathname = xmalloc (pnsize);
130 while ((c = getopt_long (argc, argv, "dfirvR", long_opts, (int *) 0)) != EOF)
134 case 0: /* Long option. */
141 ignore_missing_files = 1;
145 ignore_missing_files = 0;
161 printf ("rm - %s\n", version_string);
170 if (ignore_missing_files)
174 error (0, 0, _("too few arguments"));
179 stdin_tty = isatty (STDIN_FILENO);
181 for (; optind < argc; optind++)
185 /* Stripping slashes is harmless for rmdir;
186 if the arg is not a directory, it will fail with ENOTDIR. */
187 strip_trailing_slashes (argv[optind]);
188 len = strlen (argv[optind]);
189 if (len + 1 > pnsize)
192 pnsize = 2 * (len + 1);
193 pathname = xmalloc (pnsize);
195 strcpy (pathname, argv[optind]);
202 /* Remove file or directory `pathname' after checking appropriate things.
203 Return 0 if `pathname' is removed, 1 if not. */
208 struct stat path_stats;
209 char *base = basename (pathname);
211 if (base[0] == '.' && (base[1] == '\0'
212 || (base[1] == '.' && base[2] == '\0')))
214 error (0, 0, _("cannot remove `.' or `..'"));
218 if (lstat (pathname, &path_stats)
219 /* The following or-clause is solely for systems like SunOS 4.1.3
220 with (broken) lstat that interpret a zero-length file name
221 argument as something meaningful. For such systems, manually
222 set errno to ENOENT. */
223 || (pathname[0] == '\0' && (errno = ENOENT)))
225 if (errno == ENOENT && ignore_missing_files)
227 error (0, errno, "%s", pathname);
231 if (S_ISDIR (path_stats.st_mode) && !unlink_dirs)
232 return remove_dir (&path_stats);
234 return remove_file (&path_stats);
237 /* Query the user if appropriate, and if ok try to remove the
238 non-directory `pathname', which STATP contains info about.
239 Return 0 if `pathname' is removed, 1 if not. */
242 remove_file (struct stat *statp)
244 if (!ignore_missing_files && (interactive || stdin_tty)
245 && euidaccess (pathname, W_OK)
247 && !S_ISLNK (statp->st_mode)
251 fprintf (stderr, _("%s: remove %s`%s', overriding mode %04o? "),
253 S_ISDIR (statp->st_mode) ? _("directory ") : "",
255 (unsigned int) (statp->st_mode & 07777));
259 else if (interactive)
261 fprintf (stderr, _("%s: remove %s`%s'? "), program_name,
262 S_ISDIR (statp->st_mode) ? _("directory ") : "",
269 printf ("%s\n", pathname);
271 if (unlink (pathname) && (errno != ENOENT || !ignore_missing_files))
273 error (0, errno, "%s", pathname);
279 /* If not in recursive mode, print an error message and return 1.
280 Otherwise, query the user if appropriate, then try to recursively
281 remove directory `pathname', which STATP contains info about.
282 Return 0 if `pathname' is removed, 1 if not. */
285 remove_dir (struct stat *statp)
291 error (0, 0, _("%s: is a directory"), pathname);
295 if (!ignore_missing_files && (interactive || stdin_tty)
296 && euidaccess (pathname, W_OK))
299 _("%s: descend directory `%s', overriding mode %04o? "),
300 program_name, pathname,
301 (unsigned int) (statp->st_mode & 07777));
305 else if (interactive)
307 fprintf (stderr, _("%s: descend directory `%s'? "),
308 program_name, pathname);
314 printf ("%s\n", pathname);
316 err = clear_directory (statp);
321 fprintf (stderr, _("%s: remove directory `%s' (might be nonempty)? "),
322 program_name, pathname);
324 fprintf (stderr, _("%s: remove directory `%s'? "),
325 program_name, pathname);
330 if (rmdir (pathname) && (errno != ENOENT || !ignore_missing_files))
332 error (0, errno, "%s", pathname);
338 /* Read directory `pathname' and remove all of its entries,
339 avoiding use of chdir.
340 On entry, STATP points to the results of stat on `pathname'.
341 Return 0 for success, error count for failure.
342 Upon return, `pathname' will have the same contents as before,
343 but its address might be different; in that case, `pnsize' will
344 be larger, as well. */
347 clear_directory (struct stat *statp)
351 char *name_space; /* Copy of directory's filenames. */
352 char *namep; /* Current entry in `name_space'. */
353 unsigned name_size; /* Bytes allocated for `name_space'. */
354 int name_length; /* Length of filename in `namep' plus '\0'. */
355 int pathname_length; /* Length of `pathname'. */
356 ino_t *inode_space; /* Copy of directory's inodes. */
357 ino_t *inodep; /* Current entry in `inode_space'. */
358 unsigned n_inodes_allocated; /* There is space for this many inodes
360 int err = 0; /* Return status. */
361 struct pathstack pathframe; /* New top of stack. */
362 struct pathstack *pp; /* Temporary. */
364 name_size = statp->st_size;
365 name_space = (char *) xmalloc (name_size);
367 n_inodes_allocated = (statp->st_size + sizeof (ino_t) - 1) / sizeof (ino_t);
368 inode_space = (ino_t *) xmalloc (n_inodes_allocated * sizeof (ino_t));
373 inodep = inode_space;
376 dirp = opendir (pathname);
379 if (errno != ENOENT || !ignore_missing_files)
381 error (0, errno, "%s", pathname);
389 while ((dp = readdir (dirp)) != NULL)
391 /* Skip "." and "..". */
392 if (dp->d_name[0] != '.'
393 || (dp->d_name[1] != '\0'
394 && (dp->d_name[1] != '.' || dp->d_name[2] != '\0')))
396 unsigned size_needed = (namep - name_space) + NLENGTH (dp) + 2;
398 if (size_needed > name_size)
400 char *new_name_space;
402 while (size_needed > name_size)
405 new_name_space = xrealloc (name_space, name_size);
406 namep += new_name_space - name_space;
407 name_space = new_name_space;
409 namep = stpcpy (namep, dp->d_name) + 1;
411 if (inodep == inode_space + n_inodes_allocated)
413 ino_t *new_inode_space;
415 n_inodes_allocated += 1024;
416 new_inode_space = (ino_t *) xrealloc (inode_space,
417 n_inodes_allocated * sizeof (ino_t));
418 inodep += new_inode_space - inode_space;
419 inode_space = new_inode_space;
421 *inodep++ = D_INO (dp);
427 error (0, errno, "%s", pathname);
431 pathname_length = strlen (pathname);
433 for (namep = name_space, inodep = inode_space; *namep != '\0';
434 namep += name_length, inodep++)
436 name_length = strlen (namep) + 1;
438 /* Handle arbitrarily long filenames. */
439 if (pathname_length + 1 + name_length > pnsize)
443 pnsize = (pathname_length + 1 + name_length) * 2;
444 new_pathname = xrealloc (pathname, pnsize);
445 /* Update all pointers in the stack to use the new area. */
446 for (pp = pathstack; pp != NULL; pp = pp->next)
447 pp->pathp += new_pathname - pathname;
448 pathname = new_pathname;
451 /* Add a new frame to the top of the path stack. */
452 pathframe.pathp = pathname + pathname_length;
453 pathframe.inum = *inodep;
454 pathframe.next = pathstack;
455 pathstack = &pathframe;
457 /* Append '/' and the filename to current pathname, take care of
458 the file (which could result in recursive calls), and take
459 the filename back off. */
461 *pathstack->pathp = '/';
462 strcpy (pathstack->pathp + 1, namep);
464 /* If the i-number has already appeared, there's an error. */
465 if (duplicate_entry (pathstack->next, pathstack->inum))
470 *pathstack->pathp = '\0';
471 pathstack = pathstack->next; /* Pop the stack. */
474 /* Keep trying while there are still files to remove. */
475 while (namep > name_space && err == 0);
482 /* If STACK does not already have an entry with the same i-number as INUM,
483 return 0. Otherwise, ask the user whether to continue;
484 if yes, return 1, and if no, exit.
485 This assumes that no one tries to remove filesystem mount points;
486 doing so could cause duplication of i-numbers that would not indicate
487 a corrupted file system. */
490 duplicate_entry (struct pathstack *stack, ino_t inum)
492 #ifdef D_INO_IN_DIRENT
495 for (p = stack; p != NULL; p = p->next)
499 fprintf (stderr, _("\
500 %s: WARNING: Circular directory structure.\n\
501 This almost certainly means that you have a corrupted file system.\n\
502 NOTIFY YOUR SYSTEM MANAGER.\n\
505 is the same file as\n"), program_name, pathname);
506 *p->pathp = '\0'; /* Truncate pathname. */
507 fprintf (stderr, "%s\n", pathname);
508 *p->pathp = '/'; /* Put it back. */
511 fprintf (stderr, _("%s: continue? "), program_name);
520 #endif /* D_INO_IN_DIRENT */
528 fprintf (stderr, _("Try `%s --help' for more information.\n"),
532 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
534 Remove (unlink) the FILE(s).\n\
536 -d, --directory unlink directory, even if non-empty (super-user only)\n\
537 -f, --force ignore nonexistent files, never prompt\n\
538 -i, --interactive prompt before any removal\n\
539 -v, --verbose explain what is being done\n\
540 -r, -R, --recursive remove the contents of directories recursively\n\
541 --help display this help and exit\n\
542 --version output version information and exit\n"));