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) ...@@ -90,23 +90,17 @@ girara_xdg_open(const char* uri)
return girara_xdg_open_with_working_directory(uri, NULL); return girara_xdg_open_with_working_directory(uri, NULL);
} }
char* #if defined(HAVE_GETPWNAM_R)
girara_get_home_directory(const char* user) 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 #ifdef _SC_GETPW_R_SIZE_MAX
int bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); int bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
if (bufsize < 0) { if (bufsize < 0) {
bufsize = 4096; bufsize = 4096;
} }
#else #else
int bufsize = 4096; const int bufsize = 4096;
#endif #endif
char* buffer = g_try_malloc0(sizeof(char) * bufsize); char* buffer = g_try_malloc0(sizeof(char) * bufsize);
...@@ -114,8 +108,9 @@ girara_get_home_directory(const char* user) ...@@ -114,8 +108,9 @@ girara_get_home_directory(const char* user)
return NULL; return NULL;
} }
getpwnam_r(user, &pwd, buffer, bufsize, &result); struct passwd pwd;
if (result == NULL) { struct passwd* result = NULL;
if (getpwnam_r(user, &pwd, buffer, bufsize, &result) != 0) {
g_free(buffer); g_free(buffer);
return NULL; return NULL;
} }
...@@ -124,6 +119,28 @@ girara_get_home_directory(const char* user) ...@@ -124,6 +119,28 @@ girara_get_home_directory(const char* user)
g_free(buffer); g_free(buffer);
return dir; 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* char*
girara_get_xdg_path(girara_xdg_path_t path) girara_get_xdg_path(girara_xdg_path_t path)
......
...@@ -38,6 +38,9 @@ pango = dependency('pango', version: '>=1.14') ...@@ -38,6 +38,9 @@ pango = dependency('pango', version: '>=1.14')
build_dependencies = [libm, glib, gtk3, pango] build_dependencies = [libm, glib, gtk3, pango]
pc_requires = ['glib-2.0', 'gtk+-3.0'] 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
defines = [ defines = [
'-DGETTEXT_PACKAGE="libgirara-gtk3-@0@"'.format(so_major), '-DGETTEXT_PACKAGE="libgirara-gtk3-@0@"'.format(so_major),
...@@ -45,6 +48,10 @@ defines = [ ...@@ -45,6 +48,10 @@ defines = [
'-D_DEFAULT_SOURCE', '-D_DEFAULT_SOURCE',
] ]
if has_getpwnam_r
defines += ['-DHAVE_GETPWNAM_R']
endif
# compile flags # compile flags
flags = [ flags = [
'-Wall', '-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