42#if defined (HAVE_CONFIG_H)
63#if defined (OCTAVE_USE_WINDOWS_API)
72 : m_follow_symbolic_links (true), m_verbatim_pwd (true),
73 m_current_directory (), m_prog_name (), m_prog_invocation_name (),
74 m_user_name (), m_host_name ()
85env *env::s_instance =
nullptr;
94 s_instance =
new env ();
104 return (instance_ok ())
105 ? s_instance->do_polite_directory_format (name) :
"";
111 return (instance_ok ())
112 ? s_instance->do_absolute_pathname (s) :
false;
118 return (instance_ok ())
119 ? s_instance->do_rooted_relative_pathname (s) :
false;
125 return (instance_ok ())
126 ? s_instance->do_base_pathname (s) :
"";
132 return (instance_ok ())
133 ? s_instance->do_make_absolute (s, dot_path) :
"";
139 return (instance_ok ())
140 ? s_instance->do_getcwd () :
"";
146 return (instance_ok ())
147 ? s_instance->do_get_home_directory () :
"";
153 return (instance_ok ())
154 ? s_instance->do_get_temp_directory () :
"";
160 return (instance_ok ())
161 ? s_instance->do_get_user_config_directory () :
"";
167 return (instance_ok ())
168 ? s_instance->do_get_user_data_directory () :
"";
174 return (instance_ok ())
175 ? s_instance->m_prog_name :
"";
181 return (instance_ok ())
182 ? s_instance->m_prog_invocation_name :
"";
189 s_instance->do_set_program_name (s);
195 return (instance_ok ())
196 ? s_instance->do_get_user_name () :
"";
202 return (instance_ok ())
203 ? s_instance->do_get_host_name () :
"";
207env::do_get_temp_directory ()
const
209 std::string tempd = do_getenv (
"TMPDIR");
211#if defined (__MINGW32__) || defined (_MSC_VER)
214 tempd = do_getenv (
"TEMP");
217 tempd = do_getenv (
"TMP");
219#if defined (P_tmpdir)
226 if (tempd.empty () || tempd == R
"(\)")
227 tempd = R"(c:\temp)";
232 tempd = do_getenv (
"TMP");
234#if defined (P_tmpdir)
248env::do_get_user_config_directory ()
252#if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && defined (OCTAVE_USE_WINDOWS_API)
253 wchar_t path[MAX_PATH+1];
254 if (SHGetFolderPathW (
nullptr, CSIDL_APPDATA | CSIDL_FLAG_DONT_VERIFY,
255 nullptr, SHGFP_TYPE_CURRENT, path) == S_OK)
258 cfg_dir = do_getenv (
"XDG_CONFIG_HOME");
261 if (cfg_dir.empty ())
262 cfg_dir = do_get_home_directory () + sys::file_ops::dir_sep_str ()
269env::do_get_user_data_directory ()
273#if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && defined (OCTAVE_USE_WINDOWS_API)
274 wchar_t path[MAX_PATH+1];
275 if (SHGetFolderPathW (
nullptr, CSIDL_APPDATA | CSIDL_FLAG_DONT_VERIFY,
276 nullptr, SHGFP_TYPE_CURRENT, path) == S_OK)
279 data_dir = do_getenv (
"XDG_DATA_HOME");
283 data_dir = do_get_home_directory () + sys::file_ops::dir_sep_str ()
284 +
".local" + sys::file_ops::dir_sep_str () +
"share";
297 return (instance_ok ())
298 ? s_instance->do_getenv (name) :
"";
316 std::string display =
getenv (
"DISPLAY");
318 return ! display.empty ();
324 return (instance_ok ())
325 ? s_instance->do_chdir (newdir) :
false;
329env::do_set_program_name (
const std::string& s)
331 static bool initialized =
false;
344 m_prog_invocation_name
348 = m_prog_invocation_name.find_last_of (sys::file_ops::dir_sep_chars ());
351 m_prog_name = (pos == std::string::npos
352 ? m_prog_invocation_name
353 : m_prog_invocation_name.substr (pos+1));
363env::do_polite_directory_format (
const std::string& name)
367 std::string home_dir = do_get_home_directory ();
369 std::size_t
len = home_dir.length ();
371 if (
len > 1 && home_dir == name.substr (0,
len)
372 && (name.length () ==
len || sys::file_ops::is_dir_sep (name[
len])))
375 retval.append (name.substr (
len));
384env::do_absolute_pathname (
const std::string& s)
const
386 std::size_t
len = s.length ();
391 if (sys::file_ops::is_dir_sep (s[0]))
394#if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)
395 if ((
len == 2 && isalpha (s[0]) && s[1] ==
':')
396 || (
len > 2 && isalpha (s[0]) && s[1] ==
':'
397 && sys::file_ops::is_dir_sep (s[2])))
405env::do_rooted_relative_pathname (
const std::string& s)
const
407 std::size_t
len = s.length ();
412 if (
len == 1 && s[0] ==
'.')
415 if (
len > 1 && s[0] ==
'.' && sys::file_ops::is_dir_sep (s[1]))
418 if (
len == 2 && s[0] ==
'.' && s[1] ==
'.')
421 if (
len > 2 && s[0] ==
'.' && s[1] ==
'.'
422 && sys::file_ops::is_dir_sep (s[2]))
433env::do_base_pathname (
const std::string& s)
const
435 if (! (do_absolute_pathname (s) || do_rooted_relative_pathname (s)))
438 std::size_t pos = s.find_last_of (sys::file_ops::dir_sep_chars ());
440 if (pos == std::string::npos)
443 return s.substr (pos+1);
450env::do_make_absolute (
const std::string& s,
451 const std::string& dot_path)
const
453 if (dot_path.empty () || s.empty () || do_absolute_pathname (s))
461 std::string current_dir = dot_path;
463 if (! sys::file_ops::is_dir_sep (current_dir.back ()))
464 current_dir.append (sys::file_ops::dir_sep_str ());
467 std::size_t slen = s.length ();
476 if (sys::file_ops::is_dir_sep (s[i+1]))
484 || sys::file_ops::is_dir_sep (s[i+2])))
490 pathname_backup (current_dir, 1);
497 sep_pos = s.find_first_of (sys::file_ops::dir_sep_chars (), i);
499 if (sep_pos == std::string::npos)
501 current_dir.append (s, i, sep_pos-i);
504 else if (sep_pos == i)
511 current_dir.append (s, i, sep_pos-i+1);
517 if (sys::file_ops::is_dir_sep (current_dir.back ()))
518 current_dir.pop_back ();
528 if (! m_follow_symbolic_links)
529 m_current_directory =
"";
531 if (m_verbatim_pwd || m_current_directory.empty ())
532 m_current_directory = sys::getcwd ();
534 return m_current_directory;
541env::do_get_home_directory ()
543 std::string hd = do_getenv (
"HOME");
545#if defined (__MINGW32__) || defined (_MSC_VER)
549 std::string drv = do_getenv (
"HOMEDRIVE");
551 hd = do_getenv (
"HOMEPATH");
553 hd = drv + do_getenv (
"HOMEPATH");
559 sys::password pw = sys::password::getpwuid (sys::getuid ());
561 hd = (pw ? pw.dir () : std::string (sys::file_ops::dir_sep_str ()));
568env::do_get_user_name ()
570 if (m_user_name.empty ())
572 sys::password pw = sys::password::getpwuid (sys::getuid ());
574 m_user_name = (pw ? pw.name () :
"unknown");
581env::do_get_host_name ()
583 if (m_host_name.empty ())
589 m_host_name = (status < 0) ?
"unknown" : hostname;
596env::do_getenv (
const std::string& name)
const
605env::do_chdir (
const std::string& newdir)
611 if (m_follow_symbolic_links)
613 if (m_current_directory.empty ())
616 if (m_current_directory.empty ())
619 tmp = do_make_absolute (newdir, m_current_directory);
622 if (tmp.length () > 1 && sys::file_ops::is_dir_sep (tmp.back ()))
625 if (! sys::chdir (tmp))
627 m_current_directory = tmp;
632 retval = (! sys::chdir (newdir));
640env::pathname_backup (std::string& path,
int n)
const
645 std::size_t i = path.length () - 1;
649 while (sys::file_ops::is_dir_sep (path[i]) && i > 0)
652#if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)
654 if (i == 1 && path[i] ==
':')
657 i = std::min (i+2, path.length ());
662 while (! sys::file_ops::is_dir_sep (path[i]) && i > 0)
672env::error (
int err_num)
const
674 (*current_liboctave_error_handler) (
"%s", std::strerror (err_num));
678env::error (
const std::string& s)
const
680 (*current_liboctave_error_handler) (
"%s", s.c_str ());
683OCTAVE_END_NAMESPACE(sys)
684OCTAVE_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)