linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Gui Hecheng <guihc.fnst@cn.fujitsu.com>
To: <dsterba@suse.cz>
Cc: <linux-btrfs@vger.kernel.org>, Gui Hecheng <guihc.fnst@cn.fujitsu.com>
Subject: [PATCH 1/2] btrfs-progs: move the check_argc_* functions into utils.c
Date: Mon, 30 Jun 2014 11:54:11 +0800	[thread overview]
Message-ID: <1404100452-8894-1-git-send-email-guihc.fnst@cn.fujitsu.com> (raw)
In-Reply-To: <20140627123528.GC1553@twin.jikos.cz>

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 <guihc.fnst@cn.fujitsu.com>
---
 btrfs.c | 41 +----------------------------------------
 utils.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 utils.h |  7 +++++++
 3 files changed, 55 insertions(+), 40 deletions(-)

diff --git a/btrfs.c b/btrfs.c
index 25257b6..685455f 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] <group> [<group>...] <command> [<args>]",
@@ -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/utils.c b/utils.c
index 8ce3be9..fd59f58 100644
--- a/utils.c
+++ b/utils.c
@@ -50,11 +50,58 @@
 #include "volumes.h"
 #include "ioctl.h"
 #include "btrfs-list.h"
+#include "commands.h"
 
 #ifndef BLKDISCARD
 #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 ad772c2..f05477a 100644
--- a/utils.h
+++ b/utils.h
@@ -47,6 +47,13 @@
 #define UNITS_DECIMAL			(3)
 #define UNITS_HUMAN			UNITS_BINARY
 
+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);
-- 
1.8.1.4


  parent reply	other threads:[~2014-06-30  3:59 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-26  2:53 [PATCH 1/6] btrfs-progs: fix btrfs-image old_restore fsck failure Gui Hecheng
2014-06-26  2:53 ` [PATCH 2/6] btrfs-progs: deal with malloc failure in btrfs-image Gui Hecheng
2014-06-26  2:53 ` [PATCH 3/6] btrfs-progs: cleanup unnecessary free if malloc fails " Gui Hecheng
2014-06-26  2:53 ` [PATCH 4/6] btrfs-progs: cleanup possible silent failure " Gui Hecheng
2014-06-26  2:53 ` [PATCH 5/6] btrfs-progs: limit minimal num of args for btrfs-image Gui Hecheng
2014-06-27 12:35   ` David Sterba
2014-06-30  1:47     ` Gui Hecheng
2014-06-30  3:54     ` Gui Hecheng [this message]
2014-06-30  3:54       ` [PATCH v2 2/2] " Gui Hecheng
2014-07-01 23:11       ` [PATCH 1/2] btrfs-progs: move the check_argc_* functions into utils.c David Sterba
2014-07-02  0:20         ` WorMzy Tykashi
2014-07-02  9:34           ` David Sterba
2014-07-02 10:50             ` WorMzy Tykashi
2014-07-02  1:13         ` Gui Hecheng
2014-07-10  1:06         ` [PATCH] btrfs-progs: use check_argc_* to check arg number for all tools Gui Hecheng
2014-07-16  3:44           ` [PATCH v2] " Gui Hecheng
2014-07-16  3:58             ` Gui Hecheng
2014-07-16  3:59           ` Gui Hecheng
2014-06-26  2:53 ` [PATCH 6/6] btrfs-progs: use BTRFS_SUPER_INFO_SIZE to replace raw 4096 in btrfs-image Gui Hecheng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1404100452-8894-1-git-send-email-guihc.fnst@cn.fujitsu.com \
    --to=guihc.fnst@cn.fujitsu.com \
    --cc=dsterba@suse.cz \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).