Commit 73b99a13 authored by Sebastian Ramacher's avatar Sebastian Ramacher

Implement fallback to getpwmnam if getpwmnam_r is not available

parent e736934b
......@@ -90,23 +90,17 @@ girara_xdg_open(const char* uri)
return girara_xdg_open_with_working_directory(uri, NULL);
}
char*
girara_get_home_directory(const char* user)
#if defined(HAVE_GETPWNAM_R)
static char*
get_home_directory_getpwnam(const char* user)
{
if (user == NULL || g_strcmp0(user, g_get_user_name()) == 0) {
return g_strdup(g_get_home_dir());
}
// XXX: The following code is very unportable.
struct passwd pwd;
struct passwd* result = NULL;
#ifdef _SC_GETPW_R_SIZE_MAX
int bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
if (bufsize < 0) {
bufsize = 4096;
}
#else
int bufsize = 4096;
const int bufsize = 4096;
#endif
char* buffer = g_try_malloc0(sizeof(char) * bufsize);
......@@ -114,8 +108,9 @@ girara_get_home_directory(const char* user)
return NULL;
}
getpwnam_r(user, &pwd, buffer, bufsize, &result);
if (result == NULL) {
struct passwd pwd;
struct passwd* result = NULL;
if (getpwnam_r(user, &pwd, buffer, bufsize, &result) != 0) {
g_free(buffer);
return NULL;
}
......@@ -124,6 +119,28 @@ girara_get_home_directory(const char* user)
g_free(buffer);
return dir;
}
#else
static char*
get_home_directory_getpwnam(const char* user)
{
const struct passwd* pwd = getpwnam(user);
if (pwd != NULL) {
return g_strdup(pwd->pw_dir);
}
return NULL;
}
#endif
char*
girara_get_home_directory(const char* user)
{
if (user == NULL || g_strcmp0(user, g_get_user_name()) == 0) {
return g_strdup(g_get_home_dir());
}
return get_home_directory_getpwnam(user);
}
char*
girara_get_xdg_path(girara_xdg_path_t path)
......
......@@ -38,6 +38,9 @@ pango = dependency('pango', version: '>=1.14')
build_dependencies = [libm, glib, gtk3, pango]
pc_requires = ['glib-2.0', 'gtk+-3.0']
# supported functions
has_getpwnam_r = cc.has_function('getpwnam_r', prefix: '#define _DEFAULT_SOURCE\n#include <sys/types.h>\n#include <pwd.h>')
# defines
defines = [
'-DGETTEXT_PACKAGE="libgirara-gtk3-@0@"'.format(so_major),
......@@ -45,6 +48,10 @@ defines = [
'-D_DEFAULT_SOURCE',
]
if has_getpwnam_r
defines += ['-DHAVE_GETPWNAM_R']
endif
# compile flags
flags = [
'-Wall',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment