psmisc: Fix build with unified headers
[termux-packages] / ndk_patches_unified / pwd.h.patch
CommitLineData
29bf3001 1diff -u -r /home/fornwall/lib/android-ndk/sysroot/usr/include/pwd.h ./usr/include/pwd.h
6e4505aa
FF
2--- /home/fornwall/lib/android-ndk/sysroot/usr/include/pwd.h 2017-06-20 17:41:56.000000000 +0200
3+++ ./usr/include/pwd.h 2017-06-26 11:45:26.036064547 +0200
4@@ -63,6 +63,8 @@
5 #include <sys/cdefs.h>
6 #include <sys/types.h>
7
8+#include <stdlib.h> /* For realpath() */
9+
10 __BEGIN_DECLS
11
12 struct passwd {
13@@ -89,7 +91,6 @@
29bf3001
FF
14 struct passwd* getpwent(void) __INTRODUCED_IN(26);
15
16 void setpwent(void) __INTRODUCED_IN(26);
17-void endpwent(void) __INTRODUCED_IN(26);
18 #endif /* __ANDROID_API__ >= 26 */
19
20
6e4505aa 21@@ -99,6 +100,52 @@
29bf3001
FF
22 int getpwuid_r(uid_t, struct passwd*, char*, size_t, struct passwd**) __INTRODUCED_IN(12);
23 #endif /* __ANDROID_API__ >= 12 */
24
25+static void android_setup_pwd(struct passwd* pw) {
26+ static char realpath_buffer[4096/*PATH_MAX*/];
6e4505aa
FF
27+ char* result = realpath("/data/data/com.termux/files/home/.termux/shell", realpath_buffer);
28+ if (result == NULL || access(realpath_buffer, /*X_OK*/1) == -1) {
29+ char const* bash_path = "/data/data/com.termux/files/usr/bin/bash";
30+ if (access(bash_path, /*X_OK*/1) != -1) pw->pw_shell = (char*) bash_path;
31+ else pw->pw_shell = "/data/data/com.termux/files/usr/bin/sh";
29bf3001
FF
32+ } else {
33+ pw->pw_shell = realpath_buffer;
34+ }
6e4505aa 35+ pw->pw_dir = "/data/data/com.termux/files/home";
29bf3001
FF
36+ pw->pw_passwd = "*";
37+#ifdef __LP64__
38+ pw->pw_gecos = ""; /* Avoid NULL field. */
39+#endif
40+}
41+
42+static struct passwd* android_polyfill_getpwuid(uid_t t) {
43+ struct passwd* pw = getpwuid(t);
44+ if (pw == NULL) return NULL;
45+ android_setup_pwd(pw);
46+ return pw;
47+}
48+
49+static struct passwd* android_polyfill_getpwnam(const char* name) {
50+ struct passwd* pw = getpwnam(name);
51+ if (pw == NULL) return NULL;
52+ android_setup_pwd(pw);
53+ return pw;
54+}
55+
56+static int android_polyfill_getpwuid_r(uid_t uid,
57+ struct passwd *pwd,
58+ char *buffer,
59+ size_t bufsize,
60+ struct passwd **result) {
61+ int ret = getpwuid_r(uid, pwd, buffer, bufsize, result);
62+ if (ret != 0) return ret;
63+ android_setup_pwd(pwd);
64+ return 0;
65+}
66+
67+#define getpwnam android_polyfill_getpwnam
68+#define getpwuid android_polyfill_getpwuid
69+#define getpwuid_r android_polyfill_getpwuid_r
70+static void endpwent(void) { /* Do nothing. */ }
71
72 __END_DECLS
73