]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - sysdeps/unix/bsd/tcsetattr.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / unix / bsd / tcsetattr.c
index 0741c475e043ffb654e548be4510544a72df5324..bf5d992151d74d3d43928ed7d65e11d3e17d90ba 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991, 1993, 1996, 1997, 2002 Free Software Foundation, Inc.
+/* Copyright (C) 1992-2016 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
 #include <stddef.h>
 #include <termios.h>
 
-#include "bsdtty.h"
-
-
-const speed_t __bsd_speeds[] =
-  {
-    0,
-    50,
-    75,
-    110,
-    134,
-    150,
-    200,
-    300,
-    600,
-    1200,
-    1800,
-    2400,
-    4800,
-    9600,
-    19200,
-    38400,
-  };
+/* These are defined both in <bits/termios.h> and in <bits/ioctls.h>.
+   They should have the same values, but perhaps not written the same way.  */
+#undef ECHO
+#undef MDMBUF
+#undef TOSTOP
+#undef FLUSHO
+#undef PENDIN
+#undef NOFLSH
+#include <sys/ioctl.h>
 
 
 /* Set the state of FD to *TERMIOS_P.  */
 int
-tcsetattr (fd, optional_actions, termios_p)
-     int fd;
-     int optional_actions;
-     const struct termios *termios_p;
+tcsetattr (int fd, int optional_actions, const struct termios *termios_p)
 {
-  struct sgttyb buf;
-  struct tchars tchars;
-  struct ltchars ltchars;
-  int local;
-#ifdef TIOCGETX
-  int extra;
-#endif
-  size_t i;
+  struct termios myt;
 
-  if (__ioctl (fd, TIOCGETP, &buf) < 0 ||
-      __ioctl (fd, TIOCGETC, &tchars) < 0 ||
-      __ioctl (fd, TIOCGLTC, &ltchars) < 0 ||
-#ifdef TIOCGETX
-      __ioctl (fd, TIOCGETX, &extra) < 0 ||
-#endif
-      __ioctl (fd, TIOCLGET, &local) < 0)
-    return -1;
-
-  if (termios_p == NULL)
+  if (optional_actions & TCSASOFT)
     {
-      __set_errno (EINVAL);
-      return -1;
+      myt = *termios_p;
+      myt.c_cflag |= CIGNORE;
+      termios_p = &myt;
+      optional_actions &= ~TCSASOFT;
     }
+
   switch (optional_actions)
     {
     case TCSANOW:
-      break;
+      return __ioctl (fd, TIOCSETA, termios_p);
+
     case TCSADRAIN:
-      if (tcdrain (fd) < 0)
-       return -1;
-      break;
-    case TCSAFLUSH:
-      if (tcflush (fd, TCIFLUSH) < 0)
-       return -1;
-      break;
-    default:
-      __set_errno (EINVAL);
-      return -1;
-    }
+      return __ioctl (fd, TIOCSETAW, termios_p);
 
-  buf.sg_ispeed = buf.sg_ospeed = -1;
-  for (i = 0; i <= sizeof (__bsd_speeds) / sizeof (__bsd_speeds[0]); ++i)
-    {
-      if (__bsd_speeds[i] == termios_p->__ispeed)
-       buf.sg_ispeed = i;
-      if (__bsd_speeds[i] == termios_p->__ospeed)
-       buf.sg_ospeed = i;
-    }
-  if (buf.sg_ispeed == -1 || buf.sg_ospeed == -1)
-    {
-      __set_errno (EINVAL);
-      return -1;
+    default:
+      return __ioctl (fd, TIOCSETAF, termios_p);
     }
-
-  buf.sg_flags &= ~(CBREAK|RAW);
-  if (!(termios_p->c_lflag & ICANON))
-    buf.sg_flags |= (termios_p->c_cflag & ISIG) ? CBREAK : RAW;
-#ifdef LPASS8
-  if (termios_p->c_oflag & CS8)
-    local |= LPASS8;
-  else
-    local &= ~LPASS8;
-#endif
-  if (termios_p->c_lflag & _NOFLSH)
-    local |= LNOFLSH;
-  else
-    local &= ~LNOFLSH;
-  if (termios_p->c_oflag & OPOST)
-    local &= ~LLITOUT;
-  else
-    local |= LLITOUT;
-#ifdef TIOCGETX
-  if (termios_p->c_lflag & ISIG)
-    extra &= ~NOISIG;
-  else
-    extra |= NOISIG;
-  if (termios_p->c_cflag & CSTOPB)
-    extra |= STOPB;
-  else
-    extra &= ~STOPB;
-#endif
-  if (termios_p->c_iflag & ICRNL)
-    buf.sg_flags |= CRMOD;
-  else
-    buf.sg_flags &= ~CRMOD;
-  if (termios_p->c_iflag & IXOFF)
-    buf.sg_flags |= TANDEM;
-  else
-    buf.sg_flags &= ~TANDEM;
-
-  buf.sg_flags &= ~(ODDP|EVENP);
-  if (!(termios_p->c_cflag & PARENB))
-    buf.sg_flags |= ODDP | EVENP;
-  else if (termios_p->c_cflag & PARODD)
-    buf.sg_flags |= ODDP;
-  else
-    buf.sg_flags |= EVENP;
-
-  if (termios_p->c_lflag & _ECHO)
-    buf.sg_flags |= ECHO;
-  else
-    buf.sg_flags &= ~ECHO;
-  if (termios_p->c_lflag & ECHOE)
-    local |= LCRTERA;
-  else
-    local &= ~LCRTERA;
-  if (termios_p->c_lflag & ECHOK)
-    local |= LCRTKIL;
-  else
-    local &= ~LCRTKIL;
-  if (termios_p->c_lflag & _TOSTOP)
-    local |= LTOSTOP;
-  else
-    local &= ~LTOSTOP;
-
-  buf.sg_erase = termios_p->c_cc[VERASE];
-  buf.sg_kill = termios_p->c_cc[VKILL];
-  tchars.t_eofc = termios_p->c_cc[VEOF];
-  tchars.t_intrc = termios_p->c_cc[VINTR];
-  tchars.t_quitc = termios_p->c_cc[VQUIT];
-  ltchars.t_suspc = termios_p->c_cc[VSUSP];
-  tchars.t_startc = termios_p->c_cc[VSTART];
-  tchars.t_stopc = termios_p->c_cc[VSTOP];
-
-  if (__ioctl (fd, TIOCSETP, &buf) < 0 ||
-      __ioctl (fd, TIOCSETC, &tchars) < 0 ||
-      __ioctl (fd, TIOCSLTC, &ltchars) < 0 ||
-#ifdef TIOCGETX
-      __ioctl (fd, TIOCSETX, &extra) < 0 ||
-#endif
-      __ioctl (fd, TIOCLSET, &local) < 0)
-    return -1;
-  return 0;
 }
 libc_hidden_def (tcsetattr)