// SPDX-License-Identifier: WTFPL #include #include #include #include #include #include static int fd = -1; int serprintf(char *fmt, ...) { va_list ap; int len; char buf[4096]; va_start(ap, fmt); len = vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); if (fd >= 0) write(fd, buf, len); return len; } void serprintf_init(char *dev, int speed) { struct termios t; if ((fd = open(dev, O_RDWR | O_NOCTTY | O_NDELAY)) < 0) return; memset(&t, 0, sizeof(t)); cfsetospeed(&t, speed); cfsetispeed(&t, speed); t.c_cflag = CREAD | CLOCAL | CS8; t.c_iflag = INPCK; t.c_oflag = 0; t.c_lflag = 0; t.c_cc[VTIME] = 0; t.c_cc[VMIN] = 1; tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &t); }