X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Futil.c;fp=src%2Futil.c;h=32c3c57e15d5bd2f206bab5a1984a00e19dff06a;hb=5f9a5e8d7d0eb3123fd7068452090b72b9096bea;hp=06fbea2a7ef0856aa6a2ac0d71c07bce5ea85dd5;hpb=9bc504ebdb92e56e1a34047f8ab7c70f6b67eb90;p=i3%2Fi3 diff --git a/src/util.c b/src/util.c index 06fbea2a..32c3c57e 100644 --- a/src/util.c +++ b/src/util.c @@ -458,3 +458,20 @@ void kill_nagbar(pid_t *nagbar_pid, bool wait_for_it) { * waitpid() here. */ waitpid(*nagbar_pid, NULL, 0); } + +/* + * Converts a string into a long using strtol(). + * This is a convenience wrapper checking the parsing result. It returns true + * if the number could be parsed. + */ +bool parse_long(const char *str, long *out, int base) { + char *end; + long result = strtol(str, &end, base); + if (result == LONG_MIN || result == LONG_MAX || result < 0 || (end != NULL && *end != '\0')) { + *out = result; + return false; + } + + *out = result; + return true; +}