summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Sterba <dsterba@suse.com>2016-01-14 10:30:35 +0100
committerDavid Sterba <dsterba@suse.com>2016-01-14 11:07:10 +0100
commita6cc8ea10ad84e4b7d1303ed048e644f0dd7ae04 (patch)
tree028fb30b5e2bcaac84f849b441ca9c3284371fe7
parentc26dc1ef339c373c59a7c1626bb21d64811476ee (diff)
downloadbtrfs-progs-a6cc8ea10ad84e4b7d1303ed048e644f0dd7ae04.tar.gz
btrfs-progs: introduce helper for parsing args without options
All commands should support the "--" option separator. This is transparently handled by getopt, but we don't use that everywhere. Introduce a helper for commands that take no options (just the path). The object file dependencies need to be adjusted a bit. Signed-off-by: David Sterba <dsterba@suse.com>
-rw-r--r--Makefile.in6
-rw-r--r--utils.c28
-rw-r--r--utils.h1
3 files changed, 32 insertions, 3 deletions
diff --git a/Makefile.in b/Makefile.in
index 112c3ba..1bd497a 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -70,7 +70,7 @@ objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
extent-cache.o extent_io.o volumes.o utils.o repair.o \
qgroup.o raid6.o free-space-cache.o list_sort.o props.o \
ulist.o qgroup-verify.o backref.o string-table.o task-utils.o \
- inode.o file.o find-root.o free-space-tree.o
+ inode.o file.o find-root.o free-space-tree.o help.o
cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
cmds-inspect.o cmds-balance.o cmds-send.o cmds-receive.o \
cmds-quota.o cmds-qgroup.o cmds-replace.o cmds-check.o \
@@ -260,9 +260,9 @@ btrfs-%: $(objects) $(libs_static) btrfs-%.o
$(Q)$(CC) $(CFLAGS) -o $@ $(objects) $@.o $(libs_static) \
$(LDFLAGS) $(LIBS) $($(subst -,_,$@-libs))
-btrfs: $(objects) btrfs.o help.o $(cmds_objects) $(libs_static)
+btrfs: $(objects) btrfs.o $(cmds_objects) $(libs_static)
@echo " [LD] $@"
- $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \
+ $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o $(cmds_objects) \
$(objects) $(libs_static) $(LDFLAGS) $(LIBS)
btrfs.static: $(static_objects) btrfs.static.o help.static.o $(static_cmds_objects) $(static_libbtrfs_objects)
diff --git a/utils.c b/utils.c
index ad3ada0..3df8b42 100644
--- a/utils.c
+++ b/utils.c
@@ -37,6 +37,7 @@
#include <sys/vfs.h>
#include <sys/statfs.h>
#include <linux/magic.h>
+#include <getopt.h>
#include "kerncompat.h"
#include "radix-tree.h"
@@ -47,6 +48,7 @@
#include "utils.h"
#include "volumes.h"
#include "ioctl.h"
+#include "commands.h"
#ifndef BLKDISCARD
#define BLKDISCARD _IO(0x12,119)
@@ -3119,3 +3121,29 @@ int string_is_numerical(const char *str)
return 0;
return 1;
}
+
+/*
+ * Preprocess @argv with getopt_long to reorder options and consume the "--"
+ * option separator.
+ * Unknown short and long options are reported, optionally the @usage is printed
+ * before exit.
+ */
+void clean_args_no_options(int argc, char *argv[], const char * const *usagestr)
+{
+ static const struct option long_options[] = {
+ {NULL, 0, NULL, 0}
+ };
+
+ while (1) {
+ int c = getopt_long(argc, argv, "", long_options, NULL);
+
+ if (c < 0)
+ break;
+
+ switch (c) {
+ default:
+ if (usagestr)
+ usage(usagestr);
+ }
+ }
+}
diff --git a/utils.h b/utils.h
index e522a8c..d53357a 100644
--- a/utils.h
+++ b/utils.h
@@ -274,6 +274,7 @@ const char *get_argv0_buf(void);
"-t|--tbytes show sizes in TiB, or TB with --si"
unsigned int get_unit_mode_from_arg(int *argc, char *argv[], int df_mode);
+void clean_args_no_options(int argc, char *argv[], const char * const *usage);
int string_is_numerical(const char *str);
__attribute__ ((format (printf, 1, 2)))