diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2013-09-23 12:49:04 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2013-09-30 10:28:52 +0200 |
commit | 5ee4ad2229b676ff521a1da9b8b32f474450f56c (patch) | |
tree | 2d5526505b47363b78c1b4fb301d9ef08ebc8067 /include/environment.h | |
parent | 28141f9e5a465571cf79505b2fd4b9ae207b9ce3 (diff) | |
download | barebox-5ee4ad2229b676ff521a1da9b8b32f474450f56c.tar.gz barebox-5ee4ad2229b676ff521a1da9b8b32f474450f56c.tar.xz |
environment variables: introduce new helpers
This introduces some new environment variable helpers and updates
the existing ones. Newly introduced are:
getenv_bool: read a bool variable
getenv_ul: read an unsigned long variable
getenev_uint: read an unsigned int variable
getenv_nonempty: like normal getenv, but does return NULL instead of an
empty string
All new helpers take a pointer to the value. This value is only modified
when the variable exists. This allows the following programming scheme:
unsigned int myvalue = sanedefault;
getenv_uint("myvalue", &myvalue);
So without checking the return value myvalue contains the best possible
value.
getenv_ull is updated to this scheme.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'include/environment.h')
-rw-r--r-- | include/environment.h | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/include/environment.h b/include/environment.h index ae1ecf5bd6..a08f597000 100644 --- a/include/environment.h +++ b/include/environment.h @@ -45,7 +45,11 @@ char *var_name(struct variable_d *); const char *getenv(const char *); int setenv(const char *, const char *); void export_env_ull(const char *name, unsigned long long val); -unsigned long long getenv_ull(const char *name); +int getenv_ull(const char *name, unsigned long long *val); +int getenv_ul(const char *name, unsigned long *val); +int getenv_uint(const char *name, unsigned int *val); +int getenv_bool(const char *var, int *val); +const char *getenv_nonempty(const char *var); #else static inline char *getenv(const char *var) { @@ -56,10 +60,22 @@ static inline int setenv(const char *var, const char *val) { return 0; } + static inline void export_env_ull(const char *name, unsigned long long val) {} -static inline unsigned long long getenv_ull(const char *name) + +static inline int getenv_ull(const char *name, unsigned long long *val) { - return 0; + return -EINVAL; +} + +static inline int getenv_ul(const char *name, unsigned long *val) +{ + return -EINVAL; +} + +static inline int getenv_uint(const char *name, unsigned int *val) +{ + return -EINVAL; } static inline int export(const char *var) @@ -67,6 +83,15 @@ static inline int export(const char *var) return -EINVAL; } +static inline int getenv_bool(const char *var, int *val) +{ + return -EINVAL; +} + +static inline const char *getenv_nonempty(const char *var) +{ + return NULL; +} #endif int env_pop_context(void); |