btrfs-progs: use check_argc_* to check arg number for all tools
authorGui Hecheng <guihc.fnst@cn.fujitsu.com>
Wed, 16 Jul 2014 03:59:46 +0000 (11:59 +0800)
committerDavid Sterba <dsterba@suse.cz>
Fri, 22 Aug 2014 13:07:02 +0000 (15:07 +0200)
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>
15 files changed:
btrfs-calc-size.c
btrfs-convert.c
btrfs-corrupt-block.c
btrfs-crc.c
btrfs-debug-tree.c
btrfs-find-root.c
btrfs-fragments.c
btrfs-map-logical.c
btrfs-select-super.c
btrfs-show-super.c
btrfs-zero-log.c
btrfstune.c
cmds-check.c
cmds-restore.c
cmds-send.c

index 5eabdfc2b676ba603269b048145a8cbd9209236f..501111c732ad6349b4bc61b138d63660e64d048a 100644 (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();
                exit(1);
        }
index d62d4f81e614556ae041d4d4b7cf9ec3396e527b..952e3e6f7af3ff9f75d6bdc2a725fe142c71d583 100644 (file)
@@ -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;
        }
index 6ecbe47faf5ef0976c683f4631f6ce31b52b5db3..1a3ac3512cb71eaf5a6cb1acebbf2f372d9d0cec 100644 (file)
@@ -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];
 
index 1990534ad4d24bc2399ef92c4d42ae475183323d..723e0b7a8a24442b7b5529e151fe27427c24a7a1 100644 (file)
@@ -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;
        }
index 36e1115cfe4d40b84df6d0774ead92072cf634f5..e46500d8a7391c2ed608a0367d1f3e21813f230c 100644 (file)
@@ -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);
index e31a9b5983783eaaa86d4acd7803f265fc7d3da3..408d471ad57786b922e956763c91ac22657e8207 100644 (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();
                exit(1);
        }
index 160429ad360cd5b5ee0f12249eb311441c45e912..d03c2c3e7319e90e7ae1ee74fa693aedbf57c46d 100644 (file)
@@ -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);
index e47a1ae138fc8c788b47f7424cea66d5da6a283e..6b475fc69eab11cca3a80328099b9ab49d86bdde 100644 (file)
@@ -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();
index d7cd1873985cf59355647387aa0a567dec78eeec..6231d421128eab60eafbef7a17997f5b4f255641 100644 (file)
@@ -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) {
index ed0311fc20a32c7760953d12657bd8efd1a5ce44..38c5d262960f5c9173bc74095dd42b351722d265 100644 (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();
                exit(1);
        }
index ab7f418aa8e4a143b1acc3ca6a55f86f077c299f..88998e977155c85e87d736d96be0d13df9005f27 100644 (file)
@@ -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();
index 3f2f0cdc6fbba944a3c3415e8ebd51868671579c..b43c2f0cffbc40baea74fb4ec1137fc97505b2df 100644 (file)
@@ -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;
        }
index e2060cba58aab446d7d52ade51f62fe2cf495129..75965af36e34b3c7c175aad5354f15f1d877720a 100644 (file)
@@ -6634,7 +6634,7 @@ int cmd_check(int argc, char **argv)
        }
        argc = argc - optind;
 
-       if (argc != 1)
+       if (check_argc_exact(argc, 1))
                usage(cmd_check_usage);
 
        radix_tree_init();
index 3465f84202ecf73dedf7ec7fbeaedd87137ebeb5..8e8afc5ef44005ba6afa1b505db062754d1f8f71 100644 (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);
-       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) {
index 2df16be81766c68fd3729f06c88b78e1352fcc03..0e5bb5442f1ecf7ea7b63a7f34b4e08b44ea0047 100644 (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);
 
        if (outname != NULL) {