From 9c5888aa34ec0e99d254e817f71baa6153fa36b7 Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Sat, 4 Jan 2020 00:51:22 +0100 Subject: [PATCH] MacOS: Added the original code to the Fl_Preferences path that would search $HOME first, and only if that fails, we try other ways to find the home directory. This should be highly compatible with what we had first. --- src/Fl_cocoa.mm | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Fl_cocoa.mm b/src/Fl_cocoa.mm index 80022fdd4..cb634a43c 100644 --- a/src/Fl_cocoa.mm +++ b/src/Fl_cocoa.mm @@ -57,6 +57,7 @@ extern "C" { #include #include #include +#include #import @@ -4402,12 +4403,20 @@ char *Fl_Darwin_System_Driver::preference_rootnode(Fl_Preferences *prefs, Fl_Pre break; case Fl_Preferences::USER: { // Find the home directory, but return NULL if components were not found. - // If we ever port this to iOS: this returns tha location of the app! - NSString *nsHome = NSHomeDirectory(); - if (!nsHome) return 0L; - const char *cHome = [nsHome UTF8String]; - if (!cHome) return 0L; - snprintf(filename, FL_PATH_MAX, "%s/Library/Preferences", cHome); + // If we ever port this to iOS: NSHomeDirectory returns tha location of the app! + const char *e = getenv("HOME"); + // if $HOME does not exist, try NSHomeDirectory, the Mac way. + if ( (e==0L) || (e[0]==0) || (::access(e, F_OK)==-1) ) { + NSString *nsHome = NSHomeDirectory(); + if (nsHome) + e = [nsHome UTF8String]; + } + // if NSHomeDirectory does not work, try getpwuid(), the Unix way. + if ( (e==0L) || (e[0]==0) || (::access(e, F_OK)==-1) ) { + struct passwd *pw = getpwuid(getuid()); + e = pw->pw_dir; + } + snprintf(filename, FL_PATH_MAX, "%s/Library/Preferences", e); break; } }