[master] 4659822 - Have Fl_X11_System_Driver::newUUID() use uuid_generate() if available at run-time.

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] 4659822 - Have Fl_X11_System_Driver::newUUID() use uuid_generate() if available at run-time. "ManoloFLTK" 03:32 Oct 02 top right image
 
commit 46598229a9605b25e3da5e0d7ad41343cf429497
Author:     ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>
AuthorDate: Fri Oct 2 12:18:08 2020 +0200
Commit:     ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>
CommitDate: Fri Oct 2 12:18:18 2020 +0200

    Have Fl_X11_System_Driver::newUUID() use uuid_generate() if available at run-time.
    
    The code falls back to the ad-hoc method if uuid_generate() isn't available.

 src/drivers/Posix/Fl_Posix_System_Driver.cxx | 10 ++-
 src/drivers/X11/Fl_X11_System_Driver.cxx     | 98 +++++++++++++++++-----------
 2 files changed, 66 insertions(+), 42 deletions(-)

diff --git src/drivers/Posix/Fl_Posix_System_Driver.cxx src/drivers/Posix/Fl_Posix_System_Driver.cxx
index 0fa037b..a05781d 100644
--- src/drivers/Posix/Fl_Posix_System_Driver.cxx
+++ src/drivers/Posix/Fl_Posix_System_Driver.cxx
@@ -47,13 +47,17 @@
 
 
 #if HAVE_DLFCN_H
-static void* double_dlopen(const char *filename1)
+static void* triple_dlopen(const char *filename1)
 {
   void *ptr = ::dlopen(filename1, RTLD_LAZY | RTLD_GLOBAL);
   if (!ptr) {
     char filename2[FL_PATH_MAX];
-    sprintf(filename2, "%s.0", filename1);
+    sprintf(filename2, "%s.1", filename1);
     ptr = dlopen(filename2, RTLD_LAZY | RTLD_GLOBAL);
+    if (!ptr) {
+      sprintf(filename2, "%s.0", filename1);
+      ptr = dlopen(filename2, RTLD_LAZY | RTLD_GLOBAL);
+    }
   }
   return ptr;
 }
@@ -63,7 +67,7 @@ void *Fl_Posix_System_Driver::dlopen(const char *filename)
 {
   void *ptr = NULL;
 #if HAVE_DLFCN_H
-  ptr = double_dlopen(filename);
+  ptr = triple_dlopen(filename);
 #  ifdef __APPLE_CC__ // allows testing on Darwin + XQuartz + fink
   if (!ptr) {
     char *f_dylib = (char*)malloc(strlen(filename)+7);
diff --git src/drivers/X11/Fl_X11_System_Driver.cxx src/drivers/X11/Fl_X11_System_Driver.cxx
index 6dcaee0..c99f31f 100644
--- src/drivers/X11/Fl_X11_System_Driver.cxx
+++ src/drivers/X11/Fl_X11_System_Driver.cxx
@@ -28,6 +28,10 @@
 #include <pwd.h>
 #include <string.h>     // strerror(errno)
 #include <errno.h>      // errno
+#if HAVE_DLSYM && HAVE_DLFCN_H
+#include <dlfcn.h>   // for dlsym
+#endif
+
 
 #if defined(_AIX)
 extern "C" {
@@ -354,46 +358,65 @@ int Fl_X11_System_Driver::file_browser_load_filesystem(Fl_File_Browser *browser,
 
 void Fl_X11_System_Driver::newUUID(char *uuidBuffer)
 {
-  // warning Unix implementation of Fl_Preferences::newUUID() incomplete!
-  // #include <uuid/uuid.h>
-  // void uuid_generate(uuid_t out);
   unsigned char b[16];
-  time_t t = time(0);                   // first 4 byte
-  b[0] = (unsigned char)t;
-  b[1] = (unsigned char)(t>>8);
-  b[2] = (unsigned char)(t>>16);
-  b[3] = (unsigned char)(t>>24);
-  int r = rand();                       // four more bytes
-  b[4] = (unsigned char)r;
-  b[5] = (unsigned char)(r>>8);
-  b[6] = (unsigned char)(r>>16);
-  b[7] = (unsigned char)(r>>24);
-  unsigned long a = (unsigned long)&t;  // four more bytes
-  b[8] = (unsigned char)a;
-  b[9] = (unsigned char)(a>>8);
-  b[10] = (unsigned char)(a>>16);
-  b[11] = (unsigned char)(a>>24);
-  // Now we try to find 4 more "random" bytes. We extract the
-  // lower 4 bytes from the address of t - it is created on the
-  // stack so *might* be in a different place each time...
-  // This is now done via a union to make it compile OK on 64-bit systems.
-  union { void *pv; unsigned char a[sizeof(void*)]; } v;
-  v.pv = (void *)(&t);
-  // NOTE: May need to handle big- or little-endian systems here
+#if HAVE_DLSYM && HAVE_DLFCN_H
+  typedef void (*gener_f_type)(uchar*);
+  static bool looked_for_uuid_generate = false;
+  static gener_f_type uuid_generate_f = NULL;
+  if (!looked_for_uuid_generate) {
+    looked_for_uuid_generate = true;
+#  ifdef RTLD_DEFAULT
+    uuid_generate_f = (gener_f_type)dlsym(RTLD_DEFAULT, "uuid_generate");
+#  endif
+    if (!uuid_generate_f) {
+      void *libuuid = this->dlopen("libuuid.so");
+      if (libuuid) {
+        uuid_generate_f = (gener_f_type)dlsym(libuuid, "uuid_generate");
+      }
+    }
+  }
+  if (uuid_generate_f) {
+    uuid_generate_f(b);
+  } else
+#endif
+  {
+    time_t t = time(0);                   // first 4 byte
+    b[0] = (unsigned char)t;
+    b[1] = (unsigned char)(t>>8);
+    b[2] = (unsigned char)(t>>16);
+    b[3] = (unsigned char)(t>>24);
+    int r = rand();                       // four more bytes
+    b[4] = (unsigned char)r;
+    b[5] = (unsigned char)(r>>8);
+    b[6] = (unsigned char)(r>>16);
+    b[7] = (unsigned char)(r>>24);
+    unsigned long a = (unsigned long)&t;  // four more bytes
+    b[8] = (unsigned char)a;
+    b[9] = (unsigned char)(a>>8);
+    b[10] = (unsigned char)(a>>16);
+    b[11] = (unsigned char)(a>>24);
+    // Now we try to find 4 more "random" bytes. We extract the
+    // lower 4 bytes from the address of t - it is created on the
+    // stack so *might* be in a different place each time...
+    // This is now done via a union to make it compile OK on 64-bit systems.
+    union { void *pv; unsigned char a[sizeof(void*)]; } v;
+    v.pv = (void *)(&t);
+    // NOTE: May need to handle big- or little-endian systems here
 # if WORDS_BIGENDIAN
-  b[8] = v.a[sizeof(void*) - 1];
-  b[9] = v.a[sizeof(void*) - 2];
-  b[10] = v.a[sizeof(void*) - 3];
-  b[11] = v.a[sizeof(void*) - 4];
+    b[8] = v.a[sizeof(void*) - 1];
+    b[9] = v.a[sizeof(void*) - 2];
+    b[10] = v.a[sizeof(void*) - 3];
+    b[11] = v.a[sizeof(void*) - 4];
 # else // data ordered for a little-endian system
-  b[8] = v.a[0];
-  b[9] = v.a[1];
-  b[10] = v.a[2];
-  b[11] = v.a[3];
+    b[8] = v.a[0];
+    b[9] = v.a[1];
+    b[10] = v.a[2];
+    b[11] = v.a[3];
 # endif
-  char name[80];                        // last four bytes
-  gethostname(name, 79);
-  memcpy(b+12, name, 4);
+    char name[80];                        // last four bytes
+    gethostname(name, 79);
+    memcpy(b+12, name, 4);
+  }
   sprintf(uuidBuffer, "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
           b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
           b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]);
@@ -549,9 +572,6 @@ int Fl_X11_System_Driver::utf8locale() {
   return ret;
 }
 
-#if HAVE_DLSYM && HAVE_DLFCN_H
-#include <dlfcn.h>   // for dlopen et al
-#endif
 #if HAVE_DLSYM && HAVE_DLFCN_H && defined(RTLD_DEFAULT)
 
 bool Fl_X11_System_Driver::probe_for_GTK(int major, int minor, void **ptr_gtk) {
Direct Link to Message ]
 
bottom left image   bottom right image
Previous Message ]Next Message ]
 
 

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