Back to index

glibc  2.9
speed.c
Go to the documentation of this file.
00001 /* `struct termios' speed frobnication functions.  SunOS 4 version.
00002    Copyright (C) 1991,1992,1993,1996,1997,2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <stddef.h>
00021 #include <errno.h>
00022 #include <termios.h>
00023 
00024 static const speed_t speeds[] =
00025   {
00026     0,
00027     50,
00028     75,
00029     110,
00030     134,
00031     150,
00032     200,
00033     300,
00034     600,
00035     1200,
00036     1800,
00037     2400,
00038     4800,
00039     9600,
00040     19200,
00041     38400,
00042   };
00043 
00044 
00045 /* Return the output baud rate stored in *TERMIOS_P.  */
00046 speed_t
00047 cfgetospeed (termios_p)
00048      const struct termios *termios_p;
00049 {
00050   return termios_p->c_cflag & CBAUD;
00051 }
00052 
00053 /* Return the input baud rate stored in *TERMIOS_P.  */
00054 speed_t
00055 cfgetispeed (termios_p)
00056      const struct termios *termios_p;
00057 {
00058   return (termios_p->c_cflag & CIBAUD) >> IBSHIFT;
00059 }
00060 
00061 /* Set the output baud rate stored in *TERMIOS_P to SPEED.  */
00062 int
00063 cfsetospeed (termios_p, speed)
00064      struct termios *termios_p;
00065      speed_t speed;
00066 {
00067   register unsigned int i;
00068 
00069   if (termios_p == NULL)
00070     {
00071       __set_errno (EINVAL);
00072       return -1;
00073     }
00074 
00075   /* This allows either B1200 or 1200 to work.   XXX
00076      Do we really want to try to support this, given that
00077      fetching the speed must return one or the other?  */
00078 
00079   for (i = 0; i < sizeof (speeds) / sizeof (speeds[0]); ++i)
00080     if (i == speed || speeds[i] == speed)
00081       {
00082        termios_p->c_cflag &= ~CBAUD;
00083        termios_p->c_cflag |= i;
00084        return 0;
00085       }
00086 
00087   __set_errno (EINVAL);
00088   return -1;
00089 }
00090 libc_hidden_def (cfsetospeed)
00091 
00092 /* Set the input baud rate stored in *TERMIOS_P to SPEED.  */
00093 int
00094 cfsetispeed (termios_p, speed)
00095      struct termios *termios_p;
00096      speed_t speed;
00097 {
00098   register unsigned int i;
00099 
00100   if (termios_p == NULL)
00101     {
00102       __set_errno (EINVAL);
00103       return -1;
00104     }
00105 
00106   /* See comment in cfsetospeed (above).  */
00107   for (i = 0; i < sizeof (speeds) / sizeof (speeds[0]); ++i)
00108     if (i == speed || speeds[i] == speed)
00109       {
00110        termios_p->c_cflag &= ~CIBAUD;
00111        termios_p->c_cflag |= i << IBSHIFT;
00112        return 0;
00113       }
00114 
00115   __set_errno (EINVAL);
00116   return -1;
00117 }
00118 libc_hidden_def (cfsetispeed)