Back to index

glibc  2.9
tcgetattr.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992, 1995, 1996, 1997 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <stddef.h>
00021 #include <sysv_termio.h>
00022 #include <termios.h>
00023 #include <sys/ioctl.h>
00024 
00025 /* Put the state of FD into *TERMIOS_P.  */
00026 int
00027 __tcgetattr (fd, termios_p)
00028      int fd;
00029      struct termios *termios_p;
00030 {
00031   struct __sysv_termio buf;
00032   int termio_speed;
00033 
00034   if (termios_p == NULL)
00035     {
00036       __set_errno (EINVAL);
00037       return -1;
00038     }
00039 
00040   if (__ioctl (fd, _TCGETA, &buf) < 0)
00041     return -1;
00042 
00043   termio_speed = buf.c_cflag & _SYSV_CBAUD;
00044   termios_p->__ospeed =
00045     (termio_speed == _SYSV_B0 ? 0 :
00046      termio_speed == _SYSV_B50 ? 50 :
00047      termio_speed == _SYSV_B75 ? 75 :
00048      termio_speed == _SYSV_B110 ? 110 :
00049      termio_speed == _SYSV_B134 ? 134 :
00050      termio_speed == _SYSV_B150 ? 150 :
00051      termio_speed == _SYSV_B200 ? 200 :
00052      termio_speed == _SYSV_B300 ? 300 :
00053      termio_speed == _SYSV_B600 ? 600 :
00054      termio_speed == _SYSV_B1200 ? 1200 :
00055      termio_speed == _SYSV_B1800 ? 1800 :
00056      termio_speed == _SYSV_B2400 ? 2400 :
00057      termio_speed == _SYSV_B4800 ? 4800 :
00058      termio_speed == _SYSV_B9600 ? 9600 :
00059      termio_speed == _SYSV_B19200 ? 19200 :
00060      termio_speed == _SYSV_B38400 ? 38400 :
00061      -1);
00062   termios_p->__ispeed = termios_p->__ospeed;
00063 
00064   termios_p->c_iflag = 0;
00065   if (buf.c_iflag & _SYSV_IGNBRK)
00066     termios_p->c_iflag |= IGNBRK;
00067   if (buf.c_iflag & _SYSV_BRKINT)
00068     termios_p->c_iflag |= BRKINT;
00069   if (buf.c_iflag & _SYSV_IGNPAR)
00070     termios_p->c_iflag |= IGNPAR;
00071   if (buf.c_iflag & _SYSV_PARMRK)
00072     termios_p->c_iflag |= PARMRK;
00073   if (buf.c_iflag & _SYSV_INPCK)
00074     termios_p->c_iflag |= INPCK;
00075   if (buf.c_iflag & _SYSV_ISTRIP)
00076     termios_p->c_iflag |= ISTRIP;
00077   if (buf.c_iflag & _SYSV_INLCR)
00078     termios_p->c_iflag |= INLCR;
00079   if (buf.c_iflag & _SYSV_IGNCR)
00080     termios_p->c_iflag |= IGNCR;
00081   if (buf.c_iflag & _SYSV_ICRNL)
00082     termios_p->c_iflag |= ICRNL;
00083   if (buf.c_iflag & _SYSV_IXON)
00084     termios_p->c_iflag |= IXON;
00085   if (buf.c_iflag & _SYSV_IXOFF)
00086     termios_p->c_iflag |= IXOFF;
00087   if (buf.c_iflag & _SYSV_IXANY)
00088     termios_p->c_iflag |= IXANY;
00089   if (buf.c_iflag & _SYSV_IMAXBEL)
00090     termios_p->c_iflag |= IMAXBEL;
00091 
00092   termios_p->c_oflag = 0;
00093   if (buf.c_oflag & OPOST)
00094     termios_p->c_oflag |= OPOST;
00095   if (buf.c_oflag & ONLCR)
00096     termios_p->c_oflag |= ONLCR;
00097   termios_p->c_cflag = 0;
00098   switch (buf.c_cflag & _SYSV_CSIZE)
00099     {
00100     case _SYSV_CS5:
00101       termios_p->c_cflag |= CS5;
00102       break;
00103     case _SYSV_CS6:
00104       termios_p->c_cflag |= CS6;
00105       break;
00106     case _SYSV_CS7:
00107       termios_p->c_cflag |= CS7;
00108       break;
00109     case _SYSV_CS8:
00110       termios_p->c_cflag |= CS8;
00111       break;
00112     }
00113   if (buf.c_cflag & _SYSV_CSTOPB)
00114     termios_p->c_cflag |= CSTOPB;
00115   if (buf.c_cflag & _SYSV_CREAD)
00116     termios_p->c_cflag |= CREAD;
00117   if (buf.c_cflag & _SYSV_PARENB)
00118     termios_p->c_cflag |= PARENB;
00119   if (buf.c_cflag & _SYSV_PARODD)
00120     termios_p->c_cflag |= PARODD;
00121   if (buf.c_cflag & _SYSV_HUPCL)
00122     termios_p->c_cflag |= HUPCL;
00123   if (buf.c_cflag & _SYSV_CLOCAL)
00124     termios_p->c_cflag |= CLOCAL;
00125   termios_p->c_lflag = 0;
00126   if (buf.c_lflag & _SYSV_ISIG)
00127     termios_p->c_lflag |= _ISIG;
00128   if (buf.c_lflag & _SYSV_ICANON)
00129     termios_p->c_lflag |= _ICANON;
00130   if (buf.c_lflag & _SYSV_ECHO)
00131     termios_p->c_lflag |= _ECHO;
00132   if (buf.c_lflag & _SYSV_ECHOE)
00133     termios_p->c_lflag |= _ECHOE;
00134   if (buf.c_lflag & _SYSV_ECHOK)
00135     termios_p->c_lflag |= _ECHOK;
00136   if (buf.c_lflag & _SYSV_ECHONL)
00137     termios_p->c_lflag |= _ECHONL;
00138   if (buf.c_lflag & _SYSV_NOFLSH)
00139     termios_p->c_lflag |= _NOFLSH;
00140   if (buf.c_lflag & _SYSV_TOSTOP)
00141     termios_p->c_lflag |= _TOSTOP;
00142   if (buf.c_lflag & _SYSV_ECHOKE)
00143     termios_p->c_lflag |= ECHOKE;
00144   if (buf.c_lflag & _SYSV_ECHOPRT)
00145     termios_p->c_lflag |= ECHOPRT;
00146   if (buf.c_lflag & _SYSV_ECHOCTL)
00147     termios_p->c_lflag |= ECHOCTL;
00148   if (buf.c_lflag & _SYSV_FLUSHO)
00149     termios_p->c_lflag |= FLUSHO;
00150   if (buf.c_lflag & _SYSV_PENDIN)
00151     termios_p->c_lflag |= PENDIN;
00152   if (buf.c_lflag & _SYSV_IEXTEN)
00153     termios_p->c_lflag |= IEXTEN;
00154 
00155   termios_p->c_cc[VEOF] = buf.c_cc[_SYSV_VEOF];
00156   termios_p->c_cc[VEOL] = buf.c_cc[_SYSV_VEOL];
00157   termios_p->c_cc[VEOL2] = buf.c_cc[_SYSV_VEOL2];
00158   termios_p->c_cc[VERASE] = buf.c_cc[_SYSV_VERASE];
00159   termios_p->c_cc[VKILL] = buf.c_cc[_SYSV_VKILL];
00160   termios_p->c_cc[VINTR] = buf.c_cc[_SYSV_VINTR];
00161   termios_p->c_cc[VQUIT] = buf.c_cc[_SYSV_VQUIT];
00162   termios_p->c_cc[VSTART] = '\021'; /* XON (^Q).  */
00163   termios_p->c_cc[VSTOP] = '\023'; /* XOFF (^S).  */
00164   termios_p->c_cc[VSUSP] = '\0'; /* System V release 3 lacks job control.  */
00165   termios_p->c_cc[VMIN] = buf.c_cc[_SYSV_VMIN];
00166   termios_p->c_cc[VTIME] = buf.c_cc[_SYSV_VTIME];
00167 
00168   return 0;
00169 }
00170 
00171 weak_alias (__tcgetattr, tcgetattr)