Merge "libc: Add missing C++ guards to <pathconf.h>" into gingerbread
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index f66ff40..4bcccb5 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -112,6 +112,7 @@
 int         fchmod(int, mode_t)  94
 int         dup(int)  41
 int         pipe(int *)  42,42,-1
+int         pipe2(int *, int) 359,331
 int         dup2(int, int)   63
 int         select:_newselect(int, struct fd_set *, struct fd_set *, struct fd_set *, struct timeval *)  142
 int         ftruncate(int, off_t)  93
@@ -229,8 +230,8 @@
 int sched_rr_get_interval(pid_t pid, struct timespec *interval)  161
 
 # io priorities
-int ioprio_set(int which, int who, int ioprio) 314,289
-int ioprio_get(int which, int who) 315,290
+int ioprio_set(int which, int who, int ioprio) 314,289,288
+int ioprio_get(int which, int who) 315,290,289
 
 # other
 int     uname(struct utsname *)  122
diff --git a/libc/arch-arm/include/machine/_types.h b/libc/arch-arm/include/machine/_types.h
index 6d10e12..3e779ca 100644
--- a/libc/arch-arm/include/machine/_types.h
+++ b/libc/arch-arm/include/machine/_types.h
@@ -46,8 +46,6 @@
 typedef long           ptrdiff_t;
 #endif
 
-//#include <linux/types.h>
-
 /* 7.18.1.1 Exact-width integer types */
 typedef	__signed char		__int8_t;
 typedef	unsigned char		__uint8_t;
diff --git a/libc/arch-arm/syscalls.mk b/libc/arch-arm/syscalls.mk
index c466166..2d944ca 100644
--- a/libc/arch-arm/syscalls.mk
+++ b/libc/arch-arm/syscalls.mk
@@ -66,6 +66,7 @@
 syscall_src += arch-arm/syscalls/fchmod.S
 syscall_src += arch-arm/syscalls/dup.S
 syscall_src += arch-arm/syscalls/pipe.S
+syscall_src += arch-arm/syscalls/pipe2.S
 syscall_src += arch-arm/syscalls/dup2.S
 syscall_src += arch-arm/syscalls/select.S
 syscall_src += arch-arm/syscalls/ftruncate.S
diff --git a/libc/arch-arm/syscalls/pipe2.S b/libc/arch-arm/syscalls/pipe2.S
new file mode 100644
index 0000000..df77094
--- /dev/null
+++ b/libc/arch-arm/syscalls/pipe2.S
@@ -0,0 +1,19 @@
+/* autogenerated by gensyscalls.py */
+#include <sys/linux-syscalls.h>
+
+    .text
+    .type pipe2, #function
+    .globl pipe2
+    .align 4
+    .fnstart
+
+pipe2:
+    .save   {r4, r7}
+    stmfd   sp!, {r4, r7}
+    ldr     r7, =__NR_pipe2
+    swi     #0
+    ldmfd   sp!, {r4, r7}
+    movs    r0, r0
+    bxpl    lr
+    b       __set_syscall_errno
+    .fnend
diff --git a/libc/arch-sh/syscalls.mk b/libc/arch-sh/syscalls.mk
index ddae8d3..b690bbf 100644
--- a/libc/arch-sh/syscalls.mk
+++ b/libc/arch-sh/syscalls.mk
@@ -69,6 +69,7 @@
 syscall_src += arch-sh/syscalls/flock.S
 syscall_src += arch-sh/syscalls/fchmod.S
 syscall_src += arch-sh/syscalls/dup.S
+syscall_src += arch-sh/syscalls/pipe2.S
 syscall_src += arch-sh/syscalls/dup2.S
 syscall_src += arch-sh/syscalls/select.S
 syscall_src += arch-sh/syscalls/ftruncate.S
diff --git a/libc/arch-sh/syscalls/pipe2.S b/libc/arch-sh/syscalls/pipe2.S
new file mode 100644
index 0000000..f6d9964
--- /dev/null
+++ b/libc/arch-sh/syscalls/pipe2.S
@@ -0,0 +1,32 @@
+/* autogenerated by gensyscalls.py */
+#include <sys/linux-syscalls.h>
+
+    .text
+    .type pipe2, @function
+    .globl pipe2
+    .align 4
+
+pipe2:
+
+    /* invoke trap */
+    mov.l   0f, r3  /* trap num */
+    trapa   #(2 + 0x10)
+
+    /* check return value */
+    cmp/pz  r0
+    bt      __NR_pipe2_end
+
+    /* keep error number */
+    sts.l   pr, @-r15
+    mov.l   1f, r1
+    jsr     @r1
+    mov     r0, r4
+    lds.l   @r15+, pr
+
+__NR_pipe2_end:
+    rts
+    nop
+
+    .align  2
+0:  .long   __NR_pipe2
+1:  .long   __set_syscall_errno
diff --git a/libc/arch-x86/syscalls.mk b/libc/arch-x86/syscalls.mk
index 6ac3716..27153f9 100644
--- a/libc/arch-x86/syscalls.mk
+++ b/libc/arch-x86/syscalls.mk
@@ -69,6 +69,7 @@
 syscall_src += arch-x86/syscalls/fchmod.S
 syscall_src += arch-x86/syscalls/dup.S
 syscall_src += arch-x86/syscalls/pipe.S
+syscall_src += arch-x86/syscalls/pipe2.S
 syscall_src += arch-x86/syscalls/dup2.S
 syscall_src += arch-x86/syscalls/select.S
 syscall_src += arch-x86/syscalls/ftruncate.S
diff --git a/libc/arch-x86/syscalls/pipe2.S b/libc/arch-x86/syscalls/pipe2.S
new file mode 100644
index 0000000..c3354a7
--- /dev/null
+++ b/libc/arch-x86/syscalls/pipe2.S
@@ -0,0 +1,26 @@
+/* autogenerated by gensyscalls.py */
+#include <sys/linux-syscalls.h>
+
+    .text
+    .type pipe2, @function
+    .globl pipe2
+    .align 4
+
+pipe2:
+    pushl   %ebx
+    pushl   %ecx
+    mov     12(%esp), %ebx
+    mov     16(%esp), %ecx
+    movl    $__NR_pipe2, %eax
+    int     $0x80
+    cmpl    $-129, %eax
+    jb      1f
+    negl    %eax
+    pushl   %eax
+    call    __set_errno
+    addl    $4, %esp
+    orl     $-1, %eax
+1:
+    popl    %ecx
+    popl    %ebx
+    ret
diff --git a/libc/include/fcntl.h b/libc/include/fcntl.h
index 59e7135..7219dd7 100644
--- a/libc/include/fcntl.h
+++ b/libc/include/fcntl.h
@@ -39,6 +39,10 @@
 #define O_ASYNC  FASYNC
 #endif
 
+#ifndef O_CLOEXEC
+#define O_CLOEXEC  02000000
+#endif
+
 extern int  open(const char*  path, int  mode, ...);
 extern int  openat(int fd, const char*  path, int  mode, ...);
 extern int  unlinkat(int dirfd, const char *pathname, int flags);
diff --git a/libc/include/pthread.h b/libc/include/pthread.h
index 9773dcb..99e747a 100644
--- a/libc/include/pthread.h
+++ b/libc/include/pthread.h
@@ -309,4 +309,4 @@
 #define LONG_LONG_MAX __LONG_LONG_MAX__
 #define LONG_LONG_MIN (-__LONG_LONG_MAX__ - 1)
 
-#endif // _PTHREAD_H_
+#endif /* _PTHREAD_H_ */
diff --git a/libc/include/sys/file.h b/libc/include/sys/file.h
index 06937ff..cf2f4b1 100644
--- a/libc/include/sys/file.h
+++ b/libc/include/sys/file.h
@@ -31,7 +31,7 @@
 #include <sys/cdefs.h>
 #include <sys/types.h>
 
-// ANDROID: needed for flock()
+/* ANDROID: needed for flock() */
 #include <unistd.h>
 #include <fcntl.h>
 
diff --git a/libc/include/sys/ioctl_compat.h b/libc/include/sys/ioctl_compat.h
index d79b67a..cab5c80 100644
--- a/libc/include/sys/ioctl_compat.h
+++ b/libc/include/sys/ioctl_compat.h
@@ -39,8 +39,8 @@
 #ifndef _SYS_IOCTL_COMPAT_H_
 #define	_SYS_IOCTL_COMPAT_H_
 
-//#include <sys/ttychars.h>
-//#include <sys/ttydev.h>
+/*#include <sys/ttychars.h>*/
+/*#include <sys/ttydev.h>*/
 
 struct tchars {
 	char	t_intrc;	/* interrupt */
diff --git a/libc/include/sys/linux-syscalls.h b/libc/include/sys/linux-syscalls.h
index 9e2aa2f..6a7fc40 100644
--- a/libc/include/sys/linux-syscalls.h
+++ b/libc/include/sys/linux-syscalls.h
@@ -138,6 +138,7 @@
 #define __NR_openat                       (__NR_SYSCALL_BASE + 322)
 #define __NR_madvise                      (__NR_SYSCALL_BASE + 220)
 #define __NR_mincore                      (__NR_SYSCALL_BASE + 219)
+#define __NR_pipe2                        (__NR_SYSCALL_BASE + 359)
 #define __NR_getdents64                   (__NR_SYSCALL_BASE + 217)
 #define __NR_fstatfs64                    (__NR_SYSCALL_BASE + 267)
 #define __NR_fstatat64                    (__NR_SYSCALL_BASE + 327)
@@ -194,6 +195,7 @@
 #define __NR_openat                       (__NR_SYSCALL_BASE + 295)
 #define __NR_madvise                      (__NR_SYSCALL_BASE + 219)
 #define __NR_mincore                      (__NR_SYSCALL_BASE + 218)
+#define __NR_pipe2                        (__NR_SYSCALL_BASE + 331)
 #define __NR_getdents64                   (__NR_SYSCALL_BASE + 220)
 #define __NR_fstatfs64                    (__NR_SYSCALL_BASE + 269)
 #define __NR_fstatat64                    (__NR_SYSCALL_BASE + 300)
@@ -235,6 +237,7 @@
 #define __NR_openat                       (__NR_SYSCALL_BASE + 295)
 #define __NR_madvise                      (__NR_SYSCALL_BASE + 219)
 #define __NR_mincore                      (__NR_SYSCALL_BASE + 218)
+#define __NR_pipe2                        (__NR_SYSCALL_BASE + 331)
 #define __NR_getdents64                   (__NR_SYSCALL_BASE + 220)
 #define __NR_fstatfs64                    (__NR_SYSCALL_BASE + 269)
 #define __NR_fstatat64                    (__NR_SYSCALL_BASE + 300)
@@ -270,8 +273,8 @@
 #define __NR_socketcall                   (__NR_SYSCALL_BASE + 102)
 #define __NR_socketcall                   (__NR_SYSCALL_BASE + 102)
 #define __NR___socketcall                 (__NR_SYSCALL_BASE + 102)
-#define __NR_ioprio_set                   (__NR_SYSCALL_BASE + 289)
-#define __NR_ioprio_get                   (__NR_SYSCALL_BASE + 290)
+#define __NR_ioprio_set                   (__NR_SYSCALL_BASE + 288)
+#define __NR_ioprio_get                   (__NR_SYSCALL_BASE + 289)
 #define __NR_epoll_create                 (__NR_SYSCALL_BASE + 254)
 #define __NR_epoll_ctl                    (__NR_SYSCALL_BASE + 255)
 #define __NR_epoll_wait                   (__NR_SYSCALL_BASE + 256)
diff --git a/libc/include/sys/linux-unistd.h b/libc/include/sys/linux-unistd.h
index 6bd9ac7..7494efe 100644
--- a/libc/include/sys/linux-unistd.h
+++ b/libc/include/sys/linux-unistd.h
@@ -79,6 +79,7 @@
 int              fchmod (int, mode_t);
 int              dup (int);
 int              pipe (int *);
+int              pipe2 (int *, int);
 int              dup2 (int, int);
 int              select (int, struct fd_set *, struct fd_set *, struct fd_set *, struct timeval *);
 int              ftruncate (int, off_t);
diff --git a/libc/include/sys/ptrace.h b/libc/include/sys/ptrace.h
index 78a057a..848416b 100644
--- a/libc/include/sys/ptrace.h
+++ b/libc/include/sys/ptrace.h
@@ -30,7 +30,7 @@
 
 #include <sys/cdefs.h>
 #include <sys/types.h>
-// For all of the defines
+/* For all of the defines */
 #include <linux/ptrace.h>
 
 __BEGIN_DECLS
diff --git a/libc/include/unistd.h b/libc/include/unistd.h
index 7554198..cd09e3d 100644
--- a/libc/include/unistd.h
+++ b/libc/include/unistd.h
@@ -118,6 +118,9 @@
 extern int fchdir(int);
 extern int rmdir(const char *);
 extern int pipe(int *);
+#ifdef _GNU_SOURCE  /* GLibc compatibility */
+extern int pipe2(int *, int);
+#endif
 extern int chroot(const char *);
 extern int symlink(const char *, const char *);
 extern int readlink(const char *, char *, size_t);
diff --git a/libc/include/utmp.h b/libc/include/utmp.h
index e362b40..ffd3c92 100644
--- a/libc/include/utmp.h
+++ b/libc/include/utmp.h
@@ -87,4 +87,4 @@
 
 __END_DECLS
 
-#endif // _UTMP_H_
+#endif /* _UTMP_H_ */
diff --git a/libc/string/memmove.c b/libc/string/memmove.c
index fcaf4ee..948a766 100644
--- a/libc/string/memmove.c
+++ b/libc/string/memmove.c
@@ -31,7 +31,11 @@
 {
   const char *p = src;
   char *q = dst;
-  if (__builtin_expect(q < p, 1)) {
+
+  /* we can use highgly-optimized memcpy() if the destination
+   * is before the source, or if the two blocks are non-overlapping
+   */
+  if (__builtin_expect((q < p || (q-p) <= (ptrdiff_t)n), 1)) {
     return memcpy(dst, src, n);
   } else {
 #define PRELOAD_DISTANCE 64
diff --git a/libm/src/s_frexpf.c b/libm/src/s_frexpf.c
index 89d464b..c18cd54 100644
--- a/libm/src/s_frexpf.c
+++ b/libm/src/s_frexpf.c
@@ -39,6 +39,6 @@
 	}
 	*eptr += (ix>>23)-126;
 	hx = (hx&0x807fffff)|0x3f000000;
-	*(int*)&x = hx;
+	SET_FLOAT_WORD(x,hx);
 	return x;
 }