diff options
author | ridiculousfish <corydoras@ridiculousfish.com> | 2012-05-09 03:23:31 -0700 |
---|---|---|
committer | ridiculousfish <corydoras@ridiculousfish.com> | 2012-05-09 03:23:31 -0700 |
commit | eca2100312bbe628d11bfb3b0454913653130baf (patch) | |
tree | 72e3f7a129a27179002d0d5382bf031cfc2a9b7a /env.cpp | |
parent | 175249b4555afd7e453d5bd5a054bb79f3577fe1 (diff) |
Cleanup SHLVL variable
Diffstat (limited to 'env.cpp')
-rw-r--r-- | env.cpp | 35 |
1 files changed, 8 insertions, 27 deletions
@@ -637,35 +637,16 @@ void env_init() Set up SHLVL variable */ const env_var_t shlvl_str = env_get_string( L"SHLVL" ); - const wchar_t *shlvl = shlvl_str.missing() ? NULL : shlvl_str.c_str(); - - if ( shlvl ) - { - wchar_t *nshlvl, **end_nshlvl; - /* add an extra space for digit dump (9+1=10) */ - size_t i = wcslen( shlvl ) + 2 * sizeof(wchar_t); - - nshlvl = (wchar_t *)malloc(i); - end_nshlvl = (wchar_t **)calloc( 1, sizeof(nshlvl) ); - if ( !nshlvl || !end_nshlvl ) - DIE_MEM(); - - if ( nshlvl && swprintf( nshlvl, i, - L"%ld", wcstoul( shlvl, end_nshlvl, 10 )+1 ) != -1 ) - { - env_set( L"SHLVL", - nshlvl, - ENV_GLOBAL | ENV_EXPORT ); - } - free( end_nshlvl ); - free( nshlvl ); - } - else + wcstring nshlvl_str = L"1"; + if (! shlvl_str.missing()) { - env_set( L"SHLVL", - L"1", - ENV_GLOBAL | ENV_EXPORT ); + long shlvl_i = wcstol(shlvl_str.c_str(), NULL, 10); + if (shlvl_i >= 0) + { + nshlvl_str = format_string(L"%ld", 1 + shlvl_i); + } } + env_set(L"SHLVL", nshlvl_str.c_str(), ENV_GLOBAL | ENV_EXPORT ); /* Set correct defaults for e.g. USER and HOME variables */ env_set_defaults(); |