diff options
Diffstat (limited to 'lib/strutils.c')
-rw-r--r-- | lib/strutils.c | 60 |
1 files changed, 52 insertions, 8 deletions
diff --git a/lib/strutils.c b/lib/strutils.c index 6229a6e..9ea5da7 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -456,21 +456,28 @@ err: errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); } -long double strtold_or_err(const char *str, const char *errmesg) +int ul_strtold(const char *str, long double *num) { - double num; char *end = NULL; errno = 0; if (str == NULL || *str == '\0') - goto err; - num = strtold(str, &end); + return -(errno = EINVAL); + *num = strtold(str, &end); - if (errno || str == end || (end && *end)) - goto err; + if (errno != 0) + return -errno; + if (str == end || (end && *end)) + return -(errno = EINVAL); + return 0; +} - return num; -err: +long double strtold_or_err(const char *str, const char *errmesg) +{ + long double num = 0; + + if (ul_strtold(str, &num) == 0) + return num; if (errno == ERANGE) err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); @@ -1010,6 +1017,34 @@ int strappend(char **a, const char *b) return 0; } +/* the hybrid version of strfconcat and strappend. */ +int strfappend(char **a, const char *format, ...) +{ + va_list ap; + int res; + + va_start(ap, format); + res = strvfappend(a, format, ap); + va_end(ap); + + return res; +} + +extern int strvfappend(char **a, const char *format, va_list ap) +{ + char *val; + int sz; + int res; + + sz = vasprintf(&val, format, ap); + if (sz < 0) + return -errno; + + res = strappend(a, val); + free(val); + return res; +} + static size_t strcspn_escaped(const char *s, const char *reject) { int escaped = 0; @@ -1386,6 +1421,15 @@ int main(int argc, char *argv[]) printf("\"%s\" --> \"%s\"\n", argv[2], ul_strchr_escaped(argv[2], *argv[3])); return EXIT_SUCCESS; + } else if (argc == 2 && strcmp(argv[1], "--next-string") == 0) { + char *buf = "abc\0Y\0\0xyz\0X"; + char *end = buf + 12; + char *p = buf; + + do { + printf("str: '%s'\n", p); + } while ((p = ul_next_string(p, end))); + } else { fprintf(stderr, "usage: %1$s --size <number>[suffix]\n" " %1$s --cmp-paths <path> <path>\n" |