Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mini rm implementation for busybox |
| 3 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 1999 by Lineo, inc. |
| 6 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 24 | #include "internal.h" |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <time.h> |
| 27 | #include <utime.h> |
| 28 | #include <dirent.h> |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 29 | #include <errno.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 30 | |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 31 | static const char* rm_usage = "rm [OPTION]... FILE...\n\n" |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 32 | "Remove (unlink) the FILE(s).\n\n" |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 33 | "Options:\n" |
Eric Andersen | 08b1034 | 1999-11-19 02:38:58 +0000 | [diff] [blame] | 34 | "\t-f\t\tremove existing destinations, never prompt\n" |
| 35 | "\t-r or -R\tremove the contents of directories recursively\n"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 36 | |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 37 | |
| 38 | static int recursiveFlag = FALSE; |
| 39 | static int forceFlag = FALSE; |
| 40 | static const char *srcName; |
| 41 | |
| 42 | |
| 43 | static int fileAction(const char *fileName, struct stat* statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 44 | { |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 45 | if (unlink( fileName) < 0 ) { |
| 46 | perror( fileName); |
| 47 | return ( FALSE); |
| 48 | } |
| 49 | return ( TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 52 | static int dirAction(const char *fileName, struct stat* statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 53 | { |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 54 | if (rmdir( fileName) < 0 ) { |
| 55 | perror( fileName); |
| 56 | return ( FALSE); |
| 57 | } |
| 58 | return ( TRUE); |
| 59 | } |
| 60 | |
| 61 | extern int rm_main(int argc, char **argv) |
| 62 | { |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 63 | struct stat statbuf; |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 64 | |
| 65 | if (argc < 2) { |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 66 | usage( rm_usage); |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 67 | } |
| 68 | argc--; |
| 69 | argv++; |
| 70 | |
| 71 | /* Parse any options */ |
| 72 | while (**argv == '-') { |
| 73 | while (*++(*argv)) |
| 74 | switch (**argv) { |
Eric Andersen | 08b1034 | 1999-11-19 02:38:58 +0000 | [diff] [blame] | 75 | case 'R': |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 76 | case 'r': |
| 77 | recursiveFlag = TRUE; |
| 78 | break; |
| 79 | case 'f': |
| 80 | forceFlag = TRUE; |
| 81 | break; |
| 82 | default: |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 83 | usage( rm_usage); |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 84 | } |
| 85 | argc--; |
| 86 | argv++; |
| 87 | } |
| 88 | |
| 89 | while (argc-- > 0) { |
| 90 | srcName = *(argv++); |
Eric Andersen | a9c95ea | 1999-11-15 17:33:30 +0000 | [diff] [blame] | 91 | if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0 && errno == ENOENT) { |
| 92 | /* do not reports errors for non-existent files if -f, just skip them */ |
| 93 | } |
| 94 | else { |
| 95 | if (recursiveAction( srcName, recursiveFlag, FALSE, |
| 96 | TRUE, fileAction, dirAction) == FALSE) { |
| 97 | exit( FALSE); |
| 98 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 99 | } |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 100 | } |
| 101 | exit( TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 102 | } |