@@ -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();
exit(1);
}
@@ -2723,7 +2723,8 @@ int main(int argc, char *argv[])
}
}
argc = argc - optind;
- if (argc != 1) {
+ set_argv0(argv);
+ if (check_argc_exact(argc, 1)) {
print_usage();
return 1;
}
@@ -865,8 +865,9 @@ int main(int ac, char **av)
print_usage();
}
}
+ set_argv0(av);
ac = ac - optind;
- if (ac == 0)
+ if (check_argc_min(ac, 1))
print_usage();
dev = av[optind];
@@ -20,6 +20,7 @@
#include <stdlib.h>
#include <unistd.h>
#include "crc32c.h"
+#include "utils.h"
void usage(void)
{
@@ -62,13 +63,13 @@ int main(int argc, char **argv)
}
}
+ set_argv0(argv);
str = argv[optind];
if (!loop) {
- if (optind >= argc) {
- fprintf(stderr, "not enough arguments\n");
+ if (check_argc_min(argc - optind, 1))
return 255;
- }
+
printf("%12u - %s\n", crc32c(~1, str, strlen(str)), str);
return 0;
}
@@ -174,8 +174,9 @@ int main(int ac, char **av)
print_usage();
}
}
+ set_argv0(av);
ac = ac - optind;
- if (ac != 1)
+ if (check_argc_exact(ac, 1))
print_usage();
info = open_ctree_fs_info(av[optind], 0, 0, OPEN_CTREE_PARTIAL);
@@ -305,7 +305,9 @@ int main(int argc, char **argv)
}
}
- if (optind >= argc) {
+ set_argv0(argv);
+ argc = argc - optind;
+ if (check_argc_min(argc, 1)) {
usage();
exit(1);
}
@@ -425,13 +425,15 @@ int main(int argc, char **argv)
}
}
- if (optind < argc) {
- path = argv[optind++];
- } else {
+ set_argv0(argv);
+ argc = argc - optind;
+ if (check_argc_min(argc, 1)) {
usage();
exit(1);
}
+ path = argv[optind++];
+
fd = open_file_or_dir(path, &dirstream);
if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", path);
@@ -148,8 +148,9 @@ int main(int ac, char **av)
print_usage();
}
}
+ set_argv0(av);
ac = ac - optind;
- if (ac == 0)
+ if (check_argc_min(ac, 1))
print_usage();
if (logical == 0)
print_usage();
@@ -66,9 +66,10 @@ int main(int ac, char **av)
print_usage();
}
}
+ set_argv0(av);
ac = ac - optind;
- if (ac != 1)
+ if (check_argc_exact(ac, 1))
print_usage();
if (bytenr == 0) {
@@ -95,7 +95,9 @@ int main(int argc, char **argv)
}
}
- if (argc < optind + 1) {
+ set_argv0(argv);
+ argc = argc - optind;
+ if (check_argc_min(argc, 1)) {
print_usage();
exit(1);
}
@@ -46,7 +46,8 @@ int main(int ac, char **av)
struct btrfs_trans_handle *trans;
int ret;
- if (ac != 2)
+ set_argv0(av);
+ if (check_argc_exact(ac, 2))
print_usage();
radix_tree_init();
@@ -137,9 +137,10 @@ int main(int argc, char *argv[])
}
}
+ set_argv0(argv);
argc = argc - optind;
device = argv[optind];
- if (argc != 1) {
+ if (check_argc_exact(argc, 1)) {
print_usage();
return 1;
}
@@ -7018,9 +7018,10 @@ int cmd_check(int argc, char **argv)
check_data_csum = 1;
}
}
+ set_argv0(argv);
argc = argc - optind;
- if (argc != 1)
+ if (check_argc_exact(argc, 1))
usage(cmd_check_usage);
radix_tree_init();
@@ -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);
- else if (list_roots && optind >= argc)
+ else if (list_roots && check_argc_min(argc - optind, 1))
usage(cmd_restore_usage);
if (fs_location && root_objectid) {
@@ -556,7 +556,9 @@ int cmd_send(int argc, char **argv)
}
}
- if (optind == argc)
+ set_argv0(argv);
+ argc = argc - optind;
+ if (check_argc_min(argc, 1))
usage(cmd_send_usage);
if (g_total_data_size &&
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> --- btrfs-calc-size.c | 4 +++- btrfs-convert.c | 3 ++- btrfs-corrupt-block.c | 3 ++- btrfs-crc.c | 7 ++++--- btrfs-debug-tree.c | 3 ++- btrfs-find-root.c | 4 +++- btrfs-fragments.c | 8 +++++--- btrfs-map-logical.c | 3 ++- btrfs-select-super.c | 3 ++- btrfs-show-super.c | 4 +++- btrfs-zero-log.c | 3 ++- btrfstune.c | 3 ++- cmds-check.c | 3 ++- cmds-restore.c | 5 +++-- cmds-send.c | 4 +++- 15 files changed, 40 insertions(+), 20 deletions(-)