1 // Copyright (c) 2006, 2007 Julio M. Merino Vidal
2 // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
3 // Copyright (c) 2009 Boris Schaeling
4 // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
5 // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 
10 #ifndef BOOST_PROCESS_WINDOWS_SEARCH_PATH_HPP
11 #define BOOST_PROCESS_WINDOWS_SEARCH_PATH_HPP
12 
13 #include <boost/process/detail/config.hpp>
14 #include <boost/filesystem/path.hpp>
15 #include <boost/filesystem/operations.hpp>
16 #include <boost/system/error_code.hpp>
17 #include <string>
18 #include <stdexcept>
19 #include <array>
20 #include <atomic>
21 #include <cstdlib>
22 #include <boost/winapi/shell.hpp>
23 #include <boost/process/environment.hpp>
24 
25 namespace boost { namespace process { namespace detail { namespace windows {
26 
search_path(const boost::filesystem::path & filename,const std::vector<boost::filesystem::path> & path)27 inline boost::filesystem::path search_path(
28         const boost::filesystem::path &filename,
29         const std::vector<boost::filesystem::path> &path)
30 {
31     const ::boost::process::wnative_environment ne{};
32     typedef typename ::boost::process::wnative_environment::const_entry_type value_type;
33     const auto id = L"PATHEXT";
34 
35     auto itr = std::find_if(ne.cbegin(), ne.cend(),
36             [&](const value_type & e)
37              {return id == ::boost::to_upper_copy(e.get_name(), ::boost::process::detail::process_locale());});
38 
39     auto extensions_in = itr->to_vector();
40 
41     std::vector<std::wstring> extensions((extensions_in.size() * 2) + 1);
42 
43     auto it_ex = extensions.begin();
44     it_ex++;
45     it_ex = std::transform(extensions_in.begin(), extensions_in.end(), it_ex,
46                 [](const std::wstring & ws){return boost::to_lower_copy(ws, ::boost::process::detail::process_locale());});
47 
48     std::transform(extensions_in.begin(), extensions_in.end(), it_ex,
49                 [](const std::wstring & ws){return boost::to_upper_copy(ws, ::boost::process::detail::process_locale());});
50 
51 
52     std::copy(std::make_move_iterator(extensions_in.begin()), std::make_move_iterator(extensions_in.end()), extensions.begin() + 1);
53 
54 
55     for (auto & ext : extensions)
56         boost::to_lower(ext);
57 
58     for (const boost::filesystem::path & pp_ : path)
59     {
60         auto p = pp_ / filename;
61         for (boost::filesystem::path ext : extensions)
62         {
63             boost::filesystem::path pp_ext = p;
64             pp_ext += ext;
65             boost::system::error_code ec;
66             bool file = boost::filesystem::is_regular_file(pp_ext, ec);
67             if (!ec && file &&
68                 ::boost::winapi::sh_get_file_info(pp_ext.native().c_str(), 0, 0, 0, ::boost::winapi::SHGFI_EXETYPE_))
69             {
70                 return pp_ext;
71             }
72         }
73     }
74     return "";
75 }
76 
77 }}}}
78 
79 #endif
80