diff -u -r /home/fornwall/lib/android-ndk/sysroot/usr/include/pwd.h ./usr/include/pwd.h --- /home/fornwall/lib/android-ndk/sysroot/usr/include/pwd.h 2017-06-20 17:41:56.000000000 +0200 +++ ./usr/include/pwd.h 2017-06-26 11:45:26.036064547 +0200 @@ -63,6 +63,8 @@ #include #include +#include /* For realpath() */ + __BEGIN_DECLS struct passwd { @@ -89,7 +91,6 @@ struct passwd* getpwent(void) __INTRODUCED_IN(26); void setpwent(void) __INTRODUCED_IN(26); -void endpwent(void) __INTRODUCED_IN(26); #endif /* __ANDROID_API__ >= 26 */ @@ -99,6 +100,54 @@ int getpwuid_r(uid_t, struct passwd*, char*, size_t, struct passwd**) __INTRODUCED_IN(12); #endif /* __ANDROID_API__ >= 12 */ +int access(const char* __path, int __mode); + +static void android_setup_pwd(struct passwd* pw) { + static char realpath_buffer[4096/*PATH_MAX*/]; + char* result = realpath("/data/data/com.termux/files/home/.termux/shell", realpath_buffer); + if (result == NULL || access(realpath_buffer, /*X_OK*/1) == -1) { + char const* bash_path = "/data/data/com.termux/files/usr/bin/bash"; + if (access(bash_path, /*X_OK*/1) != -1) pw->pw_shell = (char*) bash_path; + else pw->pw_shell = "/data/data/com.termux/files/usr/bin/sh"; + } else { + pw->pw_shell = realpath_buffer; + } + pw->pw_dir = "/data/data/com.termux/files/home"; + pw->pw_passwd = "*"; +#ifdef __LP64__ + pw->pw_gecos = ""; /* Avoid NULL field. */ +#endif +} + +static struct passwd* android_polyfill_getpwuid(uid_t t) { + struct passwd* pw = getpwuid(t); + if (pw == NULL) return NULL; + android_setup_pwd(pw); + return pw; +} + +static struct passwd* android_polyfill_getpwnam(const char* name) { + struct passwd* pw = getpwnam(name); + if (pw == NULL) return NULL; + android_setup_pwd(pw); + return pw; +} + +static int android_polyfill_getpwuid_r(uid_t uid, + struct passwd *pwd, + char *buffer, + size_t bufsize, + struct passwd **result) { + int ret = getpwuid_r(uid, pwd, buffer, bufsize, result); + if (ret != 0) return ret; + android_setup_pwd(pwd); + return 0; +} + +#define getpwnam android_polyfill_getpwnam +#define getpwuid android_polyfill_getpwuid +#define getpwuid_r android_polyfill_getpwuid_r +static void endpwent(void) { /* Do nothing. */ } __END_DECLS