Branch: Tag:

2013-06-16

2013-06-16 10:53:54 by Henrik Grubbström (Grubba) <grubba@grubba.org>

MasterObject: Implement getenv() with variants.

1280:   }   #endif    - //! @decl string getenv (string varname, void|int force_update) +    //! @decl mapping(string:string) getenv (void|int force_update)   //! - //! Queries the environment variables. The first variant returns the - //! value of a specific variable or zero if it doesn't exist in the - //! environment. The second variant returns the whole environment as a - //! mapping. Destructive operations on the mapping will not affect the - //! internal environment representation. + //! Queries the environment variables.   //! - //! A cached copy of the real environment is kept to make this - //! function quicker. If the optional flag @[force_update] is nonzero - //! then the real environment is queried and the cache is updated from - //! it. That can be necessary if the environment changes through other - //! means than @[putenv], typically from a C-level library. + //! @param force_update + //! A cached copy of the real environment is kept to make this + //! function quicker. If the optional flag @[force_update] is nonzero + //! then the real environment is queried and the cache is updated from + //! it. That can be necessary if the environment changes through other + //! means than @[putenv], typically from a C-level library.   //! -  + //! @returns + //! Returns the whole environment as a mapping. Destructive + //! operations on the mapping will not affect the internal + //! environment representation. + //!   //! Variable names and values cannot be wide strings nor contain   //! @expr{'\0'@} characters. Variable names also cannot contain   //! @expr{'='@} characters.
1304:   //!   //! @seealso   //! @[putenv()] - string|mapping(string:string) getenv (void|int|string varname, -  void|int force_update) + mapping(string:string) getenv (void|int force_update)   { -  // Variants doesn't seem to work well yet. -  if (stringp (varname)) { -  if (!environment || force_update) { - #ifdef __NT__ -  set_lc_env (Builtin._getenv()); - #else -  environment = Builtin._getenv(); - #endif -  // Kill the compat environment if forced. -  compat_environment = compat_environment_copy = 0; -  } -  - #ifdef __NT__ -  varname = lower_case(varname); - #endif -  -  if (compat_environment) { -  array(string) res; -  if (!equal(res = compat_environment[varname], -  compat_environment_copy[varname])) { -  // Something has messed with the compat environment mapping. -  putenv(varname, res && res[1]); -  } -  } -  -  return environment[varname]; -  } -  -  else { -  force_update = varname; -  +     mapping(string:string) res;       if (force_update) {
1354:    else {    if (compat_environment &&    !equal(compat_environment, compat_environment_copy)) { -  foreach(compat_environment; varname; array(string) pair) { +  foreach(compat_environment; string varname; array(string) pair) {    if (!equal(pair, compat_environment_copy[varname])) {    putenv(pair[0], pair[1]);    }    } -  foreach(compat_environment_copy; varname; array(string) pair) { +  foreach(compat_environment_copy; string varname; array(string) pair) {    if (!compat_environment[varname]) {    putenv(pair[0]);    }
1378:       return res;    } +  + //! @decl string getenv (string varname, void|int force_update) + //! + //! Query the value of a specific environment variable. + //! + //! @param varname + //! Environment variable to query. + //! + //! @param force_update + //! A cached copy of the real environment is kept to make this + //! function quicker. If the optional flag @[force_update] is nonzero + //! then the real environment is queried and the cache is updated from + //! it. That can be necessary if the environment changes through other + //! means than @[putenv], typically from a C-level library. + //! + //! @returns + //! Returns the value of the environment variable @[varname] + //! if it exists, and @expr{0@} (zero) otherwise. + //! + //! Variable names and values cannot be wide strings nor contain + //! @expr{'\0'@} characters. Variable names also cannot contain + //! @expr{'='@} characters. + //! + //! @note + //! On NT the environment variable name is case insensitive. + //! + //! @seealso + //! @[putenv()] + variant string getenv (string varname, void|int force_update) + { +  if (!environment || force_update) { + #ifdef __NT__ +  set_lc_env (Builtin._getenv()); + #else +  environment = Builtin._getenv(); + #endif +  // Kill the compat environment if forced. +  compat_environment = compat_environment_copy = 0;    }    -  + #ifdef __NT__ +  varname = lower_case(varname); + #endif +  +  if (compat_environment) { +  array(string) res; +  if (!equal(res = compat_environment[varname], +  compat_environment_copy[varname])) { +  // Something has messed with the compat environment mapping. +  putenv(varname, res && res[1]); +  } +  } +  +  return environment[varname]; + } +    void putenv (string varname, void|string value)   //! Sets the environment variable @[varname] to @[value].   //!