#include <stddef.h>
+#include <string.h> /* For strcpy(3) used by ctermid() */
-+#include <fcntl.h> /* For O_RDWR and other O_* constants */
-+#include <stdlib.h> /* For random() */
++#include <asm-generic/fcntl.h> /* For O_RDWR and other O_* constants */
++#include <stdlib.h> /* For arc4random() */
+
#define __need_NULL
#include <stddef.h>
+
+/* Used by perl, fish, and others */
+static char* ctermid(char* s) {
-+ if (s == 0) return "/dev/tty";
++ if (s == 0) return (char*) "/dev/tty";
+ strcpy(s, "/dev/tty");
+ return s;
+}
#if defined(__BIONIC_FORTIFY)
__BEGIN_DECLS
-@@ -462,4 +474,26 @@
+@@ -462,4 +474,29 @@
#endif /* defined(__BIONIC_FORTIFY) */
+__BEGIN_DECLS
+
++extern int open(const char*, int, ...);
++extern pid_t getpid();
++extern int unlink(const char*);
+static FILE* tmpfile() {
+ int p = getpid();
+ char* path;
+ int i;
+ for (i = 0; i < 100; i++) {
-+ long int r = random();
-+ if (asprintf(&path, "@TERMUX_PREFIX@/tmp/tmpfile.%d-%l", p, r) == -1) return NULL;
++ unsigned int r = arc4random();
++ if (asprintf(&path, "@TERMUX_PREFIX@/tmp/tmpfile.%d-%u", p, r) == -1) return NULL;
+ int fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE);
+ free(path);
+ if (fd >= 0) {