start_dir.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // process/start_dir.hpp
  3. // ~~~~~~~~
  4. //
  5. // Copyright (c) 2021 Klemens D. Morgenstern (klemens dot morgenstern at gmx dot net)
  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. #ifndef BOOST_PROCESS_v2_START_DIR_HPP
  10. #define BOOST_PROCESS_v2_START_DIR_HPP
  11. #include <boost/process/v2/detail/config.hpp>
  12. #include <boost/process/v2/detail/last_error.hpp>
  13. #include <boost/process/v2/default_launcher.hpp>
  14. BOOST_PROCESS_V2_BEGIN_NAMESPACE
  15. /// Initializer for the starting directory of a subprocess to be launched.
  16. struct process_start_dir
  17. {
  18. filesystem::path start_dir;
  19. process_start_dir(filesystem::path start_dir) : start_dir(std::move(start_dir))
  20. {
  21. }
  22. #if defined(BOOST_PROCESS_V2_WINDOWS)
  23. error_code on_setup(windows::default_launcher & launcher,
  24. const filesystem::path &, const std::wstring &)
  25. {
  26. launcher.current_directory = start_dir;
  27. return error_code {};
  28. };
  29. #else
  30. error_code on_exec_setup(posix::default_launcher & launcher,
  31. const filesystem::path &, const char * const *)
  32. {
  33. if (::chdir(start_dir.c_str()) == -1)
  34. return detail::get_last_error();
  35. else
  36. return error_code ();
  37. }
  38. #endif
  39. };
  40. BOOST_PROCESS_V2_END_NAMESPACE
  41. #endif // BOOST_PROCESS_v2_START_DIR_HPP