diff options
author | kraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-04-24 20:04:18 +0000 |
---|---|---|
committer | kraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-04-24 20:04:18 +0000 |
commit | 8695b23e5809a0b952accd98da299f86b126d5fd (patch) | |
tree | d1403572448114ecd20030316914f473816f882d /rm.c | |
parent | c1db72874a1dbb92ee3dca555e1561e6a95b5ce2 (diff) | |
download | busybox-w32-8695b23e5809a0b952accd98da299f86b126d5fd.tar.gz busybox-w32-8695b23e5809a0b952accd98da299f86b126d5fd.tar.bz2 busybox-w32-8695b23e5809a0b952accd98da299f86b126d5fd.zip |
Rewrote rm.
git-svn-id: svn://busybox.net/trunk/busybox@2423 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'rm.c')
-rw-r--r-- | rm.c | 124 |
1 files changed, 31 insertions, 93 deletions
@@ -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 | } |