X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fopt%2Fhelpers.c;h=e657ebff8727090c676e7e594520855fa208de89;hp=7554c997b252fcf7ceda1c5102a871b08a8fcb04;hb=e34192d580958aaffff3754a4e2bf1eccbb489f8;hpb=d7d5abe98caeec82d784ce525e0444ff438acd46 diff --git a/ccan/opt/helpers.c b/ccan/opt/helpers.c index 7554c997..e657ebff 100644 --- a/ccan/opt/helpers.c +++ b/ccan/opt/helpers.c @@ -5,6 +5,9 @@ #include #include "private.h" +/* Upper bound to sprintf this simple type? Each 3 bits < 1 digit. */ +#define CHAR_SIZE(type) (((sizeof(type)*CHAR_BIT + 2) / 3) + 1) + /* FIXME: asprintf module? */ static char *arg_bad(const char *fmt, const char *arg) { @@ -88,10 +91,8 @@ char *opt_set_longval(const char *arg, long *l) *l = strtol(arg, &endp, 0); if (*endp || !arg[0]) return arg_bad("'%s' is not a number", arg); - if (errno == ERANGE) - return arg_bad("'%s' is out of range", arg); if (errno) - return opt_invalid_argument(arg); + return arg_bad("'%s' is out of range", arg); return NULL; } @@ -116,7 +117,7 @@ char *opt_inc_intval(int *i) } /* Display version string. */ -char *opt_show_version_and_exit(const char *version) +char *opt_version_and_exit(const char *version) { printf("%s\n", version); exit(0); @@ -127,3 +128,46 @@ char *opt_usage_and_exit(const char *extra) printf("%s", opt_usage(opt_argv0, extra)); exit(0); } + +void opt_show_bool(char buf[OPT_SHOW_LEN], const bool *b) +{ + strncpy(buf, *b ? "true" : "false", OPT_SHOW_LEN); +} + +void opt_show_invbool(char buf[OPT_SHOW_LEN], const bool *b) +{ + strncpy(buf, *b ? "false" : "true", OPT_SHOW_LEN); +} + +void opt_show_charp(char buf[OPT_SHOW_LEN], char *const *p) +{ + size_t len = strlen(*p); + 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'; +} + +/* Set an integer value, various forms. Sets to 1 on arg == NULL. */ +void opt_show_intval(char buf[OPT_SHOW_LEN], const int *i) +{ + snprintf(buf, OPT_SHOW_LEN, "%i", *i); +} + +void opt_show_uintval(char buf[OPT_SHOW_LEN], const unsigned int *ui) +{ + snprintf(buf, OPT_SHOW_LEN, "%u", *ui); +} + +void opt_show_longval(char buf[OPT_SHOW_LEN], const long *l) +{ + snprintf(buf, OPT_SHOW_LEN, "%li", *l); +} + +void opt_show_ulongval(char buf[OPT_SHOW_LEN], const unsigned long *ul) +{ + snprintf(buf, OPT_SHOW_LEN, "%lu", *ul); +}