Switch over to building with unified headers
[termux-packages] / ndk-patches / pwd.h.patch
1 diff -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-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 @@
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
21 @@ -99,6 +100,54 @@
22 int getpwuid_r(uid_t, struct passwd*, char*, size_t, struct passwd**) __INTRODUCED_IN(12);
23 #endif /* __ANDROID_API__ >= 12 */
24
25 +int access(const char* __path, int __mode);
26 +
27 +static void android_setup_pwd(struct passwd* pw) {
28 + static char realpath_buffer[4096/*PATH_MAX*/];
29 + char* result = realpath("/data/data/com.termux/files/home/.termux/shell", realpath_buffer);
30 + if (result == NULL || access(realpath_buffer, /*X_OK*/1) == -1) {
31 + char const* bash_path = "/data/data/com.termux/files/usr/bin/bash";
32 + if (access(bash_path, /*X_OK*/1) != -1) pw->pw_shell = (char*) bash_path;
33 + else pw->pw_shell = "/data/data/com.termux/files/usr/bin/sh";
34 + } else {
35 + pw->pw_shell = realpath_buffer;
36 + }
37 + pw->pw_dir = "/data/data/com.termux/files/home";
38 + pw->pw_passwd = "*";
39 +#ifdef __LP64__
40 + pw->pw_gecos = ""; /* Avoid NULL field. */
41 +#endif
42 +}
43 +
44 +static struct passwd* android_polyfill_getpwuid(uid_t t) {
45 + struct passwd* pw = getpwuid(t);
46 + if (pw == NULL) return NULL;
47 + android_setup_pwd(pw);
48 + return pw;
49 +}
50 +
51 +static struct passwd* android_polyfill_getpwnam(const char* name) {
52 + struct passwd* pw = getpwnam(name);
53 + if (pw == NULL) return NULL;
54 + android_setup_pwd(pw);
55 + return pw;
56 +}
57 +
58 +static int android_polyfill_getpwuid_r(uid_t uid,
59 + struct passwd *pwd,
60 + char *buffer,
61 + size_t bufsize,
62 + struct passwd **result) {
63 + int ret = getpwuid_r(uid, pwd, buffer, bufsize, result);
64 + if (ret != 0) return ret;
65 + android_setup_pwd(pwd);
66 + return 0;
67 +}
68 +
69 +#define getpwnam android_polyfill_getpwnam
70 +#define getpwuid android_polyfill_getpwuid
71 +#define getpwuid_r android_polyfill_getpwuid_r
72 +static void endpwent(void) { /* Do nothing. */ }
73
74 __END_DECLS
75