Index: share/man/man9/Makefile =================================================================== --- share/man/man9/Makefile +++ share/man/man9/Makefile @@ -1092,6 +1092,8 @@ getenv.9 getenv_quad.9 \ getenv.9 getenv_uint.9 \ getenv.9 getenv_ulong.9 \ + getenv.9 getenv_is_true.9 \ + getenv.9 getenv_is_false.9 \ getenv.9 kern_getenv.9 \ getenv.9 kern_setenv.9 \ getenv.9 kern_unsetenv.9 \ Index: share/man/man9/getenv.9 =================================================================== --- share/man/man9/getenv.9 +++ share/man/man9/getenv.9 @@ -27,7 +27,7 @@ .\" .\" $FreeBSD$ .\" -.Dd June 1, 2017 +.Dd August 31, 2020 .Dt GETENV 9 .Os .Sh NAME @@ -39,6 +39,8 @@ .Nm getenv_quad , .Nm getenv_uint , .Nm getenv_ulong , +.Nm getenv_is_true , +.Nm getenv_is_false , .Nm kern_setenv , .Nm testenv , .Nm kern_unsetenv @@ -62,6 +64,10 @@ .Fn getenv_uint "const char *name" "unsigned int *data" .Ft int .Fn getenv_ulong "const char *name" "unsigned long *data" +.Ft bool +.Fn getenv_is_true "const char *name" +.Ft bool +.Fn getenv_is_false "const char *name" .Ft int .Fn kern_setenv "const char *name" "const char *value" .Ft int @@ -194,6 +200,23 @@ characters of its value are copied to the buffer pointed to by .Fa data followed by a null character and a non-zero value is returned. +.Pp +The +.Fn getenv_is_true +and +.Fn getenv_is_false +functions are wrappers around +.Fn kern_getenv +that simplify the parsing of boolean values. +The +.Fn getenv_is_true +function performs a case-insensitive comparison of the value of the kernel +environment variable +.Fa name +to the strings "1" and "true". +The +.Fn getenv_is_false +function works similarly for the values "0" and "false". .Sh RETURN VALUES The .Fn kern_getenv @@ -211,6 +234,7 @@ .Fn testenv function returns zero if the specified environment variable does not exist and a non-zero value if it does exist. +.Pp The .Fn getenv_int , .Fn getenv_long , @@ -220,3 +244,14 @@ and .Fn getenv_ulong functions return a non-zero value on success and zero on failure. +.Pp +The +.Fn getenv_is_true +and +.Fn getenv_is_false +functions return +.Dv true +if the specified environment variable exists and its value matches the desired +boolean condition, and +.Dv false +otherwise. Index: sys/kern/kern_environment.c =================================================================== --- sys/kern/kern_environment.c +++ sys/kern/kern_environment.c @@ -943,6 +943,46 @@ return (1); } +bool +getenv_is_true(const char *name) +{ + char *val; + bool ret; + + if (name == NULL) + return (false); + + ret = false; + val = kern_getenv(name); + if ((val != NULL) && + ((strcmp(val, "1") == 0) || (strcasecmp(val, "true") == 0))) { + ret = true; + } + freeenv(val); + + return (ret); +} + +bool +getenv_is_false(const char *name) +{ + char *val; + bool ret; + + if (name == NULL) + return (false); + + ret = false; + val = kern_getenv(name); + if ((val != NULL) && + ((strcmp(val, "0") == 0) || (strcasecmp(val, "false") == 0))) { + ret = true; + } + freeenv(val); + + return (ret); +} + /* * Find the next entry after the one which (cp) falls within, return a * pointer to its start or NULL if there are no more. Index: sys/sys/systm.h =================================================================== --- sys/sys/systm.h +++ sys/sys/systm.h @@ -463,6 +463,8 @@ int getenv_int64(const char *name, int64_t *data); int getenv_uint64(const char *name, uint64_t *data); int getenv_quad(const char *name, quad_t *data); +bool getenv_is_true(const char *name); +bool getenv_is_false(const char *name); int kern_setenv(const char *name, const char *value); int kern_unsetenv(const char *name); int testenv(const char *name);