Merge "Clean up our termios implementation."
diff --git a/libc/Android.mk b/libc/Android.mk
index 472241f..bbba7bf 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -73,8 +73,6 @@
     bionic/strntoumax.c \
     bionic/strtotimeval.c \
     bionic/system_properties_compat.c \
-    bionic/tcgetpgrp.c \
-    bionic/tcsetpgrp.c \
     bionic/time64.c \
     bionic/unlockpt.c \
     stdio/findfp.c \
@@ -216,6 +214,7 @@
     bionic/sys_siglist.c \
     bionic/sys_signame.c \
     bionic/tdestroy.cpp \
+    bionic/termios.cpp \
     bionic/thread_atexit.cpp \
     bionic/tmpfile.cpp \
     bionic/umount.cpp \
diff --git a/libc/bionic/tcgetpgrp.c b/libc/bionic/tcgetpgrp.c
deleted file mode 100644
index ebff66a..0000000
--- a/libc/bionic/tcgetpgrp.c
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
-** Copyright 2006, The Android Open Source Project
-**
-** Redistribution and use in source and binary forms, with or without
-** modification, are permitted provided that the following conditions are met:
-**     * Redistributions of source code must retain the above copyright
-**       notice, this list of conditions and the following disclaimer.
-**     * Redistributions in binary form must reproduce the above copyright
-**       notice, this list of conditions and the following disclaimer in the
-**       documentation and/or other materials provided with the distribution.
-**     * Neither the name of Google Inc. nor the names of its contributors may
-**       be used to endorse or promote products derived from this software
-**       without specific prior written permission.
-**
-** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
-** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
-** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
-** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-#include <unistd.h>
-#include <termios.h>
-
-pid_t tcgetpgrp(int fd)
-{
-    pid_t _pid;
-    return ioctl(fd, TIOCGPGRP, &_pid) ? (pid_t)-1 : _pid;
-}
diff --git a/libc/bionic/tcsetpgrp.c b/libc/bionic/tcsetpgrp.c
deleted file mode 100644
index 06d9cd0..0000000
--- a/libc/bionic/tcsetpgrp.c
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
-** Copyright 2006, The Android Open Source Project
-**
-** Redistribution and use in source and binary forms, with or without
-** modification, are permitted provided that the following conditions are met:
-**     * Redistributions of source code must retain the above copyright
-**       notice, this list of conditions and the following disclaimer.
-**     * Redistributions in binary form must reproduce the above copyright
-**       notice, this list of conditions and the following disclaimer in the
-**       documentation and/or other materials provided with the distribution.
-**     * Neither the name of Google Inc. nor the names of its contributors may
-**       be used to endorse or promote products derived from this software
-**       without specific prior written permission.
-**
-** THIS SOFTWARE IS PROVIDED BY Google Inc. ``AS IS'' AND ANY EXPRESS OR
-** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
-** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
-** EVENT SHALL Google Inc. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-#include <unistd.h>
-#include <termios.h>
-
-int tcsetpgrp(int fd, pid_t _pid)
-{
-    return ioctl(fd, TIOCSPGRP, &_pid);
-}
diff --git a/libc/bionic/termios.cpp b/libc/bionic/termios.cpp
new file mode 100644
index 0000000..082dcdc
--- /dev/null
+++ b/libc/bionic/termios.cpp
@@ -0,0 +1,117 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <termios.h>
+#include <unistd.h>
+
+static speed_t cfgetspeed(const termios* s) {
+  return (s->c_cflag & CBAUD);
+}
+
+speed_t cfgetispeed(const termios* s) {
+  return cfgetspeed(s);
+}
+
+speed_t cfgetospeed(const termios* s) {
+  return cfgetspeed(s);
+}
+
+void cfmakeraw(termios* s) {
+  s->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
+  s->c_oflag &= ~OPOST;
+  s->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
+  s->c_cflag &= ~(CSIZE|PARENB);
+  s->c_cflag |= CS8;
+}
+
+int cfsetispeed(termios* s, speed_t speed) {
+  return cfsetspeed(s, speed);
+}
+
+int cfsetospeed(termios* s, speed_t speed) {
+  return cfsetspeed(s, speed);
+}
+
+int cfsetspeed(termios* s, speed_t speed) {
+  // TODO: check 'speed' is valid.
+  s->c_cflag = (s->c_cflag & ~CBAUD) | (speed & CBAUD);
+  return 0;
+}
+
+int tcdrain(int fd) {
+  // A non-zero argument to TCSBRK means "don't send a break".
+  // The drain is a side-effect of the ioctl!
+  return ioctl(fd, TCSBRK, static_cast<unsigned long>(1));
+}
+
+int tcflow(int fd, int action) {
+  return ioctl(fd, TCXONC, static_cast<unsigned long>(action));
+}
+
+int tcflush(int fd, int queue) {
+  return ioctl(fd, TCFLSH, static_cast<unsigned long>(queue));
+}
+
+int tcgetattr(int fd, termios* s) {
+  return ioctl(fd, TCGETS, s);
+}
+
+pid_t tcgetsid(int fd) {
+  pid_t sid;
+  if (ioctl(fd, TIOCGSID, &sid) == -1) {
+    return -1;
+  }
+  return sid;
+}
+
+int tcsendbreak(int fd, int duration) {
+  return ioctl(fd, TCSBRKP, static_cast<unsigned long>(duration));
+}
+
+int tcsetattr(int fd, int optional_actions, const termios* s) {
+  int cmd;
+  switch (optional_actions) {
+    case TCSANOW: cmd = TCSETS; break;
+    case TCSADRAIN: cmd = TCSETSW; break;
+    case TCSAFLUSH: cmd = TCSETSF; break;
+    default: errno = EINVAL; return -1;
+  }
+  return ioctl(fd, cmd, s);
+}
+
+pid_t tcgetpgrp(int fd) {
+  pid_t pid;
+  if (ioctl(fd, TIOCGPGRP, &pid) == -1) {
+    return -1;
+  }
+  return pid;
+}
+
+int tcsetpgrp(int fd, pid_t pid) {
+  return ioctl(fd, TIOCSPGRP, &pid);
+}
diff --git a/libc/include/termios.h b/libc/include/termios.h
index 0d44355..b9685ca 100644
--- a/libc/include/termios.h
+++ b/libc/include/termios.h
@@ -31,87 +31,23 @@
 #include <sys/cdefs.h>
 #include <sys/ioctl.h>
 #include <sys/types.h>
-#include <stdint.h>
 #include <linux/termios.h>
 
 __BEGIN_DECLS
 
-/* Redefine these to match their ioctl number */
-#undef  TCSANOW
-#define TCSANOW    TCSETS
-
-#undef  TCSADRAIN
-#define TCSADRAIN  TCSETSW
-
-#undef  TCSAFLUSH
-#define TCSAFLUSH  TCSETSF
-
-static __inline__ int tcgetattr(int fd, struct termios *s)
-{
-    return ioctl(fd, TCGETS, s);
-}
-
-static __inline__ int tcsetattr(int fd, int __opt, const struct termios *s)
-{
-    return ioctl(fd, __opt, (void *)s);
-}
-
-static __inline__ int tcflow(int fd, int action)
-{
-    return ioctl(fd, TCXONC, (void *)(intptr_t)action);
-}
-
-static __inline__ int tcflush(int fd, int __queue)
-{
-    return ioctl(fd, TCFLSH, (void *)(intptr_t)__queue);
-}
-
-static __inline__ int tcdrain(int fd)
-{
-    return ioctl(fd, TCSBRK, (void *)(intptr_t)1);
-}
-
-static __inline__ pid_t tcgetsid(int fd)
-{
-    pid_t _pid;
-    return ioctl(fd, TIOCGSID, &_pid) ? (pid_t)-1 : _pid;
-}
-
-static __inline__ int tcsendbreak(int fd, int __duration)
-{
-    return ioctl(fd, TCSBRKP, (void *)(uintptr_t)__duration);
-}
-
-static __inline__ speed_t cfgetospeed(const struct termios *s)
-{
-    return (speed_t)(s->c_cflag & CBAUD);
-}
-
-static __inline__ int cfsetospeed(struct termios *s, speed_t  speed)
-{
-    s->c_cflag = (s->c_cflag & ~CBAUD) | (speed & CBAUD);
-    return 0;
-}
-
-static __inline__ speed_t cfgetispeed(const struct termios *s)
-{
-    return (speed_t)(s->c_cflag & CBAUD);
-}
-
-static __inline__ int cfsetispeed(struct termios *s, speed_t  speed)
-{
-    s->c_cflag = (s->c_cflag & ~CBAUD) | (speed & CBAUD);
-  return 0;
-}
-
-static __inline__ void cfmakeraw(struct termios *s)
-{
-    s->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
-    s->c_oflag &= ~OPOST;
-    s->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
-    s->c_cflag &= ~(CSIZE|PARENB);
-    s->c_cflag |= CS8;
-}
+speed_t cfgetispeed(const struct termios*);
+speed_t cfgetospeed(const struct termios*);
+void cfmakeraw(struct termios*);
+int cfsetispeed(struct termios*, speed_t);
+int cfsetospeed(struct termios*, speed_t);
+int cfsetspeed(struct termios*, speed_t);
+int tcdrain(int);
+int tcflow(int, int);
+int tcflush(int, int);
+int tcgetattr(int, struct termios*);
+pid_t tcgetsid(int);
+int tcsendbreak(int, int);
+int tcsetattr(int, int, const struct termios*);
 
 __END_DECLS