X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=btrfs.c;h=2d39f2ced3e81b3d425b12971306df76495583d6;hb=a7e56ef21b666b26e9668094d4c171788a914840;hp=e83349cbb770d66b4147abf453501dbd57524234;hpb=a184abc70f7b1468e6036ab576f1587ee0574668;p=platform%2Fupstream%2Fbtrfs-progs.git diff --git a/btrfs.c b/btrfs.c index e83349c..2d39f2c 100644 --- a/btrfs.c +++ b/btrfs.c @@ -14,15 +14,16 @@ * Boston, MA 021110-1307, USA. */ -#define _GNU_SOURCE #include #include #include +#include +#include "volumes.h" #include "crc32c.h" #include "commands.h" -#include "version.h" #include "utils.h" +#include "help.h" static const char * const btrfs_cmd_group_usage[] = { "btrfs [--help] [--version] [...] []", @@ -38,15 +39,6 @@ static inline const char *skip_prefix(const char *str, const char *prefix) return strncmp(str, prefix, len) ? NULL : str + len; } -int prefixcmp(const char *str, const char *prefix) -{ - for (; ; str++, prefix++) - if (!*prefix) - return 0; - else if (*str != *prefix) - return (unsigned char)*prefix - (unsigned char)*str; -} - static int parse_one_token(const char *arg, const struct cmd_group *grp, const struct cmd_struct **cmd_ret) { @@ -169,24 +161,81 @@ static const char * const cmd_version_usage[] = { static int cmd_version(int argc, char **argv) { - printf("%s\n", BTRFS_BUILD_VERSION); + printf("%s\n", PACKAGE_STRING); return 0; } -static void handle_options(int *argc, char ***argv) +/* + * Parse global options, between binary name and first non-option argument + * after processing all valid options (including those with arguments). + * + * Returns index to argv where parsting stopped, optind is reset to 1 + */ +static int handle_global_options(int argc, char **argv) { - if (*argc > 0) { - const char *arg = (*argv)[0]; - if (arg[0] != '-' || - !strcmp(arg, "--help") || - !strcmp(arg, "--version")) - return; - fprintf(stderr, "Unknown option: %s\n", arg); - fprintf(stderr, "usage: %s\n", - btrfs_cmd_group.usagestr[0]); - exit(129); + enum { OPT_HELP = 256, OPT_VERSION, OPT_FULL }; + static const struct option long_options[] = { + { "help", no_argument, NULL, OPT_HELP }, + { "version", no_argument, NULL, OPT_VERSION }, + { "full", no_argument, NULL, OPT_FULL }, + { NULL, 0, NULL, 0} + }; + int shift; + + if (argc == 0) + return 0; + + opterr = 0; + while (1) { + int c; + + c = getopt_long(argc, argv, "+", long_options, NULL); + if (c < 0) + break; + + switch (c) { + case OPT_HELP: break; + case OPT_VERSION: break; + case OPT_FULL: break; + default: + fprintf(stderr, "Unknown global option: %s\n", + argv[optind - 1]); + exit(129); + } } - return; + + shift = optind; + optind = 1; + + return shift; +} + +void handle_special_globals(int shift, int argc, char **argv) +{ + int has_help = 0; + int has_full = 0; + int i; + + for (i = 0; i < shift; i++) { + if (strcmp(argv[i], "--help") == 0) + has_help = 1; + else if (strcmp(argv[i], "--full") == 0) + has_full = 1; + } + + if (has_help) { + if (has_full) + usage_command_group(&btrfs_cmd_group, 1, 0); + else + cmd_help(argc, argv); + exit(0); + } + + for (i = 0; i < shift; i++) + if (strcmp(argv[i], "--version") == 0) { + cmd_version(argc, argv); + exit(0); + } } static const struct cmd_group btrfs_cmd_group = { @@ -216,6 +265,9 @@ int main(int argc, char **argv) { const struct cmd_struct *cmd; const char *bname; + int ret; + + btrfs_config_init(); if ((bname = strrchr(argv[0], '/')) != NULL) bname++; @@ -225,14 +277,16 @@ int main(int argc, char **argv) if (!strcmp(bname, "btrfsck")) { argv[0] = "check"; } else { - argc--; - argv++; - handle_options(&argc, &argv); - if (argc > 0) { - if (!prefixcmp(argv[0], "--")) - argv[0] += 2; - } else { - usage_command_group(&btrfs_cmd_group, 0, 0); + int shift; + + shift = handle_global_options(argc, argv); + handle_special_globals(shift, argc, argv); + while (shift-- > 0) { + argc--; + argv++; + } + if (argc == 0) { + usage_command_group_short(&btrfs_cmd_group); exit(1); } } @@ -244,5 +298,10 @@ int main(int argc, char **argv) crc32c_optimization_init(); fixup_argv0(argv, cmd->token); - exit(cmd->fn(argc, argv)); + + ret = cmd->fn(argc, argv); + + btrfs_close_all_devices(); + + exit(ret); }