diff options
| author | maxim nikonov <maxim.nikonov@hqo.co> | 2026-02-05 21:32:25 +0500 |
|---|---|---|
| committer | maxim nikonov <maxim.nikonov@hqo.co> | 2026-02-05 21:32:25 +0500 |
| commit | dc39575fb3ef90e5a2689babe7fb335cd88f6727 (patch) | |
| tree | 24f6cef8f2b558ae6f1f812c75be0c09a53fe417 /src/drivers/Unix | |
| parent | 7d3793ce1d8cb26e7608bf859beca21359cec6e9 (diff) | |
wip
Diffstat (limited to 'src/drivers/Unix')
| -rw-r--r-- | src/drivers/Unix/Fl_Unix_System_Driver.cxx | 89 |
1 files changed, 57 insertions, 32 deletions
diff --git a/src/drivers/Unix/Fl_Unix_System_Driver.cxx b/src/drivers/Unix/Fl_Unix_System_Driver.cxx index 443838c5d..ab6f678aa 100644 --- a/src/drivers/Unix/Fl_Unix_System_Driver.cxx +++ b/src/drivers/Unix/Fl_Unix_System_Driver.cxx @@ -30,7 +30,6 @@ #include <pwd.h> #include <string.h> // strerror(errno) #include <errno.h> // errno -#include <string> #if HAVE_DLSYM && HAVE_DLFCN_H #include <dlfcn.h> // for dlsym #endif @@ -537,53 +536,79 @@ char *Fl_Unix_System_Driver::preference_user_rootnode( const char *application, char *buffer) { + char home_path[FL_PATH_MAX]; + char prefs_path_14[FL_PATH_MAX]; + char prefs_path_13[FL_PATH_MAX]; + char *p; + int len; + // Find the path to the user's home directory. const char *home_path_c = getenv("HOME"); - std::string home_path = home_path_c ? home_path_c : ""; - if (home_path.empty()) { + if (home_path_c && home_path_c[0]) { + strlcpy(home_path, home_path_c, FL_PATH_MAX); + } else { + home_path[0] = '\0'; struct passwd *pw = getpwuid(getuid()); - if (pw) - home_path = pw->pw_dir; + if (pw && pw->pw_dir) + strlcpy(home_path, pw->pw_dir, FL_PATH_MAX); } // 1: Generate the 1.4 path for this vendor and application. const char *prefs_path_14_c = getenv("XDG_CONFIG_HOME"); - std::string prefs_path_14 = prefs_path_14_c ? prefs_path_14_c : ""; - if (prefs_path_14.empty()) { - prefs_path_14 = home_path + "/.config"; + if (!prefs_path_14_c || !prefs_path_14_c[0]) { + snprintf(prefs_path_14, FL_PATH_MAX, "%s/.config", home_path); } else { - if (prefs_path_14[prefs_path_14.size()-1]!='/') - prefs_path_14.append("/"); - if (prefs_path_14.find("~/")==0) // starts with "~" - prefs_path_14.replace(0, 1, home_path); - int h_env = prefs_path_14.find("${HOME}"); - if (h_env!=(int)prefs_path_14.npos) - prefs_path_14.replace(h_env, 7, home_path); - h_env = prefs_path_14.find("$HOME/"); - if (h_env!=(int)prefs_path_14.npos) - prefs_path_14.replace(h_env, 5, home_path); + strlcpy(prefs_path_14, prefs_path_14_c, FL_PATH_MAX); + len = (int)strlen(prefs_path_14); + if (len > 0 && prefs_path_14[len-1] != '/') { + strlcat(prefs_path_14, "/", FL_PATH_MAX); + len++; + } + // Handle ~/ at start + if (prefs_path_14[0] == '~' && prefs_path_14[1] == '/') { + char temp[FL_PATH_MAX]; + snprintf(temp, FL_PATH_MAX, "%s%s", home_path, prefs_path_14 + 1); + strlcpy(prefs_path_14, temp, FL_PATH_MAX); + } + // Handle ${HOME} + p = strstr(prefs_path_14, "${HOME}"); + if (p) { + char temp[FL_PATH_MAX]; + *p = '\0'; + snprintf(temp, FL_PATH_MAX, "%s%s%s", prefs_path_14, home_path, p + 7); + strlcpy(prefs_path_14, temp, FL_PATH_MAX); + } + // Handle $HOME/ + p = strstr(prefs_path_14, "$HOME/"); + if (p) { + char temp[FL_PATH_MAX]; + *p = '\0'; + snprintf(temp, FL_PATH_MAX, "%s%s%s", prefs_path_14, home_path, p + 5); + strlcpy(prefs_path_14, temp, FL_PATH_MAX); + } } - if (prefs_path_14[prefs_path_14.size()-1]!='/') - prefs_path_14.append("/"); - prefs_path_14.append(vendor); + len = (int)strlen(prefs_path_14); + if (len > 0 && prefs_path_14[len-1] != '/') + strlcat(prefs_path_14, "/", FL_PATH_MAX); + strlcat(prefs_path_14, vendor, FL_PATH_MAX); // 2: If this base path does not exist, try the 1.3 path - if (::access(prefs_path_14.c_str(), F_OK) == -1) { - std::string prefs_path_13 = home_path + "/.fltk/" + vendor; - if (::access(prefs_path_13.c_str(), F_OK) == 0) { - prefs_path_13.append("/"); - prefs_path_13.append(application); - prefs_path_13.append(".prefs"); - strlcpy(buffer, prefs_path_13.c_str(), FL_PATH_MAX); + if (::access(prefs_path_14, F_OK) == -1) { + snprintf(prefs_path_13, FL_PATH_MAX, "%s/.fltk/%s", home_path, vendor); + if (::access(prefs_path_13, F_OK) == 0) { + strlcat(prefs_path_13, "/", FL_PATH_MAX); + strlcat(prefs_path_13, application, FL_PATH_MAX); + strlcat(prefs_path_13, ".prefs", FL_PATH_MAX); + strlcpy(buffer, prefs_path_13, FL_PATH_MAX); return buffer; } } // 3: neither path exists, return the 1.4 file path and name - prefs_path_14.append("/"); - prefs_path_14.append(application); - prefs_path_14.append(".prefs"); - strlcpy(buffer, prefs_path_14.c_str(), FL_PATH_MAX); + strlcat(prefs_path_14, "/", FL_PATH_MAX); + strlcat(prefs_path_14, application, FL_PATH_MAX); + strlcat(prefs_path_14, ".prefs", FL_PATH_MAX); + strlcpy(buffer, prefs_path_14, FL_PATH_MAX); return buffer; } |
