42#if defined (HAVE_CONFIG_H)
63#if defined (OCTAVE_USE_WINDOWS_API)
73 : m_follow_symbolic_links (true), m_verbatim_pwd (true),
74 m_current_directory (), m_prog_name (), m_prog_invocation_name (),
75 m_user_name (), m_host_name ()
86env *env::s_instance =
nullptr;
95 s_instance =
new env ();
105 return (instance_ok ())
106 ? s_instance->do_polite_directory_format (name) :
"";
112 return (instance_ok ())
113 ? s_instance->do_absolute_pathname (s) :
false;
119 return (instance_ok ())
120 ? s_instance->do_rooted_relative_pathname (s) :
false;
126 return (instance_ok ())
127 ? s_instance->do_base_pathname (s) :
"";
133 return (instance_ok ())
134 ? s_instance->do_make_absolute (s, dot_path) :
"";
140 return (instance_ok ())
141 ? s_instance->do_getcwd () :
"";
147 return (instance_ok ())
148 ? s_instance->do_get_home_directory () :
"";
154 return (instance_ok ())
155 ? s_instance->do_get_temp_directory () :
"";
161 return (instance_ok ())
162 ? s_instance->do_get_user_config_directory () :
"";
168 return (instance_ok ())
169 ? s_instance->do_get_user_data_directory () :
"";
175 return (instance_ok ())
176 ? s_instance->m_prog_name :
"";
182 return (instance_ok ())
183 ? s_instance->m_prog_invocation_name :
"";
190 s_instance->do_set_program_name (s);
196 return (instance_ok ())
197 ? s_instance->do_get_user_name () :
"";
203 return (instance_ok ())
204 ? s_instance->do_get_host_name () :
"";
208env::do_get_temp_directory ()
const
210 std::string tempd = do_getenv (
"TMPDIR");
212#if defined (__MINGW32__) || defined (_MSC_VER)
215 tempd = do_getenv (
"TEMP");
218 tempd = do_getenv (
"TMP");
220#if defined (P_tmpdir)
227 if (tempd.empty () || tempd == R
"(\)")
228 tempd = R"(c:\temp)";
233 tempd = do_getenv (
"TMP");
235#if defined (P_tmpdir)
249env::do_get_user_config_directory ()
253#if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && defined (OCTAVE_USE_WINDOWS_API)
254 wchar_t path[MAX_PATH+1];
255 if (SHGetFolderPathW (
nullptr, CSIDL_APPDATA | CSIDL_FLAG_DONT_VERIFY,
256 nullptr, SHGFP_TYPE_CURRENT, path) == S_OK)
259 cfg_dir = do_getenv (
"XDG_CONFIG_HOME");
262 if (cfg_dir.empty ())
263 cfg_dir = do_get_home_directory () + sys::file_ops::dir_sep_str ()
270env::do_get_user_data_directory ()
274#if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && defined (OCTAVE_USE_WINDOWS_API)
275 wchar_t path[MAX_PATH+1];
276 if (SHGetFolderPathW (
nullptr, CSIDL_APPDATA | CSIDL_FLAG_DONT_VERIFY,
277 nullptr, SHGFP_TYPE_CURRENT, path) == S_OK)
280 data_dir = do_getenv (
"XDG_DATA_HOME");
284 data_dir = do_get_home_directory () + sys::file_ops::dir_sep_str ()
285 +
".local" + sys::file_ops::dir_sep_str () +
"share";
298 return (instance_ok ())
299 ? s_instance->do_getenv (name) :
"";
317 std::string display =
getenv (
"DISPLAY");
319 return ! display.empty ();
325 return (instance_ok ())
326 ? s_instance->do_chdir (newdir) :
false;
330env::do_set_program_name (
const std::string& s)
332 static bool initialized =
false;
345 m_prog_invocation_name
349 = m_prog_invocation_name.find_last_of (sys::file_ops::dir_sep_chars ());
352 m_prog_name = (pos == std::string::npos
353 ? m_prog_invocation_name
354 : m_prog_invocation_name.substr (pos+1));
364env::do_polite_directory_format (
const std::string& name)
368 std::string home_dir = do_get_home_directory ();
370 std::size_t
len = home_dir.length ();
372 if (
len > 1 && home_dir == name.substr (0,
len)
373 && (name.length () ==
len || sys::file_ops::is_dir_sep (name[
len])))
376 retval.append (name.substr (
len));
385env::do_absolute_pathname (
const std::string& s)
const
387 std::size_t
len = s.length ();
392 if (sys::file_ops::is_dir_sep (s[0]))
395#if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)
396 if ((
len == 2 && isalpha (s[0]) && s[1] ==
':')
397 || (
len > 2 && isalpha (s[0]) && s[1] ==
':'
398 && sys::file_ops::is_dir_sep (s[2])))
406env::do_rooted_relative_pathname (
const std::string& s)
const
408 std::size_t
len = s.length ();
413 if (
len == 1 && s[0] ==
'.')
416 if (
len > 1 && s[0] ==
'.' && sys::file_ops::is_dir_sep (s[1]))
419 if (
len == 2 && s[0] ==
'.' && s[1] ==
'.')
422 if (
len > 2 && s[0] ==
'.' && s[1] ==
'.'
423 && sys::file_ops::is_dir_sep (s[2]))
434env::do_base_pathname (
const std::string& s)
const
436 if (! (do_absolute_pathname (s) || do_rooted_relative_pathname (s)))
439 std::size_t pos = s.find_last_of (sys::file_ops::dir_sep_chars ());
441 if (pos == std::string::npos)
444 return s.substr (pos+1);
451env::do_make_absolute (
const std::string& s,
452 const std::string& dot_path)
const
454 if (dot_path.empty () || s.empty () || do_absolute_pathname (s))
462 std::string current_dir = dot_path;
464 if (! sys::file_ops::is_dir_sep (current_dir.back ()))
465 current_dir.append (sys::file_ops::dir_sep_str ());
468 std::size_t slen = s.length ();
477 if (sys::file_ops::is_dir_sep (s[i+1]))
485 || sys::file_ops::is_dir_sep (s[i+2])))
491 pathname_backup (current_dir, 1);
498 sep_pos = s.find_first_of (sys::file_ops::dir_sep_chars (), i);
500 if (sep_pos == std::string::npos)
502 current_dir.append (s, i, sep_pos-i);
505 else if (sep_pos == i)
512 current_dir.append (s, i, sep_pos-i+1);
518 if (sys::file_ops::is_dir_sep (current_dir.back ()))
519 current_dir.pop_back ();
529 if (! m_follow_symbolic_links)
530 m_current_directory =
"";
532 if (m_verbatim_pwd || m_current_directory.empty ())
533 m_current_directory = sys::getcwd ();
535 return m_current_directory;
542env::do_get_home_directory ()
544 std::string hd = do_getenv (
"HOME");
546#if defined (__MINGW32__) || defined (_MSC_VER)
550 std::string drv = do_getenv (
"HOMEDRIVE");
552 hd = do_getenv (
"HOMEPATH");
554 hd = drv + do_getenv (
"HOMEPATH");
560 sys::password pw = sys::password::getpwuid (sys::getuid ());
562 hd = (pw ? pw.dir () : std::string (sys::file_ops::dir_sep_str ()));
569env::do_get_user_name ()
571 if (m_user_name.empty ())
573 sys::password pw = sys::password::getpwuid (sys::getuid ());
575 m_user_name = (pw ? pw.name () :
"unknown");
582env::do_get_host_name ()
584 if (m_host_name.empty ())
590 m_host_name = (status < 0) ?
"unknown" : hostname;
597env::do_getenv (
const std::string& name)
const
606env::do_chdir (
const std::string& newdir)
612 if (m_follow_symbolic_links)
614 if (m_current_directory.empty ())
617 if (m_current_directory.empty ())
620 tmp = do_make_absolute (newdir, m_current_directory);
623 if (tmp.length () > 1 && sys::file_ops::is_dir_sep (tmp.back ()))
626 if (! sys::chdir (tmp))
628 m_current_directory = tmp;
633 retval = (! sys::chdir (newdir));
641env::pathname_backup (std::string& path,
int n)
const
646 std::size_t i = path.length () - 1;
650 while (sys::file_ops::is_dir_sep (path[i]) && i > 0)
653#if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)
655 if (i == 1 && path[i] ==
':')
658 i = std::min (i+2, path.length ());
663 while (! sys::file_ops::is_dir_sep (path[i]) && i > 0)
673env::error (
int err_num)
const
675 (*current_liboctave_error_handler) (
"%s", std::strerror (err_num));
679env::error (
const std::string& s)
const
681 (*current_liboctave_error_handler) (
"%s", s.c_str ());
684OCTAVE_END_NAMESPACE(sys)
685OCTAVE_END_NAMESPACE(octave)
static std::string base_pathname(const std::string &s)
static std::string get_user_name()
static std::string getenv(const std::string &name)
static std::string get_home_directory()
static bool absolute_pathname(const std::string &s)
static bool have_x11_display()
static std::string get_program_invocation_name()
static bool rooted_relative_pathname(const std::string &s)
static bool chdir(const std::string &newdir)
static std::string get_user_config_directory()
static std::string make_absolute(const std::string &s, const std::string &dot_path=get_current_directory())
static std::string get_temp_directory()
static void putenv(const std::string &name, const std::string &value)
static std::string get_current_directory()
static std::string get_user_data_directory()
static bool isenv(const std::string &name)
static std::string polite_directory_format(const std::string &name)
static std::string get_host_name()
static std::string get_program_name()
static void set_program_name(const std::string &s)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
std::string u8_from_wstring(const std::wstring &wchar_string)
std::string getenv_wrapper(const std::string &name)
bool isenv_wrapper(const std::string &name)
void putenv_wrapper(const std::string &name, const std::string &value)
const char * octave_set_program_name_wrapper(const char *pname)
int octave_gethostname_wrapper(char *nm, size_t len)