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 ()
86 env *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 () :
"";
208 env::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)
249 env::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 ())
270 env::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");
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;
330 env::do_set_program_name (
const std::string& s)
332 static bool initialized =
false;
345 m_prog_invocation_name
352 m_prog_name = (pos == std::string::npos
353 ? m_prog_invocation_name
354 : m_prog_invocation_name.substr (pos+1));
364 env::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)
376 retval.append (name.substr (
len));
385 env::do_absolute_pathname (
const std::string& s)
const
387 std::size_t
len = s.length ();
395 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)
396 if ((
len == 2 && isalpha (s[0]) && s[1] ==
':')
397 || (
len > 2 && isalpha (s[0]) && s[1] ==
':'
406 env::do_rooted_relative_pathname (
const std::string& s)
const
408 std::size_t
len = s.length ();
413 if (
len == 1 && s[0] ==
'.')
419 if (
len == 2 && s[0] ==
'.' && s[1] ==
'.')
422 if (
len > 2 && s[0] ==
'.' && s[1] ==
'.'
434 env::do_base_pathname (
const std::string& s)
const
436 if (! (do_absolute_pathname (s) || do_rooted_relative_pathname (s)))
441 if (pos == std::string::npos)
444 return s.substr (pos+1);
451 env::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;
468 std::size_t slen = s.length ();
491 pathname_backup (current_dir, 1);
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);
519 current_dir.pop_back ();
529 if (! m_follow_symbolic_links)
530 m_current_directory =
"";
532 if (m_verbatim_pwd || m_current_directory.empty ())
535 return m_current_directory;
542 env::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 ());
569 env::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");
582 env::do_get_host_name ()
584 if (m_host_name.empty ())
590 m_host_name = (status < 0) ?
"unknown" : hostname;
597 env::do_getenv (
const std::string& name)
const
606 env::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);
628 m_current_directory = tmp;
641 env::pathname_backup (std::string& path,
int n)
const
646 std::size_t i = path.length () - 1;
653 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)
655 if (i == 1 && path[i] ==
':')
673 env::error (
int err_num)
const
675 (*current_liboctave_error_handler) (
"%s", std::strerror (err_num));
679 env::error (
const std::string& s)
const
681 (*current_liboctave_error_handler) (
"%s", s.c_str ());
684 OCTAVE_END_NAMESPACE(sys)
685 OCTAVE_END_NAMESPACE(
octave)
charNDArray min(char d, const charNDArray &m)
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 dir_sep_str()
std::string dir_sep_chars()
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)
int chdir(const std::string &path_arg)
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)