macOS: replace BLI_expand_tilde with BLI_dir_home

Use a generic function to access the home directory making macOS path
access match other Unix systems.

When the function was added [0] it solved access to HOME when the
environment variable wasn't set. Since then support for `getpwuid()`
has been added on UNIX systems [1], which also works on macOS,
removing the need for macOS to have a separate function.

Furthermore BLI_expand_tilde had undocumented limitations that didn't
apply to other platforms (see PR for details).

Ref !128734

[0]: 9df13fba69
[1]: 6039cb17e6
This commit is contained in:
Campbell Barton
2024-10-09 09:24:44 +11:00
parent 7c539312b7
commit cd9d4e992d
4 changed files with 6 additions and 37 deletions

View File

@@ -231,8 +231,8 @@ bool BKE_appdir_font_folder_default(char *dir, size_t dir_maxncpy)
BLI_strncpy_wchar_as_utf8(test_dir, wpath, sizeof(test_dir));
}
#elif defined(__APPLE__)
if (const char *fonts_dir = BLI_expand_tilde("~/Library/Fonts")) {
STRNCPY(test_dir, fonts_dir);
if (const char *home_dir = BLI_dir_home()) {
BLI_path_join(test_dir, sizeof(test_dir), home_dir, "Library/Fonts");
}
#else
STRNCPY(test_dir, "/usr/share/fonts");

View File

@@ -440,14 +440,6 @@ void *BLI_file_read_binary_as_mem(const char *filepath, size_t pad_bytes, size_t
*/
void BLI_file_free_lines(struct LinkNode *lines);
#ifdef __APPLE__
/**
* Expand the leading `~` in the given path to `/Users/$USER`.
* This doesn't preserve the trailing path separator.
* Giving a path without leading `~` is not an error.
*/
const char *BLI_expand_tilde(const char *path_with_tilde);
#endif
/* This weirdo pops up in two places. */
#if !defined(WIN32)
# ifndef O_BINARY

View File

@@ -109,21 +109,15 @@ char *BLI_current_working_dir(char *dir, const size_t maxncpy)
const char *BLI_dir_home()
{
const char *home_dir = nullptr;
const char *home_dir;
#ifdef WIN32
home_dir = BLI_getenv("userprofile");
#else
# if defined(__APPLE__)
home_dir = BLI_expand_tilde("~/");
# endif
home_dir = BLI_getenv("HOME");
if (home_dir == nullptr) {
home_dir = BLI_getenv("HOME");
if (home_dir == nullptr) {
if (const passwd *pwuser = getpwuid(getuid())) {
home_dir = pwuser->pw_dir;
}
if (const passwd *pwuser = getpwuid(getuid())) {
home_dir = pwuser->pw_dir;
}
}
#endif

View File

@@ -175,23 +175,6 @@ eFileAttributes BLI_file_attributes(const char *path)
return (eFileAttributes)ret;
}
const char *BLI_expand_tilde(const char *path_with_tilde)
{
static char path_expanded[FILE_MAX];
@autoreleasepool {
NSString *str_with_tilde = [[NSString alloc] initWithCString:path_with_tilde
encoding:NSUTF8StringEncoding];
if (!str_with_tilde) {
return nullptr;
}
NSString *str_expanded = [str_with_tilde stringByExpandingTildeInPath];
[str_expanded getCString:path_expanded
maxLength:sizeof(path_expanded)
encoding:NSUTF8StringEncoding];
}
return path_expanded;
}
char *BLI_current_working_dir(char *dir, const size_t maxncpy)
{
/* Can't just copy to the *dir pointer, as [path getCString gets grumpy. */