diff options
| author | Matthias Melcher <git@matthiasm.com> | 2020-01-04 00:51:22 +0100 |
|---|---|---|
| committer | Matthias Melcher <git@matthiasm.com> | 2020-01-04 00:51:22 +0100 |
| commit | 9c5888aa34ec0e99d254e817f71baa6153fa36b7 (patch) | |
| tree | 25fe430ddcf547cf43e41dddc1889200c93e0717 /src | |
| parent | ebec2629a2ca87349c5fb52c1b7c38583447fd29 (diff) | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Fl_cocoa.mm | 21 |
1 files 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 <limits.h> #include <dlfcn.h> #include <string.h> +#include <pwd.h> #import <Cocoa/Cocoa.h> @@ -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; } } |
