From 43602a291b3ee813cd657a63c1e3ad23733b1596 Mon Sep 17 00:00:00 2001 From: koekeishiya Date: Sat, 13 May 2023 13:47:11 +0200 Subject: change how the users home directory is determined --- src/service.h | 28 +++++++++++++++++++++++----- 1 file 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 \ "\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) -- cgit v1.2.3