ndk_patches: ttyname(_r) works in Android 5.0+

This commit is contained in:
Fredrik Fornwall 2016-04-20 15:06:53 -04:00
parent 1ea423b5a3
commit fcc35ab886

View File

@ -1,55 +0,0 @@
diff -u -r /home/fornwall/lib/android-ndk/platforms/android-19/arch-arm/usr/include/unistd.h ./usr/include/unistd.h
--- /home/fornwall/lib/android-ndk/platforms/android-19/arch-arm/usr/include/unistd.h 2012-08-21 07:23:12.000000000 +0200
+++ ./usr/include/unistd.h 2014-07-09 09:55:58.443639544 +0200
@@ -35,6 +35,7 @@
#include <sys/sysconf.h>
#include <linux/capability.h>
#include <pathconf.h>
+#include <asm/unistd.h>
__BEGIN_DECLS
@@ -166,6 +174,43 @@
extern char* ttyname(int);
extern int ttyname_r(int, char*, size_t);
+/* start android polyfill of ttyname(3) and ttyname_r(3) */
+#define ttyname(a) android_polyfill_ttyname(a)
+#define ttyname_r(a, b, c) android_polyfill_ttyname_r(a, b, c)
+
+static int android_polyfill_ttyname_r(int fd, char* buf, size_t buflen)
+{
+ char symlink_path[32] = "/proc/self/fd/";
+ ssize_t siz;
+
+ if (!isatty(fd)) return -1;
+
+ /* Would like to do sprintf(symlink_path, "/proc/self/fd/%d", fd), but stdio.h may not be included. */
+ int shifter = fd;
+ char* p = symlink_path + 14;
+ do {
+ p++;
+ shifter = shifter / 10;
+ } while(shifter);
+ *p = '\0';
+ do {
+ *--p = (fd % 10) + '0';
+ fd = fd / 10;
+ } while (fd);
+
+ siz = readlink(symlink_path, buf, buflen);
+ if (siz < 0 || siz == buflen) return -1;
+ buf[siz] = '\0';
+ return 0;
+}
+
+static char* android_polyfill_ttyname(int fd)
+{
+ static char buf[32];
+ return (ttyname_r(fd, buf, sizeof(buf)) == 0) ? buf : NULL;
+}
+/* end android polyfill of ttyname(3) and ttyname_r(3) */
+
extern int acct(const char* filepath);
static __inline__ int getpagesize(void) {