diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2024-06-04 12:51:12 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2024-06-04 12:51:12 -0400 |
commit | 3da7fe8b4f5d6a41152d81bbfbd30b0ab3a9da1c (patch) | |
tree | e675798b2918c477cbbf69707a1d2f32120cd3df /src/bfstd.c | |
parent | 32d252598e855829ebdc657e635f93270600b5a2 (diff) | |
download | bfs-3da7fe8b4f5d6a41152d81bbfbd30b0ab3a9da1c.tar.xz |
bfstd: New xstrtoll() wrapper
Diffstat (limited to 'src/bfstd.c')
-rw-r--r-- | src/bfstd.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/bfstd.c b/src/bfstd.c index 44eda7c..6d244ca 100644 --- a/src/bfstd.c +++ b/src/bfstd.c @@ -209,6 +209,35 @@ const char *xgetprogname(void) { return cmd; } +int xstrtoll(const char *str, char **end, int base, long long *value) { + // strtoll() skips leading spaces, but we want to reject them + if (xisspace(str[0])) { + errno = EINVAL; + return -1; + } + + // If end is NULL, make sure the entire string is valid + bool entire = !end; + char *endp; + if (!end) { + end = &endp; + } + + errno = 0; + long long result = strtoll(str, end, base); + if (errno != 0) { + return -1; + } + + if (*end == str || (entire && **end != '\0')) { + errno = EINVAL; + return -1; + } + + *value = result; + return 0; +} + /** Compile and execute a regular expression for xrpmatch(). */ static int xrpregex(nl_item item, const char *response) { const char *pattern = nl_langinfo(item); |