diff options
author | Karel Zak | 2011-10-03 15:40:15 +0200 |
---|---|---|
committer | Karel Zak | 2011-10-03 15:40:15 +0200 |
commit | ffbe3270f10e1e808b03ec4b1844ea62b945652d (patch) | |
tree | dc63e6208b6de48d74a4e08f3cad7f759a9bc843 /lib | |
parent | lsblk: add udev support (diff) | |
parent | cytune: fix printf type warning (diff) | |
download | kernel-qcow2-util-linux-ffbe3270f10e1e808b03ec4b1844ea62b945652d.tar.gz kernel-qcow2-util-linux-ffbe3270f10e1e808b03ec4b1844ea62b945652d.tar.xz kernel-qcow2-util-linux-ffbe3270f10e1e808b03ec4b1844ea62b945652d.zip |
Merge branch 'sys-utils-again' of https://github.com/kerolasa/lelux-utiliteetit
* 'sys-utils-again' of https://github.com/kerolasa/lelux-utiliteetit:
cytune: fix printf type warning
docs: mention long options in cytune.8
cytune: coding style fixes
cytune: refactor main(), new function query_tty_stats()
cytune: use libc error printing facilities
cytune: check numeric user inputs
cytune: add long options and usage()
docs: restructure flock.1 manual page
flock: simplify strtotimeval()
lib: [strutils] add strtod_or_err() function
flock: use strutils.h to check numeric user input
flock: use sysexit.h for all exit values
flock: use function attributes
flock: align with howto-usage-function.txt
flock: use libc error printing facilities
flock: move long_options struct to function scope
flock: fix coding style
docs: align fallocate.1 with howto-man-page.txt
Diffstat (limited to 'lib')
-rw-r--r-- | lib/strutils.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/strutils.c b/lib/strutils.c index da395e447..aad9f7738 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -168,6 +168,30 @@ char *strndup(const char *s, size_t n) #endif /* + * same as strtod(3) but exit on failure instead of returning crap + */ +double strtod_or_err(const char *str, const char *errmesg) +{ + double num; + char *end = NULL; + + if (str == NULL || *str == '\0') + goto err; + errno = 0; + num = strtod(str, &end); + + if (errno || str == end || (end && *end)) + goto err; + + return num; + err: + if (errno) + err(EXIT_FAILURE, "%s: '%s'", errmesg, str); + else + errx(EXIT_FAILURE, "%s: '%s'", errmesg, str); + return 0; +} +/* * same as strtol(3) but exit on failure instead of returning crap */ long strtol_or_err(const char *str, const char *errmesg) |