summaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
authorGui Hecheng <guihc.fnst@cn.fujitsu.com>2014-06-30 11:54:11 +0800
committerDavid Sterba <dsterba@suse.cz>2014-08-22 14:55:27 +0200
commita184abc70f7b1468e6036ab576f1587ee0574668 (patch)
treeb2195990a6e95695f163ab06952b386421e644c4 /utils.c
parent266c81a910540d85cc47463b2a0cb16b0dd4e1e1 (diff)
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 <guihc.fnst@cn.fujitsu.com> [moved argv0 and check_argc to utils.*] Signed-off-by: David Sterba <dsterba@suse.cz>
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c46
1 files changed, 46 insertions, 0 deletions
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
*/