]> git.ozlabs.org Git - ccan/commitdiff
ccan: update show callback to take explicit length.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 8 May 2023 01:30:26 +0000 (11:00 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 20 Jun 2023 03:08:36 +0000 (12:38 +0930)
For now we still use 80, but we could vary that in future.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/opt/helpers.c
ccan/opt/opt.c
ccan/opt/opt.h
ccan/opt/test/run-add_desc.c
ccan/opt/test/run-helpers.c
ccan/opt/test/utils.c
ccan/opt/test/utils.h
ccan/opt/usage.c

index 118e543602e06b981e6faee7dd5f416cf819d5cd..0a6cb345f807a4a27dea1c4bfb2f907016a4c7f4 100644 (file)
@@ -138,10 +138,10 @@ char *opt_set_floatval(const char *arg, float *f)
        return NULL;
 }
 
-void opt_show_floatval(char buf[OPT_SHOW_LEN], const float *f)
+void opt_show_floatval(char *buf, size_t len, const float *f)
 {
        double d = *f;
-       opt_show_doubleval(buf, &d);
+       opt_show_doubleval(buf, len, &d);
 }
 
 char *opt_set_doubleval(const char *arg, double *d)
@@ -160,9 +160,9 @@ char *opt_set_doubleval(const char *arg, double *d)
        return NULL;
 }
 
-void opt_show_doubleval(char buf[OPT_SHOW_LEN], const double *d)
+void opt_show_doubleval(char *buf, size_t len, const double *d)
 {
-       snprintf(buf, OPT_SHOW_LEN, "%f", *d);
+       snprintf(buf, len, "%f", *d);
 }
 
 char *opt_inc_intval(int *i)
@@ -196,52 +196,54 @@ char *opt_usage_and_exit(const char *extra)
        exit(0);
 }
 
-void opt_show_bool(char buf[OPT_SHOW_LEN], const bool *b)
+void opt_show_bool(char *buf, size_t len, const bool *b)
 {
-       strncpy(buf, *b ? "true" : "false", OPT_SHOW_LEN);
+       strncpy(buf, *b ? "true" : "false", len);
 }
 
-void opt_show_invbool(char buf[OPT_SHOW_LEN], const bool *b)
+void opt_show_invbool(char *buf, size_t len, const bool *b)
 {
-       strncpy(buf, *b ? "false" : "true", OPT_SHOW_LEN);
+       strncpy(buf, *b ? "false" : "true", len);
 }
 
-void opt_show_charp(char buf[OPT_SHOW_LEN], char *const *p)
+void opt_show_charp(char *buf, size_t len, char *const *p)
 {
-       if (*p){
-               size_t len = strlen(*p);
+       if (*p) {
+               size_t plen = strlen(*p);
+               if (len < 2)
+                       return;
                buf[0] = '"';
-               if (len > OPT_SHOW_LEN - 2)
-                       len = OPT_SHOW_LEN - 2;
-               strncpy(buf+1, *p, len);
-               buf[1+len] = '"';
-               if (len < OPT_SHOW_LEN - 2)
-                       buf[2+len] = '\0';
+               if (plen > len - 2)
+                       plen = len - 2;
+               strncpy(buf+1, *p, plen);
+               buf[1+plen] = '"';
+               if (plen < len - 2)
+                       buf[2+plen] = '\0';
        }
        else {
-               strncpy(buf, "(nil)", OPT_SHOW_LEN);
+               strncpy(buf, "(nil)", len);
        }
 }
 
 /* Show an integer value, various forms. */
-void opt_show_intval(char buf[OPT_SHOW_LEN], const int *i)
+void opt_show_intval(char *buf, size_t len, const int *i)
 {
-       snprintf(buf, OPT_SHOW_LEN, "%i", *i);
+       snprintf(buf, len, "%i", *i);
 }
 
-void opt_show_uintval(char buf[OPT_SHOW_LEN], const unsigned int *ui)
+void opt_show_uintval(char *buf, size_t len, const unsigned int *ui)
 {
-       snprintf(buf, OPT_SHOW_LEN, "%u", *ui);
+       snprintf(buf, len, "%u", *ui);
 }
 
-void opt_show_longval(char buf[OPT_SHOW_LEN], const long *l)
+void opt_show_longval(char *buf, size_t len, const long *l)
 {
-       snprintf(buf, OPT_SHOW_LEN, "%li", *l);
+       snprintf(buf, len, "%li", *l);
 }
 
-void opt_show_ulongval(char buf[OPT_SHOW_LEN], const unsigned long *ul)
+void opt_show_ulongval(char *buf, size_t len, const unsigned long *ul)
 {
-       snprintf(buf, OPT_SHOW_LEN, "%lu", *ul);
+       snprintf(buf, len, "%lu", *ul);
 }
 
 /* a helper function that multiplies out an argument's kMGTPE suffix in the
@@ -447,14 +449,14 @@ char * opt_set_uintval_si(const char *arg, unsigned int *u)
   are separate but essentially identical functions for signed and unsigned
   values, so that unsigned values greater than LLONG_MAX get suffixes.
  */
-static void show_llong_with_suffix(char buf[OPT_SHOW_LEN], long long ll,
-                                   const long long base)
+static void show_llong_with_suffix(char *buf, size_t len, long long ll,
+                                  const long long base)
 {
        const char *suffixes = "kMGTPE";
        int i;
        if (ll == 0){
                /*zero is special because everything divides it (you'd get "0E")*/
-               snprintf(buf, OPT_SHOW_LEN, "0");
+               snprintf(buf, len, "0");
                return;
        }
        for (i = 0; i < strlen(suffixes); i++){
@@ -464,19 +466,20 @@ static void show_llong_with_suffix(char buf[OPT_SHOW_LEN], long long ll,
                ll = tmp;
        }
        if (i == 0)
-               snprintf(buf, OPT_SHOW_LEN, "%"PRId64, (int64_t)ll);
+               snprintf(buf, len, "%"PRId64, (int64_t)ll);
        else
-               snprintf(buf, OPT_SHOW_LEN, "%"PRId64"%c", (int64_t)ll, suffixes[i - 1]);
+               snprintf(buf, len, "%"PRId64"%c", (int64_t)ll, suffixes[i - 1]);
 }
 
-static void show_ullong_with_suffix(char buf[OPT_SHOW_LEN], unsigned long long ull,
+static void show_ullong_with_suffix(char *buf, size_t len,
+                                   unsigned long long ull,
                                    const unsigned base)
 {
        const char *suffixes = "kMGTPE";
        int i;
        if (ull == 0){
                /*zero is special because everything divides it (you'd get "0E")*/
-               snprintf(buf, OPT_SHOW_LEN, "0");
+               snprintf(buf, len, "0");
                return;
        }
        for (i = 0; i < strlen(suffixes); i++){
@@ -486,72 +489,72 @@ static void show_ullong_with_suffix(char buf[OPT_SHOW_LEN], unsigned long long u
                ull = tmp;
        }
        if (i == 0)
-               snprintf(buf, OPT_SHOW_LEN, "%"PRIu64, (uint64_t)ull);
+               snprintf(buf, len, "%"PRIu64, (uint64_t)ull);
        else
-               snprintf(buf, OPT_SHOW_LEN, "%"PRIu64"%c", (uint64_t)ull, suffixes[i - 1]);
+               snprintf(buf, len, "%"PRIu64"%c", (uint64_t)ull, suffixes[i - 1]);
 }
 
 /* _bi, signed */
-void opt_show_intval_bi(char buf[OPT_SHOW_LEN], const int *x)
+void opt_show_intval_bi(char *buf, size_t len, const int *x)
 {
-       show_llong_with_suffix(buf, *x, 1024);
+       show_llong_with_suffix(buf, len, *x, 1024);
 }
 
-void opt_show_longval_bi(char buf[OPT_SHOW_LEN], const long *x)
+void opt_show_longval_bi(char *buf, size_t len, const long *x)
 {
-       show_llong_with_suffix(buf, *x, 1024);
+       show_llong_with_suffix(buf, len, *x, 1024);
 }
 
-void opt_show_longlongval_bi(char buf[OPT_SHOW_LEN], const long long *x)
+void opt_show_longlongval_bi(char *buf, size_t len, const long long *x)
 {
-       show_llong_with_suffix(buf, *x, 1024);
+       show_llong_with_suffix(buf, len, *x, 1024);
 }
 
 /* _bi, unsigned */
-void opt_show_uintval_bi(char buf[OPT_SHOW_LEN], const unsigned int *x)
+void opt_show_uintval_bi(char *buf, size_t len, const unsigned int *x)
 {
-       show_ullong_with_suffix(buf, (unsigned long long) *x, 1024);
+       show_ullong_with_suffix(buf, len, (unsigned long long) *x, 1024);
 }
 
-void opt_show_ulongval_bi(char buf[OPT_SHOW_LEN], const unsigned long *x)
+void opt_show_ulongval_bi(char *buf, size_t len, const unsigned long *x)
 {
-       show_ullong_with_suffix(buf, (unsigned long long) *x, 1024);
+       show_ullong_with_suffix(buf, len, (unsigned long long) *x, 1024);
 }
 
-void opt_show_ulonglongval_bi(char buf[OPT_SHOW_LEN], const unsigned long long *x)
+void opt_show_ulonglongval_bi(char *buf, size_t len, const unsigned long long *x)
 {
-       show_ullong_with_suffix(buf, (unsigned long long) *x, 1024);
+       show_ullong_with_suffix(buf, len, (unsigned long long) *x, 1024);
 }
 
 /* _si, signed */
-void opt_show_intval_si(char buf[OPT_SHOW_LEN], const int *x)
+void opt_show_intval_si(char *buf, size_t len, const int *x)
 {
-       show_llong_with_suffix(buf, (long long) *x, 1000);
+       show_llong_with_suffix(buf, len, (long long) *x, 1000);
 }
 
-void opt_show_longval_si(char buf[OPT_SHOW_LEN], const long *x)
+void opt_show_longval_si(char *buf, size_t len, const long *x)
 {
-       show_llong_with_suffix(buf, (long long) *x, 1000);
+       show_llong_with_suffix(buf, len, (long long) *x, 1000);
 }
 
-void opt_show_longlongval_si(char buf[OPT_SHOW_LEN], const long long *x)
+void opt_show_longlongval_si(char *buf, size_t len, const long long *x)
 {
-       show_llong_with_suffix(buf, *x, 1000);
+       show_llong_with_suffix(buf, len, *x, 1000);
 }
 
 /* _si, unsigned */
-void opt_show_uintval_si(char buf[OPT_SHOW_LEN], const unsigned int *x)
+void opt_show_uintval_si(char *buf, size_t len, const unsigned int *x)
 {
-       show_ullong_with_suffix(buf, (unsigned long long) *x, 1000);
+       show_ullong_with_suffix(buf, len, (unsigned long long) *x, 1000);
 }
 
-void opt_show_ulongval_si(char buf[OPT_SHOW_LEN], const unsigned long *x)
+void opt_show_ulongval_si(char *buf, size_t len, const unsigned long *x)
 {
-       show_ullong_with_suffix(buf, (unsigned long long) *x, 1000);
+       show_ullong_with_suffix(buf, len, (unsigned long long) *x, 1000);
 }
 
-void opt_show_ulonglongval_si(char buf[OPT_SHOW_LEN], const unsigned long long *x)
+void opt_show_ulonglongval_si(char *buf, size_t len, const unsigned long long *x)
 {
-       show_ullong_with_suffix(buf, (unsigned long long) *x, 1000);
+       show_ullong_with_suffix(buf, len, (unsigned long long) *x, 1000);
 }
 
index ef711d2c51889190c4190cc4f85c54c470808abc..09c8508142341730002a03f6ab4cf9589b1ade61 100644 (file)
@@ -162,7 +162,7 @@ static void add_opt(const struct opt_table *entry)
 void _opt_register(const char *names, enum opt_type type,
                   char *(*cb)(void *arg),
                   char *(*cb_arg)(const char *optarg, void *arg),
-                  void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
+                  void (*show)(char *buf, size_t len, const void *arg),
                   const void *arg, const char *desc)
 {
        struct opt_table opt;
index 9eec73854bb144f9051484578ff67f1af94ac386..9c9f337d0a61685bf301883b9a5d1d01bb93a82b 100644 (file)
@@ -47,10 +47,10 @@ struct opt_table;
  * where "type" is the type of the @arg argument.  The first argument to the
  * @cb is the argument found on the commandline.
  *
- * Similarly, if @show is not NULL, it should be of type "void *show(char *,
- * const type *)".  It should write up to OPT_SHOW_LEN bytes into the first
- * argument; unless it uses the entire OPT_SHOW_LEN bytes it should
- * nul-terminate that buffer.
+ * Similarly, if @show is not NULL, it should be of type "void show(char *,
+ * size_t len, const type *)".  It should write up to len bytes into the first
+ * argument; unless it uses the entire len bytes it should nul-terminate that
+ * buffer.
  *
  * Any number of equivalent short or long options can be listed in @names,
  * separated by '|'.  Short options are a single hyphen followed by a single
@@ -429,40 +429,37 @@ void opt_usage_exit_fail(const char *msg, ...) NORETURN;
  */
 extern const char opt_hidden[];
 
-/* Maximum length of arg to show in opt_usage */
-#define OPT_SHOW_LEN 80
-
 /* Standard helpers.  You can write your own: */
 /* Sets the @b to true. */
 char *opt_set_bool(bool *b);
 /* Sets @b based on arg: (yes/no/true/false). */
 char *opt_set_bool_arg(const char *arg, bool *b);
-void opt_show_bool(char buf[OPT_SHOW_LEN], const bool *b);
+void opt_show_bool(char *buf, size_t len, const bool *b);
 /* The inverse */
 char *opt_set_invbool(bool *b);
-void opt_show_invbool(char buf[OPT_SHOW_LEN], const bool *b);
+void opt_show_invbool(char *buf, size_t len, const bool *b);
 /* Sets @b based on !arg: (yes/no/true/false). */
 char *opt_set_invbool_arg(const char *arg, bool *b);
 
 /* Set a char *. */
 char *opt_set_charp(const char *arg, char **p);
-void opt_show_charp(char buf[OPT_SHOW_LEN], char *const *p);
+void opt_show_charp(char *buf, size_t len, char *const *p);
 
 /* Set an integer value, various forms.  Sets to 1 on arg == NULL. */
 char *opt_set_intval(const char *arg, int *i);
-void opt_show_intval(char buf[OPT_SHOW_LEN], const int *i);
+void opt_show_intval(char *buf, size_t len, const int *i);
 char *opt_set_uintval(const char *arg, unsigned int *ui);
-void opt_show_uintval(char buf[OPT_SHOW_LEN], const unsigned int *ui);
+void opt_show_uintval(char *buf, size_t len, const unsigned int *ui);
 char *opt_set_longval(const char *arg, long *l);
-void opt_show_longval(char buf[OPT_SHOW_LEN], const long *l);
+void opt_show_longval(char *buf, size_t len, const long *l);
 char *opt_set_ulongval(const char *arg, unsigned long *ul);
-void opt_show_ulongval(char buf[OPT_SHOW_LEN], const unsigned long *ul);
+void opt_show_ulongval(char *buf, size_t len, const unsigned long *ul);
 
 /* Set an floating point value, various forms. */
 char *opt_set_floatval(const char *arg, float *f);
-void opt_show_floatval(char buf[OPT_SHOW_LEN], const float *f);
+void opt_show_floatval(char *buf, size_t len, const float *f);
 char *opt_set_doubleval(const char *arg, double *d);
-void opt_show_doubleval(char buf[OPT_SHOW_LEN], const double *d);
+void opt_show_doubleval(char *buf, size_t len, const double *d);
 
 /* the following setting functions accept k, M, G, T, P, or E suffixes, which
    multiplies the numeric value by the corresponding power of 1000 or 1024
@@ -482,19 +479,19 @@ char *opt_set_ulonglongval_bi(const char *arg, unsigned long long *ll);
 char *opt_set_ulonglongval_si(const char *arg, unsigned long long *ll);
 
 
-void opt_show_intval_bi(char buf[OPT_SHOW_LEN], const int *x);
-void opt_show_longval_bi(char buf[OPT_SHOW_LEN], const long *x);
-void opt_show_longlongval_bi(char buf[OPT_SHOW_LEN], const long long *x);
-void opt_show_uintval_bi(char buf[OPT_SHOW_LEN], const unsigned int *x);
-void opt_show_ulongval_bi(char buf[OPT_SHOW_LEN], const unsigned long *x);
-void opt_show_ulonglongval_bi(char buf[OPT_SHOW_LEN], const unsigned long long *x);
+void opt_show_intval_bi(char *buf, size_t len, const int *x);
+void opt_show_longval_bi(char *buf, size_t len, const long *x);
+void opt_show_longlongval_bi(char *buf, size_t len, const long long *x);
+void opt_show_uintval_bi(char *buf, size_t len, const unsigned int *x);
+void opt_show_ulongval_bi(char *buf, size_t len, const unsigned long *x);
+void opt_show_ulonglongval_bi(char *buf, size_t len, const unsigned long long *x);
 
-void opt_show_intval_si(char buf[OPT_SHOW_LEN], const int *x);
-void opt_show_longval_si(char buf[OPT_SHOW_LEN], const long *x);
-void opt_show_longlongval_si(char buf[OPT_SHOW_LEN], const long long *x);
-void opt_show_uintval_si(char buf[OPT_SHOW_LEN], const unsigned int *x);
-void opt_show_ulongval_si(char buf[OPT_SHOW_LEN], const unsigned long *x);
-void opt_show_ulonglongval_si(char buf[OPT_SHOW_LEN], const unsigned long long *x);
+void opt_show_intval_si(char *buf, size_t len, const int *x);
+void opt_show_longval_si(char *buf, size_t len, const long *x);
+void opt_show_longlongval_si(char *buf, size_t len, const long long *x);
+void opt_show_uintval_si(char *buf, size_t len, const unsigned int *x);
+void opt_show_ulongval_si(char *buf, size_t len, const unsigned long *x);
+void opt_show_ulonglongval_si(char *buf, size_t len, const unsigned long long *x);
 
 
 
@@ -554,7 +551,7 @@ struct opt_table {
        enum opt_type type;
        char *(*cb)(void *arg); /* OPT_NOARG */
        char *(*cb_arg)(const char *optarg, void *arg); /* OPT_HASARG */
-       void (*show)(char buf[OPT_SHOW_LEN], const void *arg);
+       void (*show)(char *buf, size_t len, const void *arg);
        union {
                const void *carg;
                void *arg;
@@ -580,14 +577,14 @@ struct opt_table {
                          char *(*)(const char *, const typeof(*(arg))*), \
                          char *(*)(const char *, const void *),        \
                          (cb)),                                        \
-       typesafe_cb_cast(void (*)(char buf[], const void *),            \
-                        void (*)(char buf[], const typeof(*(arg))*), (show))
+       typesafe_cb_cast(void (*)(char *buf, size_t, const void *), \
+                        void (*)(char *buf, size_t, const typeof(*(arg))*), (show))
 
 /* Non-typesafe register function. */
 void _opt_register(const char *names, enum opt_type type,
                   char *(*cb)(void *arg),
                   char *(*cb_arg)(const char *optarg, void *arg),
-                  void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
+                  void (*show)(char *buf, size_t len, const void *arg),
                   const void *arg, const char *desc);
 
 /* We use this to get typechecking for OPT_SUBTABLE */
index b559c7f747689bae70d34589ee9794f4cf710ac0..ddec6f13e13b66051797ccca148459d88d89f028 100644 (file)
@@ -4,13 +4,13 @@
 #include <ccan/opt/helpers.c>
 #include <ccan/opt/parse.c>
 
-static void show_10(char buf[OPT_SHOW_LEN], const void *arg UNNEEDED)
+static void show_10(char *buf, size_t len, const void *arg UNNEEDED)
 {
        memset(buf, 'X', 10);
        buf[10] = '\0';
 }
 
-static void show_max(char buf[OPT_SHOW_LEN], const void *arg UNNEEDED)
+static void show_max(char *buf, size_t len, const void *arg UNNEEDED)
 {
        memset(buf, 'X', OPT_SHOW_LEN);
 }
index 0a08a85f7aa3930011cba5600186f3a21d5c5f09..9aa41fe8db62c5f384b517a811b9b49b3ca3577d 100644 (file)
@@ -476,26 +476,26 @@ int main(int argc, char *argv[])
                        char buf[OPT_SHOW_LEN+2] = { 0 };
                        buf[OPT_SHOW_LEN] = '!';
                        i = -77;
-                       opt_show_intval_bi(buf, &i);
+                       opt_show_intval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "-77") == 0);
                        i = 0;
-                       opt_show_intval_bi(buf, &i);
+                       opt_show_intval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "0") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 77;
-                       opt_show_intval_bi(buf, &i);
+                       opt_show_intval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "77") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = -1234 * k;
-                       opt_show_intval_bi(buf, &i);
+                       opt_show_intval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "-1234k") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 500 * M;
-                       opt_show_intval_bi(buf, &i);
+                       opt_show_intval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "500M") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 1024 * M;
-                       opt_show_intval_bi(buf, &i);
+                       opt_show_intval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "1G") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                }
@@ -506,27 +506,27 @@ int main(int argc, char *argv[])
                        char buf[OPT_SHOW_LEN+2] = { 0 };
                        buf[OPT_SHOW_LEN] = '!';
                        i = -77;
-                       opt_show_longval_bi(buf, &i);
+                       opt_show_longval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "-77") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 77;
-                       opt_show_longval_bi(buf, &i);
+                       opt_show_longval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "77") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = -1 * k;
-                       opt_show_longval_bi(buf, &i);
+                       opt_show_longval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "-1k") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 500 * M;
-                       opt_show_longval_bi(buf, &i);
+                       opt_show_longval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "500M") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 1024 * M;
-                       opt_show_longval_bi(buf, &i);
+                       opt_show_longval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "1G") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 0;
-                       opt_show_longval_bi(buf, &i);
+                       opt_show_longval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "0") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                }
@@ -537,23 +537,23 @@ int main(int argc, char *argv[])
                        char buf[OPT_SHOW_LEN+2] = { 0 };
                        buf[OPT_SHOW_LEN] = '!';
                        i = -7777;
-                       opt_show_longlongval_bi(buf, &i);
+                       opt_show_longlongval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "-7777") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 7777;
-                       opt_show_longlongval_bi(buf, &i);
+                       opt_show_longlongval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "7777") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = -10240000 * k;
-                       opt_show_longlongval_bi(buf, &i);
+                       opt_show_longlongval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "-10000M") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 5 * P;
-                       opt_show_longlongval_bi(buf, &i);
+                       opt_show_longlongval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "5P") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 1024 * P;
-                       opt_show_longlongval_bi(buf, &i);
+                       opt_show_longlongval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "1E") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                }
@@ -564,19 +564,19 @@ int main(int argc, char *argv[])
                        char buf[OPT_SHOW_LEN+2] = { 0 };
                        buf[OPT_SHOW_LEN] = '!';
                        i = 77;
-                       opt_show_uintval_bi(buf, &i);
+                       opt_show_uintval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "77") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 1234 * k;
-                       opt_show_uintval_bi(buf, &i);
+                       opt_show_uintval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "1234k") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 500 * M;
-                       opt_show_uintval_bi(buf, &i);
+                       opt_show_uintval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "500M") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 1024 * M;
-                       opt_show_uintval_bi(buf, &i);
+                       opt_show_uintval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "1G") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                }
@@ -587,23 +587,23 @@ int main(int argc, char *argv[])
                        char buf[OPT_SHOW_LEN+2] = { 0 };
                        buf[OPT_SHOW_LEN] = '!';
                        i = 77;
-                       opt_show_ulongval_bi(buf, &i);
+                       opt_show_ulongval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "77") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = k;
-                       opt_show_ulongval_bi(buf, &i);
+                       opt_show_ulongval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "1k") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 500 * M;
-                       opt_show_ulongval_bi(buf, &i);
+                       opt_show_ulongval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "500M") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 1024 * M;
-                       opt_show_ulongval_bi(buf, &i);
+                       opt_show_ulongval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "1G") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 0;
-                       opt_show_ulongval_bi(buf, &i);
+                       opt_show_ulongval_bi(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "0") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                }
@@ -614,19 +614,19 @@ int main(int argc, char *argv[])
                        char buf[OPT_SHOW_LEN+2] = { 0 };
                        buf[OPT_SHOW_LEN] = '!';
                        i = 7777;
-                       opt_show_ulonglongval_bi(buf, (unsigned long long *)&i);
+                       opt_show_ulonglongval_bi(buf, OPT_SHOW_LEN, (unsigned long long *)&i);
                        ok1(strcmp(buf, "7777") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 10240000 * k;
-                       opt_show_ulonglongval_bi(buf, (unsigned long long *)&i);
+                       opt_show_ulonglongval_bi(buf, OPT_SHOW_LEN, (unsigned long long *)&i);
                        ok1(strcmp(buf, "10000M") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 5 * P;
-                       opt_show_ulonglongval_bi(buf, (unsigned long long *)&i);
+                       opt_show_ulonglongval_bi(buf, OPT_SHOW_LEN, (unsigned long long *)&i);
                        ok1(strcmp(buf, "5P") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 1024 * P;
-                       opt_show_ulonglongval_bi(buf, (unsigned long long *)&i);
+                       opt_show_ulonglongval_bi(buf, OPT_SHOW_LEN, (unsigned long long *)&i);
                        ok1(strcmp(buf, "1E") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                }
@@ -860,26 +860,26 @@ int main(int argc, char *argv[])
                        char buf[OPT_SHOW_LEN+2] = { 0 };
                        buf[OPT_SHOW_LEN] = '!';
                        i = -77;
-                       opt_show_intval_si(buf, &i);
+                       opt_show_intval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "-77") == 0);
                        i = 0;
-                       opt_show_intval_si(buf, &i);
+                       opt_show_intval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "0") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 77;
-                       opt_show_intval_si(buf, &i);
+                       opt_show_intval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "77") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = -1234 * k;
-                       opt_show_intval_si(buf, &i);
+                       opt_show_intval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "-1234k") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 500 * M;
-                       opt_show_intval_si(buf, &i);
+                       opt_show_intval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "500M") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 1000 * M;
-                       opt_show_intval_si(buf, &i);
+                       opt_show_intval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "1G") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                }
@@ -890,27 +890,27 @@ int main(int argc, char *argv[])
                        char buf[OPT_SHOW_LEN+2] = { 0 };
                        buf[OPT_SHOW_LEN] = '!';
                        i = -77;
-                       opt_show_longval_si(buf, &i);
+                       opt_show_longval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "-77") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 77;
-                       opt_show_longval_si(buf, &i);
+                       opt_show_longval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "77") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = -1 * k;
-                       opt_show_longval_si(buf, &i);
+                       opt_show_longval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "-1k") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 500 * M;
-                       opt_show_longval_si(buf, &i);
+                       opt_show_longval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "500M") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 1000 * M;
-                       opt_show_longval_si(buf, &i);
+                       opt_show_longval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "1G") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 0;
-                       opt_show_longval_si(buf, &i);
+                       opt_show_longval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "0") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                }
@@ -921,23 +921,23 @@ int main(int argc, char *argv[])
                        char buf[OPT_SHOW_LEN+2] = { 0 };
                        buf[OPT_SHOW_LEN] = '!';
                        i = -7777;
-                       opt_show_longlongval_si(buf, &i);
+                       opt_show_longlongval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "-7777") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 7777;
-                       opt_show_longlongval_si(buf, &i);
+                       opt_show_longlongval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "7777") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = -10240000 * k;
-                       opt_show_longlongval_si(buf, &i);
+                       opt_show_longlongval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "-10240M") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 5 * P;
-                       opt_show_longlongval_si(buf, &i);
+                       opt_show_longlongval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "5P") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 2000 * P;
-                       opt_show_longlongval_si(buf, &i);
+                       opt_show_longlongval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "2E") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                }
@@ -948,19 +948,19 @@ int main(int argc, char *argv[])
                        char buf[OPT_SHOW_LEN+2] = { 0 };
                        buf[OPT_SHOW_LEN] = '!';
                        i = 77;
-                       opt_show_uintval_si(buf, &i);
+                       opt_show_uintval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "77") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 1234 * k;
-                       opt_show_uintval_si(buf, &i);
+                       opt_show_uintval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "1234k") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 500 * M;
-                       opt_show_uintval_si(buf, &i);
+                       opt_show_uintval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "500M") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 1000 * M;
-                       opt_show_uintval_si(buf, &i);
+                       opt_show_uintval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "1G") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                }
@@ -971,23 +971,23 @@ int main(int argc, char *argv[])
                        char buf[OPT_SHOW_LEN+2] = { 0 };
                        buf[OPT_SHOW_LEN] = '!';
                        i = 77;
-                       opt_show_ulongval_si(buf, &i);
+                       opt_show_ulongval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "77") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = k;
-                       opt_show_ulongval_si(buf, &i);
+                       opt_show_ulongval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "1k") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 500 * M;
-                       opt_show_ulongval_si(buf, &i);
+                       opt_show_ulongval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "500M") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 1024 * M;
-                       opt_show_ulongval_si(buf, &i);
+                       opt_show_ulongval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "1024M") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 0;
-                       opt_show_ulongval_si(buf, &i);
+                       opt_show_ulongval_si(buf, OPT_SHOW_LEN, &i);
                        ok1(strcmp(buf, "0") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                }
@@ -998,19 +998,19 @@ int main(int argc, char *argv[])
                        char buf[OPT_SHOW_LEN+2] = { 0 };
                        buf[OPT_SHOW_LEN] = '!';
                        i = 7777;
-                       opt_show_ulonglongval_si(buf, (unsigned long long *)&i);
+                       opt_show_ulonglongval_si(buf, OPT_SHOW_LEN, (unsigned long long *)&i);
                        ok1(strcmp(buf, "7777") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 10240000 * k;
-                       opt_show_ulonglongval_si(buf, (unsigned long long *)&i);
+                       opt_show_ulonglongval_si(buf, OPT_SHOW_LEN, (unsigned long long *)&i);
                        ok1(strcmp(buf, "10240M") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 5 * P;
-                       opt_show_ulonglongval_si(buf, (unsigned long long *)&i);
+                       opt_show_ulonglongval_si(buf, OPT_SHOW_LEN, (unsigned long long *)&i);
                        ok1(strcmp(buf, "5P") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                        i = 1000 * P;
-                       opt_show_ulonglongval_si(buf, (unsigned long long *)&i);
+                       opt_show_ulonglongval_si(buf, OPT_SHOW_LEN, (unsigned long long *)&i);
                        ok1(strcmp(buf, "1E") == 0);
                        ok1(buf[OPT_SHOW_LEN] == '!');
                }
@@ -1090,12 +1090,12 @@ int main(int argc, char *argv[])
                buf[OPT_SHOW_LEN] = '!';
 
                b = true;
-               opt_show_bool(buf, &b);
+               opt_show_bool(buf, OPT_SHOW_LEN, &b);
                ok1(strcmp(buf, "true") == 0);
                ok1(buf[OPT_SHOW_LEN] == '!');
 
                b = false;
-               opt_show_bool(buf, &b);
+               opt_show_bool(buf, OPT_SHOW_LEN, &b);
                ok1(strcmp(buf, "false") == 0);
                ok1(buf[OPT_SHOW_LEN] == '!');
        }
@@ -1107,12 +1107,12 @@ int main(int argc, char *argv[])
                buf[OPT_SHOW_LEN] = '!';
 
                b = true;
-               opt_show_invbool(buf, &b);
+               opt_show_invbool(buf, OPT_SHOW_LEN, &b);
                ok1(strcmp(buf, "false") == 0);
                ok1(buf[OPT_SHOW_LEN] == '!');
 
                b = false;
-               opt_show_invbool(buf, &b);
+               opt_show_invbool(buf, OPT_SHOW_LEN, &b);
                ok1(strcmp(buf, "true") == 0);
                ok1(buf[OPT_SHOW_LEN] == '!');
        }
@@ -1126,14 +1126,14 @@ int main(int argc, char *argv[])
                /* Short test. */
                p = str;
                strcpy(p, "short");
-               opt_show_charp(buf, &p);
+               opt_show_charp(buf, OPT_SHOW_LEN, &p);
                ok1(strcmp(buf, "\"short\"") == 0);
                ok1(buf[OPT_SHOW_LEN] == '!');
 
                /* Truncate test. */
                memset(p, 'x', OPT_SHOW_LEN*2);
                p[OPT_SHOW_LEN*2-1] = '\0';
-               opt_show_charp(buf, &p);
+               opt_show_charp(buf, OPT_SHOW_LEN, &p);
                ok1(buf[0] == '"');
                ok1(buf[OPT_SHOW_LEN-1] == '"');
                ok1(buf[OPT_SHOW_LEN] == '!');
@@ -1147,12 +1147,12 @@ int main(int argc, char *argv[])
                buf[OPT_SHOW_LEN] = '!';
 
                i = -77;
-               opt_show_intval(buf, &i);
+               opt_show_intval(buf, OPT_SHOW_LEN, &i);
                ok1(strcmp(buf, "-77") == 0);
                ok1(buf[OPT_SHOW_LEN] == '!');
 
                i = 77;
-               opt_show_intval(buf, &i);
+               opt_show_intval(buf, OPT_SHOW_LEN, &i);
                ok1(strcmp(buf, "77") == 0);
                ok1(buf[OPT_SHOW_LEN] == '!');
        }
@@ -1164,7 +1164,7 @@ int main(int argc, char *argv[])
                buf[OPT_SHOW_LEN] = '!';
 
                ui = 4294967295U;
-               opt_show_uintval(buf, &ui);
+               opt_show_uintval(buf, OPT_SHOW_LEN, &ui);
                ok1(strcmp(buf, "4294967295") == 0);
                ok1(buf[OPT_SHOW_LEN] == '!');
        }
@@ -1176,7 +1176,7 @@ int main(int argc, char *argv[])
                buf[OPT_SHOW_LEN] = '!';
 
                l = 1234567890L;
-               opt_show_longval(buf, &l);
+               opt_show_longval(buf, OPT_SHOW_LEN, &l);
                ok1(strcmp(buf, "1234567890") == 0);
                ok1(buf[OPT_SHOW_LEN] == '!');
        }
@@ -1188,7 +1188,7 @@ int main(int argc, char *argv[])
                buf[OPT_SHOW_LEN] = '!';
 
                ul = 4294967295UL;
-               opt_show_ulongval(buf, &ul);
+               opt_show_ulongval(buf, OPT_SHOW_LEN, &ul);
                ok1(strcmp(buf, "4294967295") == 0);
                ok1(buf[OPT_SHOW_LEN] == '!');
        }
@@ -1200,12 +1200,12 @@ int main(int argc, char *argv[])
                buf[OPT_SHOW_LEN] = '!';
 
                f = -77.5;
-               opt_show_floatval(buf, &f);
+               opt_show_floatval(buf, OPT_SHOW_LEN, &f);
                ok1(strcmp(buf, "-77.500000") == 0);
                ok1(buf[OPT_SHOW_LEN] == '!');
 
                f = 77.5;
-               opt_show_floatval(buf, &f);
+               opt_show_floatval(buf, OPT_SHOW_LEN, &f);
                ok1(strcmp(buf, "77.500000") == 0);
                ok1(buf[OPT_SHOW_LEN] == '!');
        }
@@ -1217,12 +1217,12 @@ int main(int argc, char *argv[])
                buf[OPT_SHOW_LEN] = '!';
 
                d = -77;
-               opt_show_doubleval(buf, &d);
+               opt_show_doubleval(buf, OPT_SHOW_LEN, &d);
                ok1(strcmp(buf, "-77.000000") == 0);
                ok1(buf[OPT_SHOW_LEN] == '!');
 
                d = 77;
-               opt_show_doubleval(buf, &d);
+               opt_show_doubleval(buf, OPT_SHOW_LEN, &d);
                ok1(strcmp(buf, "77.000000") == 0);
                ok1(buf[OPT_SHOW_LEN] == '!');
        }
index 2ff04884ebdc0efe6dc4d9dd8fe4ae6af4b9175b..700748be0c2a0127eff8a773bfea191f6ace2500 100644 (file)
@@ -21,9 +21,9 @@ char *test_arg(const char *optarg, const char *arg)
        return NULL;
 }
 
-void show_arg(char buf[OPT_SHOW_LEN], const char *arg)
+void show_arg(char *buf, size_t len, const char *arg)
 {
-       strncpy(buf, arg, OPT_SHOW_LEN);
+       strncpy(buf, arg, len);
 }
 
 char *err_output = NULL;
index 12cf0b753e99be441ead57f73b1dc1c3e2d79999..64641ec4f1d354e00e7fd77bda4699957ebc322a 100644 (file)
@@ -13,7 +13,7 @@ void reset_options(void);
 extern unsigned int test_cb_called;
 char *test_noarg(void *arg);
 char *test_arg(const char *optarg, const char *arg);
-void show_arg(char buf[OPT_SHOW_LEN], const char *arg);
+void show_arg(char *buf, size_t len, const char *arg);
 
 extern struct opt_table short_table[];
 extern struct opt_table long_table[];
index 4ed279197fe808ac391d39a93df1dbb9e8b14126..e96298382c3d018f16b55281115ceace40164939 100644 (file)
@@ -20,6 +20,9 @@ const char opt_hidden[1];
 #define MIN_DESC_WIDTH 40
 #define MIN_TOTAL_WIDTH 50
 
+/* Maximum length of arg to show in opt_usage */
+#define OPT_SHOW_LEN 80
+
 static unsigned int get_columns(void)
 {
        int ws_col = 0;
@@ -148,7 +151,7 @@ static char *add_desc(char *base, size_t *len, size_t *max,
        if (opt->show) {
                char buf[OPT_SHOW_LEN + sizeof("...")];
                strcpy(buf + OPT_SHOW_LEN, "...");
-               opt->show(buf, opt->u.arg);
+               opt->show(buf, OPT_SHOW_LEN, opt->u.arg);
 
                /* If it doesn't fit on this line, indent. */
                if (off + strlen(" (default: ") + strlen(buf) + strlen(")")