1 /* 2 * Copyright 2002. Vladimir Prus 3 * Copyright 2005. Rene Rivera 4 * Distributed under the Boost Software License, Version 1.0. 5 * (See accompanying file LICENSE_1_0.txt or copy at 6 * http://www.boost.org/LICENSE_1_0.txt) 7 */ 8 9 #include "cwd.h" 10 11 #include "jam.h" 12 #include "mem.h" 13 #include "pathsys.h" 14 15 #include <assert.h> 16 #include <errno.h> 17 #include <limits.h> 18 19 /* MinGW on Windows declares PATH_MAX in limits.h */ 20 #if defined( NT ) && !defined( __GNUC__ ) 21 # include <direct.h> 22 # define PATH_MAX _MAX_PATH 23 #else 24 # include <unistd.h> 25 # if defined( __COMO__ ) 26 # include <linux/limits.h> 27 # endif 28 #endif 29 30 #ifndef PATH_MAX 31 # define PATH_MAX 1024 32 #endif 33 34 35 static OBJECT * cwd_; 36 namespace 37 { 38 std::string cwd_s; 39 } 40 41 cwd_init(void)42void cwd_init( void ) 43 { 44 int buffer_size = PATH_MAX; 45 char * cwd_buffer = 0; 46 int error; 47 48 assert( !cwd_ ); 49 50 do 51 { 52 char * const buffer = (char *)BJAM_MALLOC_RAW( buffer_size ); 53 #ifdef OS_VMS 54 /* cwd in POSIX-format */ 55 cwd_buffer = getcwd( buffer, buffer_size, 0 ); 56 #else 57 cwd_buffer = getcwd( buffer, buffer_size ); 58 #endif 59 error = errno; 60 if ( cwd_buffer ) 61 { 62 /* We store the path using its canonical/long/key format. */ 63 OBJECT * const cwd = object_new( cwd_buffer ); 64 cwd_ = path_as_key( cwd ); 65 object_free( cwd ); 66 cwd_s = cwd_buffer; 67 } 68 buffer_size *= 2; 69 BJAM_FREE_RAW( buffer ); 70 } 71 while ( !cwd_ && error == ERANGE ); 72 73 if ( !cwd_ ) 74 { 75 perror( "can not get current working directory" ); 76 exit( EXITBAD ); 77 } 78 } 79 80 cwd(void)81OBJECT * cwd( void ) 82 { 83 assert( cwd_ ); 84 return cwd_; 85 } 86 87 cwd_done(void)88void cwd_done( void ) 89 { 90 assert( cwd_ ); 91 object_free( cwd_ ); 92 cwd_ = NULL; 93 } 94 95 cwd_str()96const std::string & b2::cwd_str() 97 { 98 return cwd_s; 99 }