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