FLTK logo

[master] c483c4c - Fl_Preferences (X11): Fix detection of preferences directory

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[master] c483c4c - Fl_Preferences (X11): Fix detection of preferences directory "Albrecht Schlosser" Feb 03, 2022  
 
commit c483c4c5de8ffa732eac2a8710008a1c67a23955
Author:     Albrecht Schlosser <albrechts.fltk@online.de>
AuthorDate: Thu Feb 3 20:46:13 2022 +0100
Commit:     Albrecht Schlosser <albrechts.fltk@online.de>
CommitDate: Thu Feb 3 20:46:13 2022 +0100

    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)

 FL/Fl_Preferences.H                      | 25 +++++++++++++-----------
 src/drivers/X11/Fl_X11_System_Driver.cxx | 33 ++++++++++++++++++--------------
 2 files changed, 33 insertions(+), 25 deletions(-)

diff --git FL/Fl_Preferences.H FL/Fl_Preferences.H
index 82f3ef4..612827a 100644
--- FL/Fl_Preferences.H
+++ FL/Fl_Preferences.H
@@ -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
   };
 
   /**
diff --git src/drivers/X11/Fl_X11_System_Driver.cxx src/drivers/X11/Fl_X11_System_Driver.cxx
index 64cac37..e62fdf8 100644
--- src/drivers/X11/Fl_X11_System_Driver.cxx
+++ src/drivers/X11/Fl_X11_System_Driver.cxx
@@ -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
Direct Link to Message ]
 
     
Previous Message ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.