FILE * fopen (const char *path, const char *mode) { FILE *f; char win32_path[MAX_PATH + 1]; /* Assume PATH is a Windows path. */ f = msvcrt_fopen (path, mode); if (f || errno != ENOENT) return f; /* Perhaps it is a Cygwin path? */ if (cygpath (path, win32_path)) f = msvcrt_fopen (win32_path, mode); return f; }
/** * Locates the executable program \a file in all the directory components * specified in \a path. If \a path is empty, the value of the PATH * environment variable is used. * * The path variable is interpreted following the same conventions used * to parse the PATH environment variable in the underlying platform. * * \throw boost::filesystem::filesystem_error If the file cannot be found * in the path. */ inline std::string find_executable_in_path(const std::string &file, std::string path = "") { #if defined(BOOST_POSIX_API) BOOST_ASSERT(file.find('/') == std::string::npos); #elif defined(BOOST_WINDOWS_API) BOOST_ASSERT(file.find_first_of("\\/") == std::string::npos); #endif std::string result; #if defined(BOOST_POSIX_API) if (path.empty()) { const char *envpath = ::getenv("PATH"); if (!envpath) boost::throw_exception(boost::filesystem::filesystem_error("boost::process::find_executable_in_path: retrieving PATH failed", file, boost::system::errc::make_error_code(boost::system::errc::no_such_file_or_directory))); path = envpath; } BOOST_ASSERT(!path.empty()); #if defined(__CYGWIN__) if (!::cygwin_posix_path_list_p(path.c_str())) { int size = ::cygwin_win32_to_posix_path_list_buf_size(path.c_str()); boost::scoped_array<char> cygpath(new char[size]); ::cygwin_win32_to_posix_path_list(path.c_str(), cygpath.get()); path = cygpath.get(); } #endif std::string::size_type pos1 = 0, pos2; do { pos2 = path.find(':', pos1); std::string dir = (pos2 != std::string::npos) ? path.substr(pos1, pos2 - pos1) : path.substr(pos1); std::string f = dir + (boost::algorithm::ends_with(dir, "/") ? "" : "/") + file; if (!::access(f.c_str(), X_OK)) result = f; pos1 = pos2 + 1; } while (pos2 != std::string::npos && result.empty()); #elif defined(BOOST_WINDOWS_API) const char *exts[] = { "", ".exe", ".com", ".bat", NULL }; const char **ext = exts; while (*ext) { char buf[MAX_PATH]; char *dummy; DWORD size = ::SearchPathA(path.empty() ? NULL : path.c_str(), file.c_str(), *ext, MAX_PATH, buf, &dummy); BOOST_ASSERT(size < MAX_PATH); if (size > 0) { result = buf; break; } ++ext; } #endif if (result.empty()) boost::throw_exception(boost::filesystem::filesystem_error("boost::process::find_executable_in_path: file not found", file, boost::system::errc::make_error_code(boost::system::errc::no_such_file_or_directory))); return result; }
/*=========================================================================*/ int WINAPI WinMain( //Windows program entry point HINSTANCE hInstance, //handle to this application HINSTANCE hPrevInstance, //handle to previous application LPSTR lpCmdLine, //executed command int nCmdShow //window display options ) { //program exit status //local macros #define BUFFER_SIZE ( 512 ) //common size of string buffers //local variables int argc; //number of command line arguments LPTSTR* argv; //list of command line arguments LPWSTR* arguments; //list of argument string pointers static TCHAR command[ BUFFER_SIZE ]; //command to execute PROCESS_INFORMATION cp_pr_info; //CreateProcess process info BOOL cp_result; //result of CreateProcess STARTUPINFO cp_su_info; //CreateProcess startup info static TCHAR path[ BUFFER_SIZE ]; //file path argument error_t path_result;//error from path translation DWORD exit_code; //exit code of spawned process HRESULT str_result; //result of string operations //parse the command line arguments = CommandLineToArgvW( GetCommandLineW(), &argc ); if( arguments == NULL ) { return 1; } //see if a file was specified if( argc > 1 ) { //check for need to convert to ANSI characters #ifndef UNICODE argv = wc2mb_array( ( LPCWSTR* ) arguments, argc ); if( argv == NULL ) { LocalFree( arguments ); return 1; } #else argv = arguments; #endif //translate the file path path_result = cygpath( path, BUFFER_SIZE, argv[ 1 ], PATH_OPT_UNIX ); #ifndef UNICODE free_array( ( void** ) argv, argc ); #endif if( path_result < ERROR_NONE ) { LocalFree( arguments ); return 1; } //format command to execute a program with a file argument str_result = StringCchPrintf( command, BUFFER_SIZE, _T( "%s %s '%s \"%s\"'" ), config_console, config_shell, config_target, path ); } //no file specified else { //format command to execute a program without a file argument str_result = StringCchPrintf( command, BUFFER_SIZE, _T( "%s %s '%s'" ), config_console, config_shell, config_target ); } //parsed command-line arguments are no longer needed LocalFree( arguments ); //check string formatting results if( str_result != S_OK ) { return 1; } //initialize CreateProcess argument data memset( &cp_su_info, 0, sizeof( cp_su_info ) ); memset( &cp_pr_info, 0, sizeof( cp_pr_info ) ); //create the process for mintty cp_result = CreateProcess( NULL, command, NULL, NULL, FALSE, 0, NULL, NULL, &cp_su_info, &cp_pr_info ); //wait for console to finish if( cp_result == TRUE ) { WaitForSingleObject( cp_pr_info.hProcess, INFINITE ); cp_result = GetExitCodeProcess( cp_pr_info.hProcess, &exit_code ); CloseHandle( cp_pr_info.hProcess ); CloseHandle( cp_pr_info.hThread ); } //console was not started else { exit_code = 1; } //return exit code from spawned process return exit_code; }