]> git.ozlabs.org Git - ccan/blobdiff - ccan/opt/helpers.c
tdb2: fix msync() arg
[ccan] / ccan / opt / helpers.c
index 7554c997b252fcf7ceda1c5102a871b08a8fcb04..e657ebff8727090c676e7e594520855fa208de89 100644 (file)
@@ -5,6 +5,9 @@
 #include <stdio.h>
 #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);
+}