diff options
| author | Matt Kraai <kraai@debian.org> | 2001-04-24 20:04:18 +0000 |
|---|---|---|
| committer | Matt Kraai <kraai@debian.org> | 2001-04-24 20:04:18 +0000 |
| commit | 8810bdbba359eb2348cc371f12347dd7f41dda18 (patch) | |
| tree | d1403572448114ecd20030316914f473816f882d /coreutils | |
| parent | 9f0fedb8f693829e7869439a2ad43cc1c21a3786 (diff) | |
| download | busybox-w32-8810bdbba359eb2348cc371f12347dd7f41dda18.tar.gz busybox-w32-8810bdbba359eb2348cc371f12347dd7f41dda18.tar.bz2 busybox-w32-8810bdbba359eb2348cc371f12347dd7f41dda18.zip | |
Rewrote rm.
Diffstat (limited to 'coreutils')
| -rw-r--r-- | coreutils/mv.c | 17 | ||||
| -rw-r--r-- | coreutils/rm.c | 124 |
2 files changed, 32 insertions, 109 deletions
diff --git a/coreutils/mv.c b/coreutils/mv.c index efc4ae6d8..b890abf6e 100644 --- a/coreutils/mv.c +++ b/coreutils/mv.c | |||
| @@ -32,20 +32,6 @@ | |||
| 32 | 32 | ||
| 33 | static int flags; | 33 | static int flags; |
| 34 | 34 | ||
| 35 | static int remove_file(const char *path, struct stat *statbuf, void *junk) | ||
| 36 | { | ||
| 37 | if (unlink(path) < 0) | ||
| 38 | return FALSE; | ||
| 39 | return TRUE; | ||
| 40 | } | ||
| 41 | |||
| 42 | static int remove_directory(const char *path, struct stat *statbuf, void *junk) | ||
| 43 | { | ||
| 44 | if (rmdir(path) < 0) | ||
| 45 | return FALSE; | ||
| 46 | return TRUE; | ||
| 47 | } | ||
| 48 | |||
| 49 | static int manual_rename(const char *source, const char *dest) | 35 | static int manual_rename(const char *source, const char *dest) |
| 50 | { | 36 | { |
| 51 | struct stat source_stat; | 37 | struct stat source_stat; |
| @@ -92,8 +78,7 @@ static int manual_rename(const char *source, const char *dest) | |||
| 92 | FILEUTILS_PRESERVE_SYMLINKS) < 0) | 78 | FILEUTILS_PRESERVE_SYMLINKS) < 0) |
| 93 | return -1; | 79 | return -1; |
| 94 | 80 | ||
| 95 | if (!recursive_action(source, TRUE, FALSE, TRUE, remove_file, | 81 | if (remove_file(source, FILEUTILS_RECUR | FILEUTILS_FORCE) < 0) |
| 96 | remove_directory, NULL)) | ||
| 97 | return -1; | 82 | return -1; |
| 98 | 83 | ||
| 99 | return 0; | 84 | return 0; |
diff --git a/coreutils/rm.c b/coreutils/rm.c index ea7f86ce1..f8e16625c 100644 --- a/coreutils/rm.c +++ b/coreutils/rm.c | |||
| @@ -3,12 +3,8 @@ | |||
| 3 | * Mini rm implementation for busybox | 3 | * Mini rm implementation for busybox |
| 4 | * | 4 | * |
| 5 | * | 5 | * |
| 6 | * Copyright (C) 1999,2000,2001 by Lineo, inc. | 6 | * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu> |
| 7 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> | ||
| 8 | * | 7 | * |
| 9 | * INTERACTIVE feature Copyright (C) 2001 by Alcove | ||
| 10 | * written by Christophe Boyanique <Christophe.Boyanique@fr.alcove.com> | ||
| 11 | * for Ipanema Technologies | ||
| 12 | * | 8 | * |
| 13 | * This program is free software; you can redistribute it and/or modify | 9 | * 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 | 10 | * it under the terms of the GNU General Public License as published by |
| @@ -36,103 +32,45 @@ | |||
| 36 | #include <getopt.h> | 32 | #include <getopt.h> |
| 37 | #include "busybox.h" | 33 | #include "busybox.h" |
| 38 | 34 | ||
| 39 | static int recursiveFlag = FALSE; | ||
| 40 | static int forceFlag = FALSE; | ||
| 41 | #ifdef BB_FEATURE_RM_INTERACTIVE | ||
| 42 | static int interactiveFlag = FALSE; | ||
| 43 | #endif | ||
| 44 | static const char *srcName; | ||
| 45 | |||
| 46 | |||
| 47 | static int fileAction(const char *fileName, struct stat *statbuf, void* junk) | ||
| 48 | { | ||
| 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 | ||
| 56 | if (unlink(fileName) < 0) { | ||
| 57 | perror_msg("%s", fileName); | ||
| 58 | return (FALSE); | ||
| 59 | } | ||
| 60 | return (TRUE); | ||
| 61 | } | ||
| 62 | |||
| 63 | static int dirAction(const char *fileName, struct stat *statbuf, void* junk) | ||
| 64 | { | ||
| 65 | if (recursiveFlag == FALSE) { | ||
| 66 | errno = EISDIR; | ||
| 67 | perror_msg("%s", fileName); | ||
| 68 | return (FALSE); | ||
| 69 | } | ||
| 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 | ||
| 77 | if (rmdir(fileName) < 0) { | ||
| 78 | perror_msg("%s", fileName); | ||
| 79 | return (FALSE); | ||
| 80 | } | ||
| 81 | return (TRUE); | ||
| 82 | } | ||
| 83 | |||
| 84 | extern int rm_main(int argc, char **argv) | 35 | extern int rm_main(int argc, char **argv) |
| 85 | { | 36 | { |
| 37 | int status = 0; | ||
| 86 | int opt; | 38 | int opt; |
| 87 | int status = EXIT_SUCCESS; | 39 | int flags = 0; |
| 88 | struct stat statbuf; | 40 | int i; |
| 89 | |||
| 90 | |||
| 91 | /* do normal option parsing */ | ||
| 92 | while ((opt = getopt(argc, argv, "Rrf-" | ||
| 93 | #ifdef BB_FEATURE_RM_INTERACTIVE | ||
| 94 | "i" | ||
| 95 | #endif | ||
| 96 | )) > 0) { | ||
| 97 | switch (opt) { | ||
| 98 | case 'R': | ||
| 99 | case 'r': | ||
| 100 | recursiveFlag = TRUE; | ||
| 101 | break; | ||
| 102 | case 'f': | ||
| 103 | forceFlag = TRUE; | ||
| 104 | #ifdef BB_FEATURE_RM_INTERACTIVE | ||
| 105 | 41 | ||
| 106 | interactiveFlag = FALSE; | 42 | while ((opt = getopt(argc, argv, "fiRr")) != -1) { |
| 107 | #endif | 43 | switch (opt) { |
| 108 | break; | 44 | case 'f': |
| 109 | #ifdef BB_FEATURE_RM_INTERACTIVE | 45 | flags &= ~FILEUTILS_INTERACTIVE; |
| 110 | case 'i': | 46 | flags |= FILEUTILS_FORCE; |
| 111 | interactiveFlag = TRUE; | 47 | break; |
| 112 | forceFlag = FALSE; | 48 | case 'i': |
| 113 | break; | 49 | flags &= ~FILEUTILS_FORCE; |
| 114 | #endif | 50 | flags |= FILEUTILS_INTERACTIVE; |
| 115 | default: | 51 | break; |
| 116 | show_usage(); | 52 | case 'R': |
| 53 | case 'r': | ||
| 54 | flags |= FILEUTILS_RECUR; | ||
| 55 | break; | ||
| 117 | } | 56 | } |
| 118 | } | 57 | } |
| 119 | 58 | ||
| 120 | if (argc == optind && forceFlag == FALSE) { | 59 | if (!(flags & FILEUTILS_FORCE) && optind == argc) |
| 121 | show_usage(); | 60 | show_usage(); |
| 122 | } | ||
| 123 | 61 | ||
| 124 | while (optind < argc) { | 62 | for (i = optind; i < argc; i++) { |
| 125 | srcName = argv[optind]; | 63 | char *base = get_last_path_component(argv[i]); |
| 126 | if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0 | 64 | |
| 127 | && errno == ENOENT) { | 65 | if (strcmp(base, ".") == 0 || strcmp(base, "..") == 0) { |
| 128 | /* do not reports errors for non-existent files if -f, just skip them */ | 66 | error_msg("cannot remove `.' or `..'"); |
| 129 | } else { | 67 | status = 1; |
| 130 | if (recursive_action(srcName, recursiveFlag, FALSE, | 68 | continue; |
| 131 | TRUE, fileAction, dirAction, NULL) == FALSE) { | ||
| 132 | status = EXIT_FAILURE; | ||
| 133 | } | ||
| 134 | } | 69 | } |
| 135 | optind++; | 70 | |
| 71 | if (remove_file(argv[i], flags) < 0) | ||
| 72 | status = 1; | ||
| 136 | } | 73 | } |
| 74 | |||
| 137 | return status; | 75 | return status; |
| 138 | } | 76 | } |
