Back to index

glibc  2.9
tcsetattr.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992,1995,1996,1997,2000,2002 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 #include <sys/ioctl.h>
00023 
00024 #include <sysv_termio.h>
00025 
00026 
00027 const speed_t __unix_speeds[] =
00028   {
00029     0,
00030     50,
00031     75,
00032     110,
00033     134,
00034     150,
00035     200,
00036     300,
00037     600,
00038     1200,
00039     1800,
00040     2400,
00041     4800,
00042     9600,
00043     19200,
00044     38400,
00045   };
00046 
00047 
00048 /* Set the state of FD to *TERMIOS_P.  */
00049 int
00050 tcsetattr (fd, optional_actions, termios_p)
00051      int fd;
00052      int optional_actions;
00053      const struct termios *termios_p;
00054 {
00055   struct __sysv_termio buf;
00056   int ioctl_function;
00057 
00058   if (termios_p == NULL)
00059     {
00060       __set_errno (EINVAL);
00061       return -1;
00062     }
00063   switch (optional_actions)
00064     {
00065     case TCSANOW:
00066       ioctl_function = _TCSETA;
00067       break;
00068     case TCSADRAIN:
00069       ioctl_function = _TCSETAW;
00070       break;
00071     case TCSAFLUSH:
00072       ioctl_function = _TCSETAF;
00073       break;
00074     default:
00075       __set_errno (EINVAL);
00076       return -1;
00077     }
00078 
00079   if ((termios_p->c_cflag & 0x000f0000) >> 16 != (termios_p->c_cflag & 0xf))
00080     {
00081       __set_errno (EINVAL);
00082       return -1;
00083     }
00084 
00085   buf.c_iflag = 0;
00086   if (termios_p->c_iflag & IGNBRK)
00087     buf.c_iflag |= _SYSV_IGNBRK;
00088   if (termios_p->c_iflag & BRKINT)
00089     buf.c_iflag |= _SYSV_BRKINT;
00090   if (termios_p->c_iflag & IGNPAR)
00091     buf.c_iflag |= _SYSV_IGNPAR;
00092   if (termios_p->c_iflag & PARMRK)
00093     buf.c_iflag |= _SYSV_PARMRK;
00094   if (termios_p->c_iflag & INPCK)
00095     buf.c_iflag |= _SYSV_INPCK;
00096   if (termios_p->c_iflag & ISTRIP)
00097     buf.c_iflag |= _SYSV_ISTRIP;
00098   if (termios_p->c_iflag & INLCR)
00099     buf.c_iflag |= _SYSV_INLCR;
00100   if (termios_p->c_iflag & IGNCR)
00101     buf.c_iflag |= _SYSV_IGNCR;
00102   if (termios_p->c_iflag & ICRNL)
00103     buf.c_iflag |= _SYSV_ICRNL;
00104   if (termios_p->c_iflag & IXON)
00105     buf.c_iflag |= _SYSV_IXON;
00106   if (termios_p->c_iflag & IXOFF)
00107     buf.c_iflag |= _SYSV_IXOFF;
00108   if (termios_p->c_iflag & IXANY)
00109     buf.c_iflag |= _SYSV_IXANY;
00110   if (termios_p->c_iflag & IMAXBEL)
00111     buf.c_iflag |= _SYSV_IMAXBEL;
00112 
00113   buf.c_oflag = 0;
00114   if (termios_p->c_oflag & OPOST)
00115     buf.c_oflag |= _SYSV_OPOST;
00116   if (termios_p->c_oflag & ONLCR)
00117     buf.c_oflag |= _SYSV_ONLCR;
00118 
00119   /* So far, buf.c_cflag contains the speed in CBAUD.  */
00120   if (termios_p->c_cflag & CSTOPB)
00121     buf.c_cflag |= _SYSV_CSTOPB;
00122   if (termios_p->c_cflag & CREAD)
00123     buf.c_cflag |= _SYSV_CREAD;
00124   if (termios_p->c_cflag & PARENB)
00125     buf.c_cflag |= _SYSV_PARENB;
00126   if (termios_p->c_cflag & PARODD)
00127     buf.c_cflag |= _SYSV_PARODD;
00128   if (termios_p->c_cflag & HUPCL)
00129     buf.c_cflag |= _SYSV_HUPCL;
00130   if (termios_p->c_cflag & CLOCAL)
00131     buf.c_cflag |= _SYSV_CLOCAL;
00132   switch (termios_p->c_cflag & CSIZE)
00133     {
00134     case CS5:
00135       buf.c_cflag |= _SYSV_CS5;
00136       break;
00137     case CS6:
00138       buf.c_cflag |= _SYSV_CS6;
00139       break;
00140     case CS7:
00141       buf.c_cflag |= _SYSV_CS7;
00142       break;
00143     case CS8:
00144       buf.c_cflag |= _SYSV_CS8;
00145       break;
00146     }
00147 
00148   buf.c_lflag = 0;
00149   if (termios_p->c_lflag & ISIG)
00150     buf.c_lflag |= _SYSV_ISIG;
00151   if (termios_p->c_lflag & ICANON)
00152     buf.c_lflag |= _SYSV_ICANON;
00153   if (termios_p->c_lflag & ECHO)
00154     buf.c_lflag |= _SYSV_ECHO;
00155   if (termios_p->c_lflag & ECHOE)
00156     buf.c_lflag |= _SYSV_ECHOE;
00157   if (termios_p->c_lflag & ECHOK)
00158     buf.c_lflag |= _SYSV_ECHOK;
00159   if (termios_p->c_lflag & ECHONL)
00160     buf.c_lflag |= _SYSV_ECHONL;
00161   if (termios_p->c_lflag & NOFLSH)
00162     buf.c_lflag |= _SYSV_NOFLSH;
00163   if (termios_p->c_lflag & TOSTOP)
00164     buf.c_lflag |= _SYSV_TOSTOP;
00165   if (termios_p->c_lflag & ECHOCTL)
00166     buf.c_lflag |= _SYSV_ECHOCTL;
00167   if (termios_p->c_lflag & ECHOPRT)
00168     buf.c_lflag |= _SYSV_ECHOPRT;
00169   if (termios_p->c_lflag & ECHOKE)
00170     buf.c_lflag |= _SYSV_ECHOKE;
00171   if (termios_p->c_lflag & FLUSHO)
00172     buf.c_lflag |= _SYSV_FLUSHO;
00173   if (termios_p->c_lflag & PENDIN)
00174     buf.c_lflag |= _SYSV_PENDIN;
00175   if (termios_p->c_lflag & IEXTEN)
00176     buf.c_lflag |= _SYSV_IEXTEN;
00177 
00178   buf.c_cc[_SYSV_VINTR] = termios_p->c_cc[VINTR];
00179   buf.c_cc[_SYSV_VQUIT] = termios_p->c_cc[VQUIT];
00180   buf.c_cc[_SYSV_VERASE] = termios_p->c_cc[VERASE];
00181   buf.c_cc[_SYSV_VKILL] = termios_p->c_cc[VKILL];
00182   if (buf.c_lflag & _SYSV_ICANON)
00183     {
00184       buf.c_cc[_SYSV_VEOF] = termios_p->c_cc[VEOF];
00185       buf.c_cc[_SYSV_VEOL] = termios_p->c_cc[VEOL];
00186     }
00187   else
00188     {
00189       buf.c_cc[_SYSV_VMIN] = termios_p->c_cc[VMIN];
00190       buf.c_cc[_SYSV_VTIME] = termios_p->c_cc[VTIME];
00191     }
00192   buf.c_cc[_SYSV_VEOL2] = termios_p->c_cc[VEOL2];
00193 
00194   if (__ioctl (fd, ioctl_function, &buf) < 0)
00195     return -1;
00196   return 0;
00197 }
00198 libc_hidden_def (tcsetattr)