Applied patch from Jeff Garzik to getopt-ify rmmod.

This commit is contained in:
Mark Whitley 2001-03-09 21:49:12 +00:00
parent 8d4c397d9d
commit f90c28dd1e
2 changed files with 24 additions and 26 deletions

View File

@ -25,6 +25,7 @@
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <getopt.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <linux/unistd.h> #include <linux/unistd.h>
#include "busybox.h" #include "busybox.h"
@ -37,15 +38,11 @@ _syscall1(int, delete_module, const char *, name)
extern int rmmod_main(int argc, char **argv) extern int rmmod_main(int argc, char **argv)
{ {
int ret = EXIT_SUCCESS; int n, ret = EXIT_SUCCESS;
if (argc <= 1) {
show_usage();
}
/* Parse any options */ /* Parse command line. */
while (--argc > 0 && **(++argv) == '-') { while ((n = getopt(argc, argv, "a")) != EOF) {
while (*(++(*argv))) { switch (n) {
switch (**argv) {
case 'a': case 'a':
/* Unload _all_ unused modules via NULL delete_module() call */ /* Unload _all_ unused modules via NULL delete_module() call */
if (delete_module(NULL)) if (delete_module(NULL))
@ -53,16 +50,18 @@ extern int rmmod_main(int argc, char **argv)
return EXIT_SUCCESS; return EXIT_SUCCESS;
default: default:
show_usage(); show_usage();
}
} }
} }
while (argc-- > 0) { if (optind == argc)
if (delete_module(*argv) < 0) { show_usage();
perror_msg("%s", *argv);
for (n = optind; n < argc; n++) {
if (delete_module(argv[n]) < 0) {
perror_msg("%s", argv[n]);
ret = EXIT_FAILURE; ret = EXIT_FAILURE;
} }
argv++;
} }
return(ret); return(ret);
} }

25
rmmod.c
View File

@ -25,6 +25,7 @@
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <getopt.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <linux/unistd.h> #include <linux/unistd.h>
#include "busybox.h" #include "busybox.h"
@ -37,15 +38,11 @@ _syscall1(int, delete_module, const char *, name)
extern int rmmod_main(int argc, char **argv) extern int rmmod_main(int argc, char **argv)
{ {
int ret = EXIT_SUCCESS; int n, ret = EXIT_SUCCESS;
if (argc <= 1) {
show_usage();
}
/* Parse any options */ /* Parse command line. */
while (--argc > 0 && **(++argv) == '-') { while ((n = getopt(argc, argv, "a")) != EOF) {
while (*(++(*argv))) { switch (n) {
switch (**argv) {
case 'a': case 'a':
/* Unload _all_ unused modules via NULL delete_module() call */ /* Unload _all_ unused modules via NULL delete_module() call */
if (delete_module(NULL)) if (delete_module(NULL))
@ -53,16 +50,18 @@ extern int rmmod_main(int argc, char **argv)
return EXIT_SUCCESS; return EXIT_SUCCESS;
default: default:
show_usage(); show_usage();
}
} }
} }
while (argc-- > 0) { if (optind == argc)
if (delete_module(*argv) < 0) { show_usage();
perror_msg("%s", *argv);
for (n = optind; n < argc; n++) {
if (delete_module(argv[n]) < 0) {
perror_msg("%s", argv[n]);
ret = EXIT_FAILURE; ret = EXIT_FAILURE;
} }
argv++;
} }
return(ret); return(ret);
} }