diff --git a/dlib/dlib.cc b/dlib/dlib.cc index 6e6a6b4..395d75f 100644 --- a/dlib/dlib.cc +++ b/dlib/dlib.cc @@ -27,6 +27,8 @@ #include #include +#include + #include "dlib.hh" static bool dLib_show_msg = TRUE; @@ -902,17 +904,27 @@ void dLib_show_messages(bool show) /** * Return the current working directory in a new string */ -char *dGetcwd (void) +std::optional< std::string > +dGetcwd_string() +{ + auto owned= Alepha::AutoRAII< char * >{ dGetcwd, dFree }; + const char *const ptr= owned; + + if( not ptr ) return std::nullopt; + return ptr; +} + +CharPtrNoStringConversion dGetcwd (void) { size_t size = 128; while (1) { char *buffer = dNew(char, size); if (getcwd (buffer, size) == buffer) - return buffer; + return { buffer }; dFree (buffer); if (errno != ERANGE) - return 0; + return {}; size *= 2; } } @@ -920,7 +932,17 @@ char *dGetcwd (void) /** * Return the home directory in a static string (don't free) */ -char *dGethomedir (void) +std::optional< std::string > +dGethomedir_string() +{ + auto owned= Alepha::AutoRAII< char * >{ dGethomedir, dFree }; + const char *const ptr= owned; + + if( not ptr ) return std::nullopt; + return ptr; +} + +CharPtrNoStringConversion dGethomedir (void) { static char *homedir = NULL; @@ -935,7 +957,7 @@ char *dGethomedir (void) homedir = dStrdup("/"); } } - return homedir; + return { homedir }; } /** diff --git a/dlib/dlib.hh b/dlib/dlib.hh index 91341d3..712d636 100644 --- a/dlib/dlib.hh +++ b/dlib/dlib.hh @@ -190,8 +190,11 @@ void dLib_show_messages(bool show); /* *- Misc utility functions ---------------------------------------------------- */ -char *dGetcwd(void); -char *dGethomedir(void); +CharPtrNoStringConversion dGetcwd(); +std::optional< std::string > dGetcwd_string(); +CharPtrNoStringConversion dGethomedir(); +std::optional< std::string > dGethomedir_string(); + std::optional< std::string > dGetline_string(FILE *stream); char *dGetline_unsafe(FILE *stream); std::optional< std::string > dGetline(FILE *stream); diff --git a/dpid/dpid.cc b/dpid/dpid.cc index 30c2304..d4edff2 100644 --- a/dpid/dpid.cc +++ b/dpid/dpid.cc @@ -54,7 +54,7 @@ char *SharedKey = NULL; */ void cleanup(void) { - std::string fname = dGethomedir() + "/"s + dotDILLO_DPID_COMM_KEYS; + std::string fname = dGethomedir_string().value() + "/"s + dotDILLO_DPID_COMM_KEYS; unlink(fname.c_str()); } diff --git a/src/styleengine.cc b/src/styleengine.cc index 75e0473..35be711 100644 --- a/src/styleengine.cc +++ b/src/styleengine.cc @@ -1069,7 +1069,7 @@ void StyleEngine::init () { void StyleEngine::buildUserStyle () { using namespace std::literals::string_literals; - std::string filename = dGethomedir() + "/.flenser/style.css"s; + std::string filename = dGethomedir_string().value() + "/.flenser/style.css"s; const std::string style= a_Misc_file2dstr(filename.c_str()); if (not style.empty()) {