btrfs-progs: use check_argc_* to check arg number for all tools

Since this patch:
	btrfs-progs: move the check_argc_* functions into utils.c

All tools including the independent tools(e.g. btrfs-image, btrfs-convert)
can share the convenience of the check_argc_* functions, so this patch
adopt the argc check functions globally.

Signed-off-by: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
master
Gui Hecheng 2014-07-16 11:59:46 +08:00 committed by David Sterba
parent 2e1accf4fd
commit f1672e50c7
15 changed files with 36 additions and 20 deletions

View File

@ -452,7 +452,9 @@ int main(int argc, char **argv)
} }
} }
if (optind >= argc) { set_argv0(argv);
argc = argc - optind;
if (check_argc_min(argc, 1)) {
usage(); usage();
exit(1); exit(1);
} }

View File

@ -2723,7 +2723,8 @@ int main(int argc, char *argv[])
} }
} }
argc = argc - optind; argc = argc - optind;
if (argc != 1) { set_argv0(argv);
if (check_argc_exact(argc, 1)) {
print_usage(); print_usage();
return 1; return 1;
} }

View File

@ -865,8 +865,9 @@ int main(int ac, char **av)
print_usage(); print_usage();
} }
} }
set_argv0(av);
ac = ac - optind; ac = ac - optind;
if (ac == 0) if (check_argc_min(ac, 1))
print_usage(); print_usage();
dev = av[optind]; dev = av[optind];

View File

@ -20,6 +20,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "crc32c.h" #include "crc32c.h"
#include "utils.h"
void usage(void) void usage(void)
{ {
@ -62,13 +63,13 @@ int main(int argc, char **argv)
} }
} }
set_argv0(argv);
str = argv[optind]; str = argv[optind];
if (!loop) { if (!loop) {
if (optind >= argc) { if (check_argc_min(argc - optind, 1))
fprintf(stderr, "not enough arguments\n");
return 255; return 255;
}
printf("%12u - %s\n", crc32c(~1, str, strlen(str)), str); printf("%12u - %s\n", crc32c(~1, str, strlen(str)), str);
return 0; return 0;
} }

View File

@ -174,8 +174,9 @@ int main(int ac, char **av)
print_usage(); print_usage();
} }
} }
set_argv0(av);
ac = ac - optind; ac = ac - optind;
if (ac != 1) if (check_argc_exact(ac, 1))
print_usage(); print_usage();
info = open_ctree_fs_info(av[optind], 0, 0, OPEN_CTREE_PARTIAL); info = open_ctree_fs_info(av[optind], 0, 0, OPEN_CTREE_PARTIAL);

View File

@ -302,7 +302,9 @@ int main(int argc, char **argv)
} }
} }
if (optind >= argc) { set_argv0(argv);
argc = argc - optind;
if (check_argc_min(argc, 1)) {
usage(); usage();
exit(1); exit(1);
} }

View File

@ -425,13 +425,15 @@ int main(int argc, char **argv)
} }
} }
if (optind < argc) { set_argv0(argv);
path = argv[optind++]; argc = argc - optind;
} else { if (check_argc_min(argc, 1)) {
usage(); usage();
exit(1); exit(1);
} }
path = argv[optind++];
fd = open_file_or_dir(path, &dirstream); fd = open_file_or_dir(path, &dirstream);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", path); fprintf(stderr, "ERROR: can't access '%s'\n", path);

View File

@ -148,8 +148,9 @@ int main(int ac, char **av)
print_usage(); print_usage();
} }
} }
set_argv0(av);
ac = ac - optind; ac = ac - optind;
if (ac == 0) if (check_argc_min(ac, 1))
print_usage(); print_usage();
if (logical == 0) if (logical == 0)
print_usage(); print_usage();

View File

@ -66,9 +66,10 @@ int main(int ac, char **av)
print_usage(); print_usage();
} }
} }
set_argv0(av);
ac = ac - optind; ac = ac - optind;
if (ac != 1) if (check_argc_exact(ac, 1))
print_usage(); print_usage();
if (bytenr == 0) { if (bytenr == 0) {

View File

@ -95,7 +95,8 @@ int main(int argc, char **argv)
} }
} }
if (argc < optind + 1) { set_argv0(argv);
if (check_argc_min(argc - optind, 1)) {
print_usage(); print_usage();
exit(1); exit(1);
} }

View File

@ -46,7 +46,8 @@ int main(int ac, char **av)
struct btrfs_trans_handle *trans; struct btrfs_trans_handle *trans;
int ret; int ret;
if (ac != 2) set_argv0(av);
if (check_argc_exact(ac, 2))
print_usage(); print_usage();
radix_tree_init(); radix_tree_init();

View File

@ -137,9 +137,10 @@ int main(int argc, char *argv[])
} }
} }
set_argv0(argv);
argc = argc - optind; argc = argc - optind;
device = argv[optind]; device = argv[optind];
if (argc != 1) { if (check_argc_exact(argc, 1)) {
print_usage(); print_usage();
return 1; return 1;
} }

View File

@ -6634,7 +6634,7 @@ int cmd_check(int argc, char **argv)
} }
argc = argc - optind; argc = argc - optind;
if (argc != 1) if (check_argc_exact(argc, 1))
usage(cmd_check_usage); usage(cmd_check_usage);
radix_tree_init(); radix_tree_init();

View File

@ -1214,9 +1214,10 @@ int cmd_restore(int argc, char **argv)
} }
} }
if (!list_roots && optind + 1 >= argc) set_argv0(argv);
if (!list_roots && check_argc_min(argc - optind, 2))
usage(cmd_restore_usage); usage(cmd_restore_usage);
else if (list_roots && optind >= argc) else if (list_roots && check_argc_min(argc - optind, 1))
usage(cmd_restore_usage); usage(cmd_restore_usage);
if (fs_location && root_objectid) { if (fs_location && root_objectid) {

View File

@ -522,7 +522,7 @@ int cmd_send(int argc, char **argv)
} }
} }
if (optind == argc) if (check_argc_min(argc - optind, 1))
usage(cmd_send_usage); usage(cmd_send_usage);
if (outname != NULL) { if (outname != NULL) {