Changeset 3192 for trunk/src/kmk/kmkbuiltin/rmdir.c
- Timestamp:
- Mar 26, 2018, 10:25:56 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/kmkbuiltin/rmdir.c
r2912 r3192 44 44 #endif 45 45 46 47 /********************************************************************************************************************************* 48 * Header Files * 49 *********************************************************************************************************************************/ 46 50 #include "config.h" 47 51 #include "err.h" … … 60 64 # include "mscfakes.h" 61 65 #endif 62 #if defined(KMK) && defined(KBUILD_OS_WINDOWS) 63 extern int dir_cache_deleted_directory(const char *pszDir); 64 #endif 65 66 static int rm_path(char *); 67 static int usage(FILE *); 68 69 static int pflag; 70 static int vflag; 71 static int ignore_fail_on_non_empty; 72 static int ignore_fail_on_not_exist; 73 66 67 68 /********************************************************************************************************************************* 69 * Structures and Typedefs * 70 *********************************************************************************************************************************/ 71 typedef struct RMDIRINSTANCE 72 { 73 PKMKBUILTINCTX pCtx; 74 int pflag; 75 int vflag; 76 int ignore_fail_on_non_empty; 77 int ignore_fail_on_not_exist; 78 } RMDIRINSTANCE; 79 typedef RMDIRINSTANCE *PRMDIRINSTANCE; 80 81 82 /********************************************************************************************************************************* 83 * Global Variables * 84 *********************************************************************************************************************************/ 74 85 static struct option long_options[] = 75 86 { … … 84 95 85 96 97 98 /********************************************************************************************************************************* 99 * Internal Functions * 100 *********************************************************************************************************************************/ 101 #if !defined(KMK_BUILTIN_STANDALONE) && defined(KBUILD_OS_WINDOWS) 102 extern int dir_cache_deleted_directory(const char *pszDir); 103 #endif 104 static int rm_path(PRMDIRINSTANCE, char *); 105 static int usage(PKMKBUILTINCTX, int); 106 107 86 108 int 87 kmk_builtin_rmdir(int argc, char *argv[], char **envp) 88 { 109 kmk_builtin_rmdir(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx) 110 { 111 RMDIRINSTANCE This; 89 112 int ch, errors; 90 113 91 /* reinitialize globals */ 92 ignore_fail_on_not_exist = ignore_fail_on_non_empty = vflag = pflag = 0; 114 /* Initialize global instance. */ 115 This.pCtx = pCtx; 116 This.ignore_fail_on_not_exist = 0; 117 This.ignore_fail_on_non_empty = 0; 118 This.vflag = 0; 119 This.pflag = 0; 93 120 94 121 /* kmk: reset getopt and set progname */ 95 g_progname = argv[0];96 122 opterr = 1; 97 123 optarg = NULL; … … 101 127 switch(ch) { 102 128 case 'p': 103 pflag = 1;129 This.pflag = 1; 104 130 break; 105 131 case 'v': 106 vflag = 1;132 This.vflag = 1; 107 133 break; 108 134 case 260: 109 ignore_fail_on_non_empty = 1;135 This.ignore_fail_on_non_empty = 1; 110 136 break; 111 137 case 261: 112 ignore_fail_on_not_exist = 1;138 This.ignore_fail_on_not_exist = 1; 113 139 break; 114 140 case 262: 115 usage( stdout);141 usage(pCtx, 0); 116 142 return 0; 117 143 case 263: … … 119 145 case '?': 120 146 default: 121 return usage( stderr);147 return usage(pCtx, 1); 122 148 } 123 149 argc -= optind; … … 129 155 for (errors = 0; *argv; argv++) { 130 156 if (rmdir(*argv) < 0) { 131 if ( (!ignore_fail_on_non_empty || (errno != ENOTEMPTY && errno != EPERM && errno != EACCES && errno != EINVAL && errno != EEXIST)) 132 && (!ignore_fail_on_not_exist || errno != ENOENT)) { 133 warn("rmdir: %s", *argv); 157 if ( ( !This.ignore_fail_on_non_empty 158 || (errno != ENOTEMPTY && errno != EPERM && errno != EACCES && errno != EINVAL && errno != EEXIST)) 159 && ( !This.ignore_fail_on_not_exist 160 || errno != ENOENT)) { 161 warn(pCtx, "rmdir: %s", *argv); 134 162 errors = 1; 135 163 continue; 136 164 } 137 if (! ignore_fail_on_not_exist || errno != ENOENT)165 if (!This.ignore_fail_on_not_exist || errno != ENOENT) 138 166 continue; 139 167 /* (only ignored doesn't exist errors fall thru) */ 140 168 } else { 141 #if defined(KMK) && defined(KBUILD_OS_WINDOWS)169 #if !defined(KMK_BUILTIN_STANDALONE) && defined(KBUILD_OS_WINDOWS) 142 170 dir_cache_deleted_directory(*argv); 143 171 #endif 144 if (vflag) { 145 printf("%s\n", *argv); 146 } 147 } 148 if (pflag) 149 errors |= rm_path(*argv); 172 if (This.vflag) 173 kmk_builtin_ctx_printf(pCtx, 0, "%s\n", *argv); 174 } 175 if (This.pflag) 176 errors |= rm_path(&This, *argv); 150 177 } 151 178 … … 153 180 } 154 181 182 #ifdef KMK_BUILTIN_STANDALONE 183 int main(int argc, char **argv, char **envp) 184 { 185 KMKBUILTINCTX Ctx = { "kmk_rmdir", NULL }; 186 return kmk_builtin_rmdir(argc, argv, envp, &Ctx); 187 } 188 #endif 189 155 190 static int 156 rm_path( char *path)191 rm_path(PRMDIRINSTANCE pThis, char *path) 157 192 { 158 193 char *p; … … 186 221 187 222 if (rmdir(path) < 0) { 188 if ( ignore_fail_on_non_empty223 if ( pThis->ignore_fail_on_non_empty 189 224 && ( errno == ENOTEMPTY || errno == EPERM || errno == EACCES || errno == EINVAL || errno == EEXIST)) 190 225 break; 191 if (! ignore_fail_on_not_exist || errno != ENOENT) {192 warn( "rmdir: %s", path);226 if (!pThis->ignore_fail_on_not_exist || errno != ENOENT) { 227 warn(pThis->pCtx, "rmdir: %s", path); 193 228 return (1); 194 229 } … … 199 234 } 200 235 #endif 201 if ( vflag)202 printf("%s\n", path);236 if (pThis->vflag) 237 kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s\n", path); 203 238 } 204 239 … … 207 242 208 243 static int 209 usage(FILE *pf) 210 { 211 (void)fprintf(pf, "usage: %s [-pv --ignore-fail-on-non-empty --ignore-fail-on-not-exist] directory ...\n" 212 " or: %s --help\n" 213 " or: %s --version\n", 214 g_progname, g_progname, g_progname); 244 usage(PKMKBUILTINCTX pCtx, int fIsErr) 245 { 246 kmk_builtin_ctx_printf(pCtx, fIsErr, 247 "usage: %s [-pv --ignore-fail-on-non-empty --ignore-fail-on-not-exist] directory ...\n" 248 " or: %s --help\n" 249 " or: %s --version\n", 250 pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName); 215 251 return 1; 216 252 } 253
Note:
See TracChangeset
for help on using the changeset viewer.