+
+ if (custom_baud_rate == FALSE) {
+ if (cfsetospeed (&newtio, baud_rate) < 0 || cfsetispeed (&newtio, baud_rate) < 0)
+ return FALSE;
+ } else {
+#if __linux__ || defined(HAVE_IOKIT)
+
+ /* On Linux to set a custom baud rate, we must set the
+ * "standard" baud_rate to 38400. On Apple we set it purely
+ * so that tcsetattr has something to use (and report back later), but
+ * the Apple specific API is still opaque to these APIs, see:
+ * https://developer.apple.com/library/mac/samplecode/SerialPortSample/Listings/SerialPortSample_SerialPortSample_c.html#//apple_ref/doc/uid/DTS10000454-SerialPortSample_SerialPortSample_c-DontLinkElementID_4
+ */
+ if (cfsetospeed (&newtio, B38400) < 0 || cfsetispeed (&newtio, B38400) < 0)
+ return FALSE;
+#endif
+ }
+
+ if (tcsetattr (fd, TCSANOW, &newtio) < 0)
+ return FALSE;
+
+ if (custom_baud_rate == TRUE){
+#if defined(HAVE_LINUX_SERIAL_H)
+ struct serial_struct ser;
+
+ if (ioctl (fd, TIOCGSERIAL, &ser) < 0)
+ {
+ return FALSE;
+ }
+
+ ser.custom_divisor = ser.baud_base / baud_rate;
+ ser.flags &= ~ASYNC_SPD_MASK;
+ ser.flags |= ASYNC_SPD_CUST;
+
+ if (ioctl (fd, TIOCSSERIAL, &ser) < 0)
+ {
+ return FALSE;
+ }
+#elif defined(HAVE_IOKIT)
+ speed_t speed = baud_rate;
+ if (ioctl(fd, IOSSIOSPEED, &speed) == -1)
+ return FALSE;
+#else
+ /* Don't know how to set custom baud rate on this platform. */