From 1272be04be0cb803eec87f602edb2e3e6f111aea Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 21:33:34 +0200 Subject: Merging upstream version 2.40. Signed-off-by: Daniel Baumann --- lib/strutils.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 8 deletions(-) (limited to 'lib/strutils.c') 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 [suffix]\n" " %1$s --cmp-paths \n" -- cgit v1.2.3