Back to index

glibc  2.9
cfsetspeed.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992,93,96,97,98,2001 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 <termios.h>
00020 #include <errno.h>
00021 #include <stddef.h>
00022 
00023 struct speed_struct
00024 {
00025   speed_t value;
00026   speed_t internal;
00027 };
00028 
00029 static const struct speed_struct speeds[] =
00030   {
00031 #ifdef B0
00032     { 0, B0 },
00033 #endif
00034 #ifdef B50
00035     { 50, B50 },
00036 #endif
00037 #ifdef B75
00038     { 75, B75 },
00039 #endif
00040 #ifdef B110
00041     { 110, B110 },
00042 #endif
00043 #ifdef B134
00044     { 134, B134 },
00045 #endif
00046 #ifdef B150
00047     { 150, B150 },
00048 #endif
00049 #ifdef B200
00050     { 200, B200 },
00051 #endif
00052 #ifdef B300
00053     { 300, B300 },
00054 #endif
00055 #ifdef B600
00056     { 600, B600 },
00057 #endif
00058 #ifdef B1200
00059     { 1200, B1200 },
00060 #endif
00061 #ifdef B1200
00062     { 1200, B1200 },
00063 #endif
00064 #ifdef B1800
00065     { 1800, B1800 },
00066 #endif
00067 #ifdef B2400
00068     { 2400, B2400 },
00069 #endif
00070 #ifdef B4800
00071     { 4800, B4800 },
00072 #endif
00073 #ifdef B9600
00074     { 9600, B9600 },
00075 #endif
00076 #ifdef B19200
00077     { 19200, B19200 },
00078 #endif
00079 #ifdef B38400
00080     { 38400, B38400 },
00081 #endif
00082 #ifdef B57600
00083     { 57600, B57600 },
00084 #endif
00085 #ifdef B76800
00086     { 76800, B76800 },
00087 #endif
00088 #ifdef B115200
00089     { 115200, B115200 },
00090 #endif
00091 #ifdef B153600
00092     { 153600, B153600 },
00093 #endif
00094 #ifdef B230400
00095     { 230400, B230400 },
00096 #endif
00097 #ifdef B307200
00098     { 307200, B307200 },
00099 #endif
00100 #ifdef B460800
00101     { 460800, B460800 },
00102 #endif
00103 #ifdef B500000
00104     { 500000, B500000 },
00105 #endif
00106 #ifdef B576000
00107     { 576000, B576000 },
00108 #endif
00109 #ifdef B921600
00110     { 921600, B921600 },
00111 #endif
00112 #ifdef B1000000
00113     { 1000000, B1000000 },
00114 #endif
00115 #ifdef B1152000
00116     { 1152000, B1152000 },
00117 #endif
00118 #ifdef B1500000
00119     { 1500000, B1500000 },
00120 #endif
00121 #ifdef B2000000
00122     { 2000000, B2000000 },
00123 #endif
00124 #ifdef B2500000
00125     { 2500000, B2500000 },
00126 #endif
00127 #ifdef B3000000
00128     { 3000000, B3000000 },
00129 #endif
00130 #ifdef B3500000
00131     { 3500000, B3500000 },
00132 #endif
00133 #ifdef B4000000
00134     { 4000000, B4000000 },
00135 #endif
00136   };
00137 
00138 
00139 /* Set both the input and output baud rates stored in *TERMIOS_P to SPEED.  */
00140 int
00141 cfsetspeed (struct termios *termios_p, speed_t speed)
00142 {
00143   size_t cnt;
00144 
00145   for (cnt = 0; cnt < sizeof (speeds) / sizeof (speeds[0]); ++cnt)
00146     if (speed == speeds[cnt].internal)
00147       {
00148        cfsetispeed (termios_p, speed);
00149        cfsetospeed (termios_p, speed);
00150        return 0;
00151       }
00152     else if (speed == speeds[cnt].value)
00153       {
00154        cfsetispeed (termios_p, speeds[cnt].internal);
00155        cfsetospeed (termios_p, speeds[cnt].internal);
00156        return 0;
00157       }
00158 
00159   __set_errno (EINVAL);
00160 
00161   return -1;
00162 }