Back to index

glibc  2.9
speed.c
Go to the documentation of this file.
00001 /* `struct termios' speed frobnication functions.  Linux version.
00002    Copyright (C) 1991,1992,1993,1995,1996,1997,1998,2000,2002,2003
00003        Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <stddef.h>
00022 #include <errno.h>
00023 #include <termios.h>
00024 
00025 
00026 /* This is a gross hack around a kernel bug.  If the cfsetispeed functions
00027    is called with the SPEED argument set to zero this means use the same
00028    speed as for output.  But we don't have independent input and output
00029    speeds and therefore cannot record this.
00030 
00031    We use an unused bit in the `c_iflag' field to keep track of this
00032    use of `cfsetispeed'.  The value here must correspond to the one used
00033    in `tcsetattr.c'.  */
00034 #define IBAUD0       020000000000
00035 
00036 
00037 /* Return the output baud rate stored in *TERMIOS_P.  */
00038 speed_t
00039 cfgetospeed (termios_p)
00040      const struct termios *termios_p;
00041 {
00042   return termios_p->c_cflag & (CBAUD | CBAUDEX);
00043 }
00044 
00045 /* Return the input baud rate stored in *TERMIOS_P.
00046    Although for Linux there is no difference between input and output
00047    speed, the numerical 0 is a special case for the input baud rate. It
00048    should set the input baud rate to the output baud rate. */
00049 speed_t
00050 cfgetispeed (termios_p)
00051      const struct termios *termios_p;
00052 {
00053   return ((termios_p->c_iflag & IBAUD0)
00054          ? 0 : termios_p->c_cflag & (CBAUD | CBAUDEX));
00055 }
00056 
00057 /* Set the output baud rate stored in *TERMIOS_P to SPEED.  */
00058 int
00059 cfsetospeed  (termios_p, speed)
00060      struct termios *termios_p;
00061      speed_t speed;
00062 {
00063   if ((speed & ~CBAUD) != 0
00064       && (speed < B57600 || speed > __MAX_BAUD))
00065     {
00066       __set_errno (EINVAL);
00067       return -1;
00068     }
00069 
00070 #ifdef _HAVE_STRUCT_TERMIOS_C_OSPEED
00071   termios_p->c_ospeed = speed;
00072 #endif
00073   termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
00074   termios_p->c_cflag |= speed;
00075 
00076   return 0;
00077 }
00078 libc_hidden_def (cfsetospeed)
00079 
00080 
00081 /* Set the input baud rate stored in *TERMIOS_P to SPEED.
00082    Although for Linux there is no difference between input and output
00083    speed, the numerical 0 is a special case for the input baud rate.  It
00084    should set the input baud rate to the output baud rate.  */
00085 int
00086 cfsetispeed (termios_p, speed)
00087      struct termios *termios_p;
00088      speed_t speed;
00089 {
00090   if ((speed & ~CBAUD) != 0
00091       && (speed < B57600 || speed > __MAX_BAUD))
00092     {
00093       __set_errno (EINVAL);
00094       return -1;
00095     }
00096 
00097 #ifdef _HAVE_STRUCT_TERMIOS_C_ISPEED
00098   termios_p->c_ispeed = speed;
00099 #endif
00100   if (speed == 0)
00101     termios_p->c_iflag |= IBAUD0;
00102   else
00103     {
00104       termios_p->c_iflag &= ~IBAUD0;
00105       termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
00106       termios_p->c_cflag |= speed;
00107     }
00108 
00109   return 0;
00110 }
00111 libc_hidden_def (cfsetispeed)