X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libbcachefs.c;h=65ca07e2d7d57e13bab3f1a977e8bd9a2b01aa58;hb=cec9ecc7e0575c7c88bd048e1d24549a6b3c8fe6;hp=e5dcfd8f7bf9a07bd3cb9f4ed0965422262a5cf8;hpb=08e14f84d9ca9b06443d7e54989731d9e03a6dc0;p=bcachefs-tools-debian diff --git a/libbcachefs.c b/libbcachefs.c index e5dcfd8..65ca07e 100644 --- a/libbcachefs.c +++ b/libbcachefs.c @@ -1006,6 +1006,16 @@ int bchu_data(struct bchfs_handle fs, struct bch_ioctl_data cmd) /* option parsing */ +void bch2_opt_strs_free(struct bch_opt_strs *opts) +{ + unsigned i; + + for (i = 0; i < bch2_opts_nr; i++) { + free(opts->by_id[i]); + opts->by_id[i] = NULL; + } +} + struct bch_opt_strs bch2_cmdline_opts_get(int *argc, char *argv[], unsigned opt_types) { @@ -1038,9 +1048,8 @@ struct bch_opt_strs bch2_cmdline_opts_get(int *argc, char *argv[], optid = bch2_opt_lookup(optstr); if (optid < 0 || !(bch2_opt_table[optid].mode & opt_types)) { - free(optstr); i++; - continue; + goto next; } if (!valstr && @@ -1052,13 +1061,15 @@ struct bch_opt_strs bch2_cmdline_opts_get(int *argc, char *argv[], if (!valstr) valstr = "1"; - opts.by_id[optid] = valstr; + opts.by_id[optid] = strdup(valstr); *argc -= nr_args; memmove(&argv[i], &argv[i + nr_args], sizeof(char *) * (*argc - i)); argv[*argc] = NULL; +next: + free(optstr); } return opts; @@ -1089,22 +1100,17 @@ struct bch_opts bch2_parse_opts(struct bch_opt_strs strs) return opts; } +#define newline(c) \ + do { \ + printf("\n"); \ + c = 0; \ + } while(0) void bch2_opts_usage(unsigned opt_types) { const struct bch_option *opt; unsigned i, c = 0, helpcol = 30; - void tabalign() { - while (c < helpcol) { - putchar(' '); - c++; - } - } - void newline() { - printf("\n"); - c = 0; - } for (opt = bch2_opt_table; opt < bch2_opt_table + bch2_opts_nr; @@ -1135,21 +1141,24 @@ void bch2_opts_usage(unsigned opt_types) const char *l = opt->help; if (c >= helpcol) - newline(); + newline(c); while (1) { const char *n = strchrnul(l, '\n'); - tabalign(); + while (c < helpcol) { + putchar(' '); + c++; + } printf("%.*s", (int) (n - l), l); - newline(); + newline(c); if (!*n) break; l = n + 1; } } else { - newline(); + newline(c); } } }