getopt-ify rm so that BB_FEATURE_RM_INTERACTIVE will work

This commit is contained in:
Eric Andersen 2001-03-19 18:52:37 +00:00
parent c75586e06d
commit 7c25441792
2 changed files with 64 additions and 68 deletions

View File

@ -33,6 +33,7 @@
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <getopt.h>
#include "busybox.h" #include "busybox.h"
static int recursiveFlag = FALSE; static int recursiveFlag = FALSE;
@ -82,52 +83,48 @@ static int dirAction(const char *fileName, struct stat *statbuf, void* junk)
extern int rm_main(int argc, char **argv) extern int rm_main(int argc, char **argv)
{ {
int opt;
int status = EXIT_SUCCESS; int status = EXIT_SUCCESS;
int stopIt=FALSE; int stopIt=FALSE;
struct stat statbuf; struct stat statbuf;
argc--;
argv++;
/* Parse any options */ /* do normal option parsing */
while (argc > 0 && stopIt == FALSE) { while ((opt = getopt(argc, argv, "Rrf-"
if (**argv == '-') {
while (*++(*argv))
switch (**argv) {
case 'R':
case 'r':
recursiveFlag = TRUE;
break;
case 'f':
forceFlag = TRUE;
#ifdef BB_FEATURE_RM_INTERACTIVE #ifdef BB_FEATURE_RM_INTERACTIVE
interactiveFlag = FALSE; "i"
#endif #endif
break; )) > 0) {
case 'i': switch (opt) {
case 'R':
case 'r':
recursiveFlag = TRUE;
break;
case 'f':
forceFlag = TRUE;
#ifdef BB_FEATURE_RM_INTERACTIVE #ifdef BB_FEATURE_RM_INTERACTIVE
interactiveFlag = TRUE; interactiveFlag = FALSE;
#endif #endif
break; break;
case '-': case 'i':
stopIt = TRUE; #ifdef BB_FEATURE_RM_INTERACTIVE
break; interactiveFlag = TRUE;
default: #endif
show_usage(); break;
} case '-':
argc--; stopIt = TRUE;
argv++; break;
default:
show_usage();
} }
else
break;
} }
if (argc < 1 && forceFlag == FALSE) { if ((argc-optind) < 1 && forceFlag == FALSE) {
show_usage(); show_usage();
} }
while (argc-- > 0) { while (optind < argc) {
srcName = *(argv++); srcName = argv[optind];
if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0 if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0
&& errno == ENOENT) { && errno == ENOENT) {
/* do not reports errors for non-existent files if -f, just skip them */ /* do not reports errors for non-existent files if -f, just skip them */
@ -137,6 +134,7 @@ extern int rm_main(int argc, char **argv)
status = EXIT_FAILURE; status = EXIT_FAILURE;
} }
} }
optind++;
} }
return status; return status;
} }

60
rm.c
View File

@ -33,6 +33,7 @@
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <getopt.h>
#include "busybox.h" #include "busybox.h"
static int recursiveFlag = FALSE; static int recursiveFlag = FALSE;
@ -82,52 +83,48 @@ static int dirAction(const char *fileName, struct stat *statbuf, void* junk)
extern int rm_main(int argc, char **argv) extern int rm_main(int argc, char **argv)
{ {
int opt;
int status = EXIT_SUCCESS; int status = EXIT_SUCCESS;
int stopIt=FALSE; int stopIt=FALSE;
struct stat statbuf; struct stat statbuf;
argc--;
argv++;
/* Parse any options */ /* do normal option parsing */
while (argc > 0 && stopIt == FALSE) { while ((opt = getopt(argc, argv, "Rrf-"
if (**argv == '-') {
while (*++(*argv))
switch (**argv) {
case 'R':
case 'r':
recursiveFlag = TRUE;
break;
case 'f':
forceFlag = TRUE;
#ifdef BB_FEATURE_RM_INTERACTIVE #ifdef BB_FEATURE_RM_INTERACTIVE
interactiveFlag = FALSE; "i"
#endif #endif
break; )) > 0) {
case 'i': switch (opt) {
case 'R':
case 'r':
recursiveFlag = TRUE;
break;
case 'f':
forceFlag = TRUE;
#ifdef BB_FEATURE_RM_INTERACTIVE #ifdef BB_FEATURE_RM_INTERACTIVE
interactiveFlag = TRUE; interactiveFlag = FALSE;
#endif #endif
break; break;
case '-': case 'i':
stopIt = TRUE; #ifdef BB_FEATURE_RM_INTERACTIVE
break; interactiveFlag = TRUE;
default: #endif
show_usage(); break;
} case '-':
argc--; stopIt = TRUE;
argv++; break;
default:
show_usage();
} }
else
break;
} }
if (argc < 1 && forceFlag == FALSE) { if ((argc-optind) < 1 && forceFlag == FALSE) {
show_usage(); show_usage();
} }
while (argc-- > 0) { while (optind < argc) {
srcName = *(argv++); srcName = argv[optind];
if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0 if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0
&& errno == ENOENT) { && errno == ENOENT) {
/* do not reports errors for non-existent files if -f, just skip them */ /* do not reports errors for non-existent files if -f, just skip them */
@ -137,6 +134,7 @@ extern int rm_main(int argc, char **argv)
status = EXIT_FAILURE; status = EXIT_FAILURE;
} }
} }
optind++;
} }
return status; return status;
} }