environment_posix.ipp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // process/this_process/detail/environment_posix.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. //
  10. #ifndef BOOST_PROCESS_V2_DETAIL_ENVIRONMENT_WIN_HPP
  11. #define BOOST_PROCESS_V2_DETAIL_ENVIRONMENT_WIN_HPP
  12. #include <boost/process/v2/detail/config.hpp>
  13. #include <boost/process/v2/detail/last_error.hpp>
  14. #include <boost/process/v2/environment.hpp>
  15. #include <boost/process/v2/cstring_ref.hpp>
  16. #include <unistd.h>
  17. #include <cstring>
  18. BOOST_PROCESS_V2_BEGIN_NAMESPACE
  19. namespace environment
  20. {
  21. namespace detail
  22. {
  23. basic_cstring_ref<char_type, value_char_traits<char>> get(
  24. basic_cstring_ref<char_type, key_char_traits<char_type>> key,
  25. error_code & ec)
  26. {
  27. auto res = ::getenv(key.c_str());
  28. if (res == nullptr)
  29. {
  30. BOOST_PROCESS_V2_ASSIGN_EC(ec, ENOENT, system_category())
  31. return {};
  32. }
  33. return res;
  34. }
  35. void set(basic_cstring_ref<char_type, key_char_traits<char_type>> key,
  36. basic_cstring_ref<char_type, value_char_traits<char_type>> value,
  37. error_code & ec)
  38. {
  39. if (::setenv(key.c_str(), value.c_str(), true))
  40. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  41. }
  42. void unset(basic_cstring_ref<char_type, key_char_traits<char_type>> key, error_code & ec)
  43. {
  44. if (::unsetenv(key.c_str()))
  45. BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec)
  46. }
  47. native_handle_type load_native_handle() { return ::environ; }
  48. native_iterator next(native_iterator nh)
  49. {
  50. return nh + 1;
  51. }
  52. native_iterator find_end(native_handle_type nh)
  53. {
  54. while (*nh != nullptr)
  55. nh++;
  56. return nh;
  57. }
  58. bool is_executable(const filesystem::path & p, error_code & ec)
  59. {
  60. return filesystem::is_regular_file(p, ec) && (::access(p.c_str(), X_OK) == 0);
  61. }
  62. }
  63. }
  64. BOOST_PROCESS_V2_END_NAMESPACE
  65. #endif //BOOST_PROCESS_V2_DETAIL_ENVIRONMENT_WIN_HPP