Fl_Preferences (X11): Fix detection of preferences directory

- Fix compiler warning [-Wmaybe-uninitialized] for variable home
- Reformat enum 'Root' for better readability
- Add new enum values CORE_SYSTEM_L and CORE_USER_L
- Improve documentation (deprecated and new enums)
- Initialize internal static variable 'filename' which could be used
  uninitialized and thus return any previous value (type == MEMORY)
This commit is contained in:
Albrecht Schlosser 2022-02-03 20:46:13 +01:00
parent 80a22e97e8
commit c483c4c5de
2 changed files with 33 additions and 25 deletions

View File

@ -121,17 +121,20 @@ public:
Define the scope of the preferences.
*/
enum Root {
UNKNOWN_ROOT_TYPE = -1, ///< Returned if storage could not be determined.
SYSTEM = 0, ///< Preferences are used system-wide, deprecated, see SYSTEM_L
USER, ///< Preferences apply only to the current user, deprecated, see USER_L
MEMORY, ///< Returned if querying memory mapped preferences
ROOT_MASK = 0xFF, ///< masks for the values above
CORE = 0x100, ///< OR'd by FLTK to read and write core library preferences and options
CORE_SYSTEM = CORE|SYSTEM,
CORE_USER = CORE|USER,
C_LOCALE = 0x1000, ///< this flag should always be set, it makes sure that floating point values wre writte correctly independently of the current locale
SYSTEM_L = SYSTEM|C_LOCALE, ///< Preferences are used system-wide, locale independent
USER_L = USER|C_LOCALE, ///< Preferences apply only to the current user, locale independent
UNKNOWN_ROOT_TYPE = -1, ///< Returned if storage could not be determined.
SYSTEM = 0, ///< Preferences are used system-wide, deprecated, see SYSTEM_L
USER, ///< Preferences apply only to the current user, deprecated, see USER_L
MEMORY, ///< Returned if querying memory mapped preferences
ROOT_MASK = 0x00FF, ///< mask for the values above
CORE = 0x0100, ///< OR'd by FLTK to read and write core library preferences and options
C_LOCALE = 0x1000, ///< this flag should always be set, it makes sure that floating point
///< values are written correctly independently of the current locale
SYSTEM_L = SYSTEM | C_LOCALE, ///< Preferences are used system-wide, locale independent
USER_L = USER | C_LOCALE, ///< Preferences apply only to the current user, locale independent
CORE_SYSTEM_L = CORE | SYSTEM_L, ///< same as CORE | SYSTEM | C_LOCALE
CORE_USER_L = CORE | USER_L, ///< same as CORE | USER | C_LOCALE
CORE_SYSTEM = CORE | SYSTEM, ///< deprecated, same as CORE | SYSTEM
CORE_USER = CORE | USER, ///< deprecated, same as CORE | USER
};
/**

View File

@ -455,32 +455,37 @@ void Fl_X11_System_Driver::newUUID(char *uuidBuffer)
/*
Note: `prefs` can be NULL!
*/
char *Fl_X11_System_Driver::preference_rootnode(Fl_Preferences * /*prefs*/, Fl_Preferences::Root root, const char *vendor,
char *Fl_X11_System_Driver::preference_rootnode(Fl_Preferences * /*prefs*/,
Fl_Preferences::Root root,
const char *vendor,
const char *application)
{
static char *filename = 0L;
if (!filename) filename = (char*)::calloc(1, FL_PATH_MAX);
const char *home;
switch (root&Fl_Preferences::ROOT_MASK) {
const char *home = "";
int pref_type = root & Fl_Preferences::ROOT_MASK;
switch (pref_type) {
case Fl_Preferences::USER:
home = getenv("HOME");
// make sure that $HOME is set to an existing directory
if ( (home==NULL) || (home[0]==0) || (::access(home, F_OK)==-1) ) {
if ((home == NULL) || (home[0] == 0) || (::access(home, F_OK) == -1)) {
struct passwd *pw = getpwuid(getuid());
home = pw->pw_dir;
if (pw)
home = pw->pw_dir;
}
if ( (home==0L) || (home[0]==0) || (::access(home, F_OK)==-1) ) {
if ((home == 0L) || (home[0] == 0) || (::access(home, F_OK) == -1))
return NULL;
} else {
strlcpy(filename, home, FL_PATH_MAX);
if (filename[strlen(filename)-1] != '/')
strlcat(filename, "/", FL_PATH_MAX);
strlcat(filename, ".fltk/", FL_PATH_MAX);
}
strlcpy(filename, home, FL_PATH_MAX);
if (filename[strlen(filename) - 1] != '/')
strlcat(filename, "/", FL_PATH_MAX);
strlcat(filename, ".fltk/", FL_PATH_MAX);
break;
case Fl_Preferences::SYSTEM:
strcpy(filename, "/etc/fltk/");
break;
default: // MEMORY
filename[0] = '\0'; // empty string
break;
}
// Make sure that the parameters are not NULL
@ -492,8 +497,8 @@ char *Fl_X11_System_Driver::preference_rootnode(Fl_Preferences * /*prefs*/, Fl_P
snprintf(filename + strlen(filename), FL_PATH_MAX - strlen(filename),
"%s/%s.prefs", vendor, application);
// If this is the SYSTEM path, we are done
if ((root&Fl_Preferences::ROOT_MASK)!=Fl_Preferences::USER)
// If this is not the USER path (i.e. SYSTEM or MEMORY), we are done
if ((pref_type) != Fl_Preferences::USER)
return filename;
// If the legacy file exists, we are also done