From: Rob Clark Date: Fri, 9 Nov 2018 18:28:36 +0000 (-0500) Subject: util: env_var_as_unsigned() helper X-Git-Url: https://git.libre-soc.org/?p=mesa.git;a=commitdiff_plain;h=a321f939f648e25d7730507d7e60bb17c34d6e6e util: env_var_as_unsigned() helper So I can drop env2u() helper from freedreno_util.h and get rid of one small ir3 dependency on gallium/freedreno Signed-off-by: Rob Clark --- diff --git a/src/util/debug.c b/src/util/debug.c index 98b1853325d..2773b55cc4d 100644 --- a/src/util/debug.c +++ b/src/util/debug.c @@ -21,6 +21,7 @@ * IN THE SOFTWARE. */ +#include #include #include "main/macros.h" #include "debug.h" @@ -76,3 +77,22 @@ env_var_as_boolean(const char *var_name, bool default_value) return default_value; } } + +/** + * Reads an environment variable and interprets its value as a unsigned. + */ +unsigned +env_var_as_unsigned(const char *var_name, unsigned default_value) +{ + char *str = getenv(var_name); + if (str) { + char *end; + unsigned long result; + + errno = 0; + result = strtoul(str, &end, 0); + if (errno == 0 && end != str && *end == '\0') + return result; + } + return default_value; +} diff --git a/src/util/debug.h b/src/util/debug.h index 75ebc2ebffb..2e34ebe3421 100644 --- a/src/util/debug.h +++ b/src/util/debug.h @@ -41,6 +41,8 @@ parse_debug_string(const char *debug, const struct debug_control *control); bool env_var_as_boolean(const char *var_name, bool default_value); +unsigned +env_var_as_unsigned(const char *var_name, unsigned default_value); #ifdef __cplusplus } /* extern C */