diff --git a/v2/engine/build.bat b/v2/engine/build.bat index 2f4984bb6..36e818a53 100644 --- a/v2/engine/build.bat +++ b/v2/engine/build.bat @@ -470,8 +470,8 @@ set BJAM_SOURCES=%BJAM_SOURCES% jambase.c jamgram.c lists.c make.c make1.c set BJAM_SOURCES=%BJAM_SOURCES% object.c option.c output.c parse.c pathnt.c set BJAM_SOURCES=%BJAM_SOURCES% pathsys.c regexp.c rules.c scan.c search.c set BJAM_SOURCES=%BJAM_SOURCES% subst.c timestamp.c variable.c modules.c -set BJAM_SOURCES=%BJAM_SOURCES% strings.c filesys.c builtins.c md5.c pwd.c -set BJAM_SOURCES=%BJAM_SOURCES% class.c w32_getreg.c native.c modules/set.c +set BJAM_SOURCES=%BJAM_SOURCES% strings.c filesys.c builtins.c md5.c class.c +set BJAM_SOURCES=%BJAM_SOURCES% cwd.c w32_getreg.c native.c modules/set.c set BJAM_SOURCES=%BJAM_SOURCES% modules/path.c modules/regex.c set BJAM_SOURCES=%BJAM_SOURCES% modules/property-set.c modules/sequence.c set BJAM_SOURCES=%BJAM_SOURCES% modules/order.c diff --git a/v2/engine/build.jam b/v2/engine/build.jam index be7a3e4e7..119e2097e 100644 --- a/v2/engine/build.jam +++ b/v2/engine/build.jam @@ -482,7 +482,7 @@ jam.source = hash.c hcache.c headers.c hdrmacro.c jam.c jambase.c jamgram.c lists.c make.c make1.c mem.c object.c option.c output.c parse.c pathsys.c regexp.c rules.c scan.c search.c subst.c w32_getreg.c timestamp.c variable.c - modules.c strings.c filesys.c builtins.c pwd.c class.c native.c md5.c + modules.c strings.c filesys.c builtins.c class.c cwd.c native.c md5.c modules/set.c modules/path.c modules/regex.c modules/property-set.c modules/sequence.c modules/order.c ; if $(OS) = NT diff --git a/v2/engine/build.sh b/v2/engine/build.sh index cc4eea959..5f78a5631 100755 --- a/v2/engine/build.sh +++ b/v2/engine/build.sh @@ -249,7 +249,7 @@ BJAM_SOURCES="\ hash.c hdrmacro.c headers.c jam.c jambase.c jamgram.c lists.c make.c make1.c\ object.c option.c output.c parse.c pathsys.c pathunix.c regexp.c rules.c\ scan.c search.c subst.c timestamp.c variable.c modules.c strings.c filesys.c\ - builtins.c pwd.c class.c native.c md5.c w32_getreg.c modules/set.c\ + builtins.c class.c cwd.c native.c md5.c w32_getreg.c modules/set.c\ modules/path.c modules/regex.c modules/property-set.c modules/sequence.c\ modules/order.c" case $BOOST_JAM_TOOLSET in diff --git a/v2/engine/builtins.c b/v2/engine/builtins.c index a4bcc62b0..dec2ef77e 100644 --- a/v2/engine/builtins.c +++ b/v2/engine/builtins.c @@ -9,6 +9,7 @@ #include "compile.h" #include "constants.h" +#include "cwd.h" #include "filesys.h" #include "frames.h" #include "hash.h" @@ -20,7 +21,6 @@ #include "object.h" #include "parse.h" #include "pathsys.h" -#include "pwd.h" #include "rules.h" #include "strings.h" #include "subst.h" @@ -1371,7 +1371,7 @@ LIST * builtin_caller_module( FRAME * frame, int flags ) LIST * builtin_pwd( FRAME * frame, int flags ) { - return pwd(); + return list_new( object_copy( cwd() ) ); } diff --git a/v2/engine/cwd.c b/v2/engine/cwd.c new file mode 100644 index 000000000..7ebe97045 --- /dev/null +++ b/v2/engine/cwd.c @@ -0,0 +1,83 @@ +/* + * Copyright 2002. Vladimir Prus + * Copyright 2005. Rene Rivera + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ + +#include "cwd.h" + +#include "jam.h" +#include "mem.h" +#include "pathsys.h" + +#include +#include +#include + +/* MinGW on Windows declares PATH_MAX in limits.h */ +#if defined( NT ) && !defined( __GNUC__ ) +# include +# define PATH_MAX _MAX_PATH +#else +# include +# if defined( __COMO__ ) +# include +# endif +#endif + +#ifndef PATH_MAX +# define PATH_MAX 1024 +#endif + + +static OBJECT * cwd_; + + +void cwd_init( void ) +{ + int buffer_size = PATH_MAX; + char * cwd_buffer = 0; + int error; + + assert( !cwd_ ); + + do + { + char * const buffer = BJAM_MALLOC_RAW( buffer_size ); + cwd_buffer = getcwd( buffer, buffer_size ); + error = errno; + if ( cwd_buffer ) + { + /* We store the path using its canonical/long/key format. */ + OBJECT * const cwd = object_new( cwd_buffer ); + cwd_ = path_as_key( cwd ); + object_free( cwd ); + } + buffer_size *= 2; + BJAM_FREE_RAW( buffer ); + } + while ( !cwd_ && error == ERANGE ); + + if ( !cwd_ ) + { + perror( "can not get current working directory" ); + exit( EXITBAD ); + } +} + + +OBJECT * cwd( void ) +{ + assert( cwd_ ); + return cwd_; +} + + +void cwd_done( void ) +{ + assert( cwd_ ); + object_free( cwd_ ); + cwd_ = NULL; +} diff --git a/v2/engine/cwd.h b/v2/engine/cwd.h new file mode 100644 index 000000000..886714a8f --- /dev/null +++ b/v2/engine/cwd.h @@ -0,0 +1,35 @@ +/* + * Copyright 2002. Vladimir Prus + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ + +/* + * cwd.h - manages the current working folder information + */ + +#ifndef CWD_H +#define CWD_H + +#include "object.h" + + +/* cwd() - returns the current working folder */ +OBJECT * cwd( void ); + +/* cwd_init() - initialize the cwd module functionality + * + * The current working folder can not change in Boost Jam so this function + * gets the current working folder information from the OS and stores it + * internally. + * + * Expected to be called at program startup before the program's current + * working folder has been changed + */ +void cwd_init( void ); + +/* cwd_done() - cleans up the cwd module functionality */ +void cwd_done( void ); + +#endif diff --git a/v2/engine/jam.c b/v2/engine/jam.c index 5a73329f3..d633dfd96 100644 --- a/v2/engine/jam.c +++ b/v2/engine/jam.c @@ -113,7 +113,7 @@ #include "option.h" #include "output.h" #include "parse.h" -#include "pwd.h" +#include "cwd.h" #include "rules.h" #include "scan.h" #include "search.h" @@ -345,6 +345,7 @@ int main( int argc, char * * argv, char * * arg_environ ) } constants_init(); + cwd_init(); { PROFILE_ENTER( MAIN ); @@ -579,7 +580,7 @@ int main( int argc, char * * argv, char * * arg_environ ) class_done(); modules_done(); regex_done(); - pwd_done(); + cwd_done(); path_done(); function_done(); list_done(); diff --git a/v2/engine/pwd.c b/v2/engine/pwd.c deleted file mode 100644 index 440fb17d5..000000000 --- a/v2/engine/pwd.c +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2002. Vladimir Prus - * Copyright 2005. Rene Rivera - * Distributed under the Boost Software License, Version 1.0. - * (See accompanying file LICENSE_1_0.txt or copy at - * http://www.boost.org/LICENSE_1_0.txt) - */ - -#include "jam.h" -#include "pwd.h" - -#include "mem.h" -#include "object.h" -#include "pathsys.h" - -#include -#include - -/* MinGW on Windows declares PATH_MAX in limits.h */ -#if defined(NT) && !defined(__GNUC__) -# include -# define PATH_MAX _MAX_PATH -#else -# include -# if defined(__COMO__) -# include -# endif -#endif - -#ifndef PATH_MAX -# define PATH_MAX 1024 -#endif - -/* The current directory can not change in Boost Jam, so optimize pwd() by - * caching the result. -*/ -static OBJECT * pwd_result; - - -LIST * pwd( void ) -{ - if ( !pwd_result ) - { - int buffer_size = PATH_MAX; - char * result_buffer = 0; - int error; - do - { - char * const buffer = BJAM_MALLOC_RAW( buffer_size ); - result_buffer = getcwd( buffer, buffer_size ); - error = errno; - if ( result_buffer ) - { - /* We return the path using its canonical/long/key format. */ - OBJECT * const result = object_new( result_buffer ); - pwd_result = path_as_key( result ); - object_free( result ); - } - buffer_size *= 2; - BJAM_FREE_RAW( buffer ); - } - while ( !pwd_result && error == ERANGE ); - - if ( !pwd_result ) - { - perror( "can not get current directory" ); - return L0; - } - } - return list_new( object_copy( pwd_result ) ); -} - - -void pwd_done( void ) -{ - if ( pwd_result ) - { - object_free( pwd_result ); - } -} diff --git a/v2/engine/pwd.h b/v2/engine/pwd.h deleted file mode 100644 index 0e0d6fba7..000000000 --- a/v2/engine/pwd.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright 2002. Vladimir Prus - * Distributed under the Boost Software License, Version 1.0. - * (See accompanying file LICENSE_1_0.txt or copy at - * http://www.boost.org/LICENSE_1_0.txt) - */ - -#ifndef PWD_H -#define PWD_H - -#include "lists.h" - -LIST * pwd( void ); -void pwd_done( void ); - -#endif