Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini rm implementation for busybox |
| 4 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * |
Eric Andersen | 8ec10a9 | 2001-01-27 09:33:39 +0000 | [diff] [blame] | 6 | * Copyright (C) 1999,2000,2001 by Lineo, inc. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 7 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 8 | * |
Mark Whitley | f6ba2da | 2001-03-13 16:35:55 +0000 | [diff] [blame] | 9 | * INTERACTIVE feature Copyright (C) 2001 by Alcove |
| 10 | * written by Christophe Boyanique <Christophe.Boyanique@fr.alcove.com> |
| 11 | * for Ipanema Technologies |
| 12 | * |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | * General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 26 | * |
| 27 | */ |
| 28 | |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <time.h> |
| 31 | #include <utime.h> |
| 32 | #include <dirent.h> |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 33 | #include <errno.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | #include <stdlib.h> |
Eric Andersen | 7c25441 | 2001-03-19 18:52:37 +0000 | [diff] [blame] | 36 | #include <getopt.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 37 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 38 | |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 39 | static int recursiveFlag = FALSE; |
| 40 | static int forceFlag = FALSE; |
Mark Whitley | e0bf91d | 2001-03-13 00:40:19 +0000 | [diff] [blame] | 41 | #ifdef BB_FEATURE_RM_INTERACTIVE |
| 42 | static int interactiveFlag = FALSE; |
| 43 | #endif |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 44 | static const char *srcName; |
| 45 | |
| 46 | |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 47 | static int fileAction(const char *fileName, struct stat *statbuf, void* junk) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 48 | { |
Mark Whitley | e0bf91d | 2001-03-13 00:40:19 +0000 | [diff] [blame] | 49 | #ifdef BB_FEATURE_RM_INTERACTIVE |
| 50 | if (interactiveFlag == TRUE) { |
| 51 | printf("rm: remove `%s'? ", fileName); |
| 52 | if (ask_confirmation() == 0) |
| 53 | return (TRUE); |
| 54 | } |
| 55 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | if (unlink(fileName) < 0) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 57 | perror_msg("%s", fileName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 58 | return (FALSE); |
| 59 | } |
| 60 | return (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 63 | static int dirAction(const char *fileName, struct stat *statbuf, void* junk) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 64 | { |
Matt Kraai | 7c22b77 | 2000-09-20 23:10:21 +0000 | [diff] [blame] | 65 | if (recursiveFlag == FALSE) { |
| 66 | errno = EISDIR; |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 67 | perror_msg("%s", fileName); |
Matt Kraai | 7c22b77 | 2000-09-20 23:10:21 +0000 | [diff] [blame] | 68 | return (FALSE); |
| 69 | } |
Mark Whitley | e0bf91d | 2001-03-13 00:40:19 +0000 | [diff] [blame] | 70 | #ifdef BB_FEATURE_RM_INTERACTIVE |
| 71 | if (interactiveFlag == TRUE) { |
| 72 | printf("rm: remove directory `%s'? ", fileName); |
| 73 | if (ask_confirmation() == 0) |
| 74 | return (TRUE); |
| 75 | } |
| 76 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 77 | if (rmdir(fileName) < 0) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 78 | perror_msg("%s", fileName); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 79 | return (FALSE); |
| 80 | } |
| 81 | return (TRUE); |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | extern int rm_main(int argc, char **argv) |
| 85 | { |
Eric Andersen | 7c25441 | 2001-03-19 18:52:37 +0000 | [diff] [blame] | 86 | int opt; |
Matt Kraai | d27753a | 2000-12-05 05:11:41 +0000 | [diff] [blame] | 87 | int status = EXIT_SUCCESS; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 88 | struct stat statbuf; |
Eric Andersen | 7c25441 | 2001-03-19 18:52:37 +0000 | [diff] [blame] | 89 | |
| 90 | |
| 91 | /* do normal option parsing */ |
| 92 | while ((opt = getopt(argc, argv, "Rrf-" |
Mark Whitley | e0bf91d | 2001-03-13 00:40:19 +0000 | [diff] [blame] | 93 | #ifdef BB_FEATURE_RM_INTERACTIVE |
Eric Andersen | 7c25441 | 2001-03-19 18:52:37 +0000 | [diff] [blame] | 94 | "i" |
Mark Whitley | e0bf91d | 2001-03-13 00:40:19 +0000 | [diff] [blame] | 95 | #endif |
Eric Andersen | 7c25441 | 2001-03-19 18:52:37 +0000 | [diff] [blame] | 96 | )) > 0) { |
| 97 | switch (opt) { |
| 98 | case 'R': |
| 99 | case 'r': |
| 100 | recursiveFlag = TRUE; |
| 101 | break; |
| 102 | case 'f': |
| 103 | forceFlag = TRUE; |
Eric Andersen | 7c25441 | 2001-03-19 18:52:37 +0000 | [diff] [blame] | 104 | break; |
Eric Andersen | 7c25441 | 2001-03-19 18:52:37 +0000 | [diff] [blame] | 105 | #ifdef BB_FEATURE_RM_INTERACTIVE |
Eric Andersen | cc165b9 | 2001-03-19 18:59:01 +0000 | [diff] [blame] | 106 | case 'i': |
Eric Andersen | 7c25441 | 2001-03-19 18:52:37 +0000 | [diff] [blame] | 107 | interactiveFlag = TRUE; |
| 108 | #endif |
| 109 | break; |
Eric Andersen | 7c25441 | 2001-03-19 18:52:37 +0000 | [diff] [blame] | 110 | default: |
| 111 | show_usage(); |
Eric Andersen | 815e904 | 2000-06-06 16:15:23 +0000 | [diff] [blame] | 112 | } |
Pavel Roskin | e97da40 | 2000-06-14 17:39:41 +0000 | [diff] [blame] | 113 | } |
Eric Andersen | 7c25441 | 2001-03-19 18:52:37 +0000 | [diff] [blame] | 114 | |
Eric Andersen | 8269396 | 2001-03-19 19:40:43 +0000 | [diff] [blame] | 115 | if (argc == optind && forceFlag == FALSE) { |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 116 | show_usage(); |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 117 | } |
Eric Andersen | 16f7015 | 2001-03-19 18:54:38 +0000 | [diff] [blame] | 118 | #ifdef BB_FEATURE_RM_INTERACTIVE |
| 119 | if (forceFlag == TRUE) |
| 120 | interactiveFlag = FALSE; |
| 121 | #endif |
| 122 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 123 | |
Eric Andersen | 7c25441 | 2001-03-19 18:52:37 +0000 | [diff] [blame] | 124 | while (optind < argc) { |
| 125 | srcName = argv[optind]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 126 | if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0 |
| 127 | && errno == ENOENT) { |
| 128 | /* do not reports errors for non-existent files if -f, just skip them */ |
| 129 | } else { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 130 | if (recursive_action(srcName, recursiveFlag, FALSE, |
Erik Andersen | 3364d78 | 2000-03-28 00:58:14 +0000 | [diff] [blame] | 131 | TRUE, fileAction, dirAction, NULL) == FALSE) { |
Matt Kraai | d27753a | 2000-12-05 05:11:41 +0000 | [diff] [blame] | 132 | status = EXIT_FAILURE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
Eric Andersen | 7c25441 | 2001-03-19 18:52:37 +0000 | [diff] [blame] | 135 | optind++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 136 | } |
Matt Kraai | d27753a | 2000-12-05 05:11:41 +0000 | [diff] [blame] | 137 | return status; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 138 | } |