(root)/
glibc-2.38/
termios/
cfsetspeed.c
       1  /* Copyright (C) 1992-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       9     The GNU C Library is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <termios.h>
      19  #include <errno.h>
      20  #include <stddef.h>
      21  
      22  struct speed_struct
      23  {
      24    speed_t value;
      25    speed_t internal;
      26  };
      27  
      28  static const struct speed_struct speeds[] =
      29    {
      30  #ifdef B0
      31      { 0, B0 },
      32  #endif
      33  #ifdef B50
      34      { 50, B50 },
      35  #endif
      36  #ifdef B75
      37      { 75, B75 },
      38  #endif
      39  #ifdef B110
      40      { 110, B110 },
      41  #endif
      42  #ifdef B134
      43      { 134, B134 },
      44  #endif
      45  #ifdef B150
      46      { 150, B150 },
      47  #endif
      48  #ifdef B200
      49      { 200, B200 },
      50  #endif
      51  #ifdef B300
      52      { 300, B300 },
      53  #endif
      54  #ifdef B600
      55      { 600, B600 },
      56  #endif
      57  #ifdef B1200
      58      { 1200, B1200 },
      59  #endif
      60  #ifdef B1200
      61      { 1200, B1200 },
      62  #endif
      63  #ifdef B1800
      64      { 1800, B1800 },
      65  #endif
      66  #ifdef B2400
      67      { 2400, B2400 },
      68  #endif
      69  #ifdef B4800
      70      { 4800, B4800 },
      71  #endif
      72  #ifdef B9600
      73      { 9600, B9600 },
      74  #endif
      75  #ifdef B19200
      76      { 19200, B19200 },
      77  #endif
      78  #ifdef B38400
      79      { 38400, B38400 },
      80  #endif
      81  #ifdef B57600
      82      { 57600, B57600 },
      83  #endif
      84  #ifdef B76800
      85      { 76800, B76800 },
      86  #endif
      87  #ifdef B115200
      88      { 115200, B115200 },
      89  #endif
      90  #ifdef B153600
      91      { 153600, B153600 },
      92  #endif
      93  #ifdef B230400
      94      { 230400, B230400 },
      95  #endif
      96  #ifdef B307200
      97      { 307200, B307200 },
      98  #endif
      99  #ifdef B460800
     100      { 460800, B460800 },
     101  #endif
     102  #ifdef B500000
     103      { 500000, B500000 },
     104  #endif
     105  #ifdef B576000
     106      { 576000, B576000 },
     107  #endif
     108  #ifdef B921600
     109      { 921600, B921600 },
     110  #endif
     111  #ifdef B1000000
     112      { 1000000, B1000000 },
     113  #endif
     114  #ifdef B1152000
     115      { 1152000, B1152000 },
     116  #endif
     117  #ifdef B1500000
     118      { 1500000, B1500000 },
     119  #endif
     120  #ifdef B2000000
     121      { 2000000, B2000000 },
     122  #endif
     123  #ifdef B2500000
     124      { 2500000, B2500000 },
     125  #endif
     126  #ifdef B3000000
     127      { 3000000, B3000000 },
     128  #endif
     129  #ifdef B3500000
     130      { 3500000, B3500000 },
     131  #endif
     132  #ifdef B4000000
     133      { 4000000, B4000000 },
     134  #endif
     135    };
     136  
     137  
     138  /* Set both the input and output baud rates stored in *TERMIOS_P to SPEED.  */
     139  int
     140  cfsetspeed (struct termios *termios_p, speed_t speed)
     141  {
     142    size_t cnt;
     143  
     144    for (cnt = 0; cnt < sizeof (speeds) / sizeof (speeds[0]); ++cnt)
     145      if (speed == speeds[cnt].internal)
     146        {
     147  	cfsetispeed (termios_p, speed);
     148  	cfsetospeed (termios_p, speed);
     149  	return 0;
     150        }
     151      else if (speed == speeds[cnt].value)
     152        {
     153  	cfsetispeed (termios_p, speeds[cnt].internal);
     154  	cfsetospeed (termios_p, speeds[cnt].internal);
     155  	return 0;
     156        }
     157  
     158    __set_errno (EINVAL);
     159  
     160    return -1;
     161  }