diff options
author | koekeishiya <aasvi93@hotmail.com> | 2023-05-13 13:47:11 +0200 |
---|---|---|
committer | koekeishiya <aasvi93@hotmail.com> | 2023-05-13 13:47:11 +0200 |
commit | 43602a291b3ee813cd657a63c1e3ad23733b1596 (patch) | |
tree | d4e15f15ff3bb86882a935d67d7c593214353db2 /src | |
parent | cd9af4ff099c6b7ffa4f9f4b261cc16948245863 (diff) | |
download | skhd-43602a291b3ee813cd657a63c1e3ad23733b1596.tar.gz skhd-43602a291b3ee813cd657a63c1e3ad23733b1596.zip |
change how the users home directory is determined
Diffstat (limited to 'src')
-rw-r--r-- | src/service.h | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/src/service.h b/src/service.h index 2617d06..be79cbb 100644 --- a/src/service.h +++ b/src/service.h @@ -8,7 +8,7 @@ #define _PATH_LAUNCHCTL "/bin/launchctl" #define _NAME_SKHD_PLIST "com.koekeishiya.skhd" -#define _PATH_SKHD_PLIST "/Users/%s/Library/LaunchAgents/"_NAME_SKHD_PLIST".plist" +#define _PATH_SKHD_PLIST "%s/Library/LaunchAgents/"_NAME_SKHD_PLIST".plist" #define _SKHD_PLIST \ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" \ @@ -64,14 +64,32 @@ static int safe_exec(char *const argv[]) } } +static inline char *cfstring_copy(CFStringRef string) +{ + CFIndex num_bytes = CFStringGetMaximumSizeForEncoding(CFStringGetLength(string), kCFStringEncodingUTF8); + char *result = malloc(num_bytes + 1); + if (!result) return NULL; + + if (!CFStringGetCString(string, result, num_bytes + 1, kCFStringEncodingUTF8)) { + free(result); + result = NULL; + } + + return result; +} + +extern CFURLRef CFCopyHomeDirectoryURLForUser(void *user); static void populate_plist_path(char *skhd_plist_path, int size) { - char *user = getenv("USER"); - if (!user) { - error("skhd: 'env USER' not set! abort..\n"); + CFURLRef homeurl_ref = CFCopyHomeDirectoryURLForUser(NULL); + CFStringRef home_ref = homeurl_ref ? CFURLCopyFileSystemPath(homeurl_ref, kCFURLPOSIXPathStyle) : NULL; + char *home = home_ref ? cfstring_copy(home_ref) : NULL; + + if (!home) { + error("skhd: unable to retrieve home directory! abort..\n"); } - snprintf(skhd_plist_path, size, _PATH_SKHD_PLIST, user); + snprintf(skhd_plist_path, size, _PATH_SKHD_PLIST, home); } static void populate_plist(char *skhd_plist, int size) |