Back to index

glibc  2.9
tcgetattr.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 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 <termios.h>
00022 
00023 #include "bsdtty.h"
00024 
00025 extern const speed_t __bsd_speeds[]; /* Defined in tcsetattr.c.  */
00026 
00027 /* Put the state of FD into *TERMIOS_P.  */
00028 int
00029 __tcgetattr (fd, termios_p)
00030      int fd;
00031      struct termios *termios_p;
00032 {
00033   struct sgttyb buf;
00034   struct tchars tchars;
00035   struct ltchars ltchars;
00036   int local;
00037 #ifdef TIOCGETX
00038   int extra;
00039 #endif
00040 
00041   if (termios_p == NULL)
00042     {
00043       __set_errno (EINVAL);
00044       return -1;
00045     }
00046 
00047   if (__ioctl(fd, TIOCGETP, &buf) < 0 ||
00048       __ioctl(fd, TIOCGETC, &tchars) < 0 ||
00049       __ioctl(fd, TIOCGLTC, &ltchars) < 0 ||
00050 #ifdef TIOCGETX
00051       __ioctl(fd, TIOCGETX, &extra) < 0 ||
00052 #endif
00053       __ioctl(fd, TIOCLGET, &local) < 0)
00054     return -1;
00055 
00056   termios_p->__ispeed = __bsd_speeds[(unsigned char) buf.sg_ispeed];
00057   termios_p->__ospeed = __bsd_speeds[(unsigned char) buf.sg_ospeed];
00058 
00059   termios_p->c_iflag = 0;
00060   termios_p->c_oflag = 0;
00061   termios_p->c_cflag = 0;
00062   termios_p->c_lflag = 0;
00063   termios_p->c_oflag |= CREAD | HUPCL;
00064 #ifdef LPASS8
00065   if (local & LPASS8)
00066     termios_p->c_oflag |= CS8;
00067   else
00068 #endif
00069     termios_p->c_oflag |= CS7;
00070   if (!(buf.sg_flags & RAW))
00071     {
00072       termios_p->c_iflag |= IXON;
00073       termios_p->c_cflag |= OPOST;
00074 #ifndef       NOISIG
00075       termios_p->c_lflag |= ISIG;
00076 #endif
00077     }
00078   if ((buf.sg_flags & (CBREAK|RAW)) == 0)
00079     termios_p->c_lflag |= ICANON;
00080   if (!(buf.sg_flags & RAW) && !(local & LLITOUT))
00081     termios_p->c_oflag |= OPOST;
00082   if (buf.sg_flags & CRMOD)
00083     termios_p->c_iflag |= ICRNL;
00084   if (buf.sg_flags & TANDEM)
00085     termios_p->c_iflag |= IXOFF;
00086 #ifdef TIOCGETX
00087   if (!(extra & NOISIG))
00088     termios_p->c_lflag |= ISIG;
00089   if (extra & STOPB)
00090     termios_p->c_cflag |= CSTOPB;
00091 #endif
00092 
00093   switch (buf.sg_flags & (EVENP|ODDP))
00094     {
00095     case EVENP|ODDP:
00096       break;
00097     case ODDP:
00098       termios_p->c_cflag |= PARODD;
00099     default:
00100       termios_p->c_cflag |= PARENB;
00101       termios_p->c_iflag |= IGNPAR | INPCK;
00102       break;
00103     }
00104   if (buf.sg_flags & ECHO)
00105     termios_p->c_lflag |= _ECHO;
00106   if (local & LCRTERA)
00107     termios_p->c_lflag |= ECHOE;
00108   if (local & LCRTKIL)
00109     termios_p->c_lflag |= ECHOK;
00110   if (local & LTOSTOP)
00111     termios_p->c_lflag |= _TOSTOP;
00112   if (local & LNOFLSH)
00113     termios_p->c_lflag |= _NOFLSH;
00114 
00115   termios_p->c_cc[VEOF] = tchars.t_eofc;
00116   termios_p->c_cc[VEOL] = '\n';
00117   termios_p->c_cc[VERASE] = buf.sg_erase;
00118   termios_p->c_cc[VKILL] = buf.sg_kill;
00119   termios_p->c_cc[VINTR] = tchars.t_intrc;
00120   termios_p->c_cc[VQUIT] = tchars.t_quitc;
00121   termios_p->c_cc[VSTART] = tchars.t_startc;
00122   termios_p->c_cc[VSTOP] = tchars.t_stopc;
00123   termios_p->c_cc[VSUSP] = ltchars.t_suspc;
00124   termios_p->c_cc[VMIN] = -1;
00125   termios_p->c_cc[VTIME] = -1;
00126 
00127   return 0;
00128 }
00129 
00130 weak_alias (__tcgetattr, tcgetattr)