From a184abc70f7b1468e6036ab576f1587ee0574668 Mon Sep 17 00:00:00 2001 From: Gui Hecheng Date: Mon, 30 Jun 2014 11:54:11 +0800 Subject: btrfs-progs: move the check_argc_* functions into utils.c To let the independent tools(e.g. btrfs-image, btrfs-convert, etc.) share the convenience of check_argc_* functions, just move it into utils.c. Also add a new function "set_argv0" to set the correct tool name: *btrfs-image*: too few arguments The original btrfs* tools work as before. Signed-off-by: Gui Hecheng [moved argv0 and check_argc to utils.*] Signed-off-by: David Sterba --- btrfs.c | 41 +---------------------------------------- cmds-property.c | 1 + commands.h | 6 ------ help.c | 1 + utils.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 9 +++++++++ 6 files changed, 58 insertions(+), 46 deletions(-) diff --git a/btrfs.c b/btrfs.c index f8b87af2..e83349cb 100644 --- a/btrfs.c +++ b/btrfs.c @@ -22,6 +22,7 @@ #include "crc32c.h" #include "commands.h" #include "version.h" +#include "utils.h" static const char * const btrfs_cmd_group_usage[] = { "btrfs [--help] [--version] [...] []", @@ -31,8 +32,6 @@ static const char * const btrfs_cmd_group_usage[] = { static const char btrfs_cmd_group_info[] = "Use --help as an argument for information on a specific group or command."; -static char argv0_buf[ARGV0_BUF_SIZE] = "btrfs"; - static inline const char *skip_prefix(const char *str, const char *prefix) { size_t len = strlen(prefix); @@ -125,14 +124,6 @@ static void handle_help_options_next_level(const struct cmd_struct *cmd, } } -static void fixup_argv0(char **argv, const char *token) -{ - int len = strlen(argv0_buf); - - snprintf(argv0_buf + len, sizeof(argv0_buf) - len, " %s", token); - argv[0] = argv0_buf; -} - int handle_command_group(const struct cmd_group *grp, int argc, char **argv) @@ -154,36 +145,6 @@ int handle_command_group(const struct cmd_group *grp, int argc, return cmd->fn(argc, argv); } -int check_argc_exact(int nargs, int expected) -{ - if (nargs < expected) - fprintf(stderr, "%s: too few arguments\n", argv0_buf); - if (nargs > expected) - fprintf(stderr, "%s: too many arguments\n", argv0_buf); - - return nargs != expected; -} - -int check_argc_min(int nargs, int expected) -{ - if (nargs < expected) { - fprintf(stderr, "%s: too few arguments\n", argv0_buf); - return 1; - } - - return 0; -} - -int check_argc_max(int nargs, int expected) -{ - if (nargs > expected) { - fprintf(stderr, "%s: too many arguments\n", argv0_buf); - return 1; - } - - return 0; -} - static const struct cmd_group btrfs_cmd_group; static const char * const cmd_help_usage[] = { diff --git a/cmds-property.c b/cmds-property.c index 8b4869fd..9c4eb92c 100644 --- a/cmds-property.c +++ b/cmds-property.c @@ -25,6 +25,7 @@ #include "commands.h" #include "props.h" #include "ctree.h" +#include "utils.h" static const char * const property_cmd_group_usage[] = { "btrfs property get/set/list [-t ] [] [value]", diff --git a/commands.h b/commands.h index db700438..4d870f6a 100644 --- a/commands.h +++ b/commands.h @@ -14,8 +14,6 @@ * Boston, MA 021110-1307, USA. */ -#define ARGV0_BUF_SIZE 64 - struct cmd_struct { const char *token; int (*fn)(int, char **); @@ -62,10 +60,6 @@ struct cmd_group { /* btrfs.c */ int prefixcmp(const char *str, const char *prefix); -int check_argc_exact(int nargs, int expected); -int check_argc_min(int nargs, int expected); -int check_argc_max(int nargs, int expected); - int handle_command_group(const struct cmd_group *grp, int argc, char **argv); diff --git a/help.c b/help.c index 09dc706a..fab942b4 100644 --- a/help.c +++ b/help.c @@ -19,6 +19,7 @@ #include #include "commands.h" +#include "utils.h" static char argv0_buf[ARGV0_BUF_SIZE]; diff --git a/utils.c b/utils.c index c139eb2e..27a0079c 100644 --- a/utils.c +++ b/utils.c @@ -52,6 +52,52 @@ #define BLKDISCARD _IO(0x12,119) #endif +static char argv0_buf[ARGV0_BUF_SIZE] = "btrfs"; + +void fixup_argv0(char **argv, const char *token) +{ + int len = strlen(argv0_buf); + + snprintf(argv0_buf + len, sizeof(argv0_buf) - len, " %s", token); + argv[0] = argv0_buf; +} + +void set_argv0(char **argv) +{ + sprintf(argv0_buf, "%s", argv[0]); +} + +int check_argc_exact(int nargs, int expected) +{ + if (nargs < expected) + fprintf(stderr, "%s: too few arguments\n", argv0_buf); + if (nargs > expected) + fprintf(stderr, "%s: too many arguments\n", argv0_buf); + + return nargs != expected; +} + +int check_argc_min(int nargs, int expected) +{ + if (nargs < expected) { + fprintf(stderr, "%s: too few arguments\n", argv0_buf); + return 1; + } + + return 0; +} + +int check_argc_max(int nargs, int expected) +{ + if (nargs > expected) { + fprintf(stderr, "%s: too many arguments\n", argv0_buf); + return 1; + } + + return 0; +} + + /* * Discard the given range in one go */ diff --git a/utils.h b/utils.h index 6599ee4d..e29ab936 100644 --- a/utils.h +++ b/utils.h @@ -39,6 +39,15 @@ #define BTRFS_UUID_UNPARSED_SIZE 37 +#define ARGV0_BUF_SIZE 64 + +int check_argc_exact(int nargs, int expected); +int check_argc_min(int nargs, int expected); +int check_argc_max(int nargs, int expected); + +void fixup_argv0(char **argv, const char *token); +void set_argv0(char **argv); + int make_btrfs(int fd, const char *device, const char *label, char *fs_uuid, u64 blocks[6], u64 num_bytes, u32 nodesize, u32 leafsize, u32 sectorsize, u32 stripesize, u64 features); -- cgit v1.2.3