mirror of
https://github.com/fish-shell/fish-shell.git
synced 2025-03-25 04:22:04 +08:00
Make sure that the PWD and HOME variables are always in canonical form
darcs-hash:20070510191128-ac50b-dd51a75617d62e4f403094ddc7527a82c5de3103.gz
This commit is contained in:
parent
03f322c715
commit
e4f5bc69e2
16
env.c
16
env.c
@ -52,6 +52,9 @@
|
||||
#include "env_universal.h"
|
||||
#include "input_common.h"
|
||||
#include "event.h"
|
||||
#include "path.h"
|
||||
#include "halloc.h"
|
||||
#include "halloc_util.h"
|
||||
|
||||
#include "complete.h"
|
||||
|
||||
@ -710,6 +713,19 @@ int env_set( const wchar_t *key,
|
||||
|
||||
CHECK( key, ENV_INVALID );
|
||||
|
||||
if( val && CONTAINS( key, L"PWD", L"HOME" ) )
|
||||
{
|
||||
void *context = halloc( 0, 0 );
|
||||
const wchar_t *val_canonical = path_make_canonical( context, val );
|
||||
if( wcscmp( val_canonical, val ) )
|
||||
{
|
||||
int res = env_set( key, val_canonical, var_mode );
|
||||
halloc_free( context );
|
||||
return res;
|
||||
}
|
||||
halloc_free( context );
|
||||
}
|
||||
|
||||
if( (var_mode & ENV_USER ) &&
|
||||
hash_get( &env_read_only, key ) )
|
||||
{
|
||||
|
26
fish_tests.c
26
fish_tests.c
@ -44,6 +44,8 @@
|
||||
#include "output.h"
|
||||
#include "exec.h"
|
||||
#include "event.h"
|
||||
#include "path.h"
|
||||
#include "halloc.h"
|
||||
#include "halloc_util.h"
|
||||
|
||||
/**
|
||||
@ -600,6 +602,26 @@ static void test_expand()
|
||||
|
||||
}
|
||||
|
||||
static void test_path()
|
||||
{
|
||||
say( L"Testing path functions" );
|
||||
|
||||
void *context = halloc( 0, 0 );
|
||||
|
||||
|
||||
wchar_t *can = path_make_canonical( context, L"//foo//////bar/" );
|
||||
|
||||
if( wcscmp( can, L"/foo/bar" ) )
|
||||
{
|
||||
err( L"Bug in canonical PATH code" );
|
||||
}
|
||||
|
||||
halloc_free( context );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Test speed of completion calculations
|
||||
*/
|
||||
@ -673,6 +695,9 @@ void perf_complete()
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Main test
|
||||
*/
|
||||
@ -697,6 +722,7 @@ int main( int argc, char **argv )
|
||||
test_tok();
|
||||
test_parser();
|
||||
test_expand();
|
||||
test_path();
|
||||
|
||||
say( L"Encountered %d errors in low-level tests", err_count );
|
||||
|
||||
|
35
path.c
35
path.c
@ -266,3 +266,38 @@ wchar_t *path_get_config( void *context)
|
||||
|
||||
}
|
||||
|
||||
wchar_t *path_make_canonical( void *context, const wchar_t *path )
|
||||
{
|
||||
wchar_t *res = halloc_wcsdup( context, path );
|
||||
wchar_t *in, *out;
|
||||
|
||||
in = out = res;
|
||||
|
||||
while( *in )
|
||||
{
|
||||
if( *in == L'/' )
|
||||
{
|
||||
while( *(in+1) == L'/' )
|
||||
{
|
||||
in++;
|
||||
}
|
||||
}
|
||||
*out = *in;
|
||||
|
||||
out++;
|
||||
in++;
|
||||
}
|
||||
|
||||
while( 1 )
|
||||
{
|
||||
if( out == res )
|
||||
break;
|
||||
if( *(out-1) != L'/' )
|
||||
break;
|
||||
out--;
|
||||
}
|
||||
*out = 0;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user