cfsetspeed is not consistent with Linux

Ken Brown kbrown@cornell.edu
Sun Jul 11 16:33:42 GMT 2021


While investigating an emacs bug 
(https://debbugs.gnu.org/cgi/bugreport.cgi?bug=49524), I noticed a difference in 
the behavior of cfsetspeed(3) on Cygwin and Linux.  I'm not sure we should "fix" 
this, because Cygwin's behavior is consistent with the Linux man page, and 
Linux's behavior is not.  But I thought I should point it out for the sake of 
discussion, because Cygwin generally tries to emulate Linux.  Here are the details:

The Linux man page for cfsetspeed(3) specifies that the speed argument must be 
one of the constants Bnnn (e.g., B9600) defined in termios.h.  But Linux in fact 
allows the speed to be the numerical baud rate (e.g., 9600).  Test case:

$ cat cfsetspeed_test.c
#include <termios.h>
#include <stdio.h>

int
main ()
{
   struct termios tp;

   printf ("Calling cfsetspeed with speed B9600\n");
   if (cfsetspeed (&tp, B9600) < 0)
     perror ("cfsetspeed");
   else
     printf ("cfgetispeed reports speed %u\n", cfgetispeed (&tp));
   printf ("Calling cfsetspeed with speed 9600\n");
   if (cfsetspeed (&tp, 9600) < 0)
     perror ("cfsetspeed");
   else
     printf ("cfgetispeed reports speed %u\n", cfgetispeed (&tp));
}

$ gcc cfsetspeed_test.c

$ ./a.out
Calling cfsetspeed with speed B9600
cfgetispeed reports speed 13
Calling cfsetspeed with speed 9600
cfgetispeed reports speed 13

On Cygwin, however, the output of the same program is:

$ ./a
Calling cfsetspeed with speed B9600
cfgetispeed reports speed 13
Calling cfsetspeed with speed 9600
cfsetspeed: Invalid argument

If we decide that Cygwin should emulate Linux here, it would be a simple matter 
to copy the glibc code, which checks whether the speed is a numerical baud rate 
and, if so, converts it to a Bnnn constant.

Ken


More information about the Cygwin mailing list