void mread(int fd, char *s, int count)
{
while (count) {
while (read(fd, s, 1) == 0);
s++;
count--;
}
}
// Make sure that nothing is waiting in the pipeline
static void mempty(int fd)
{
char c;
while (read(fd, &c, 1) == 1) {
DEBUG("Emptying %d\n", c);
}
}
if (tcsetattr (fd, TCSANOW, &tty) != 0) {
fprintf(stderr, "error %d setting term attributes", errno);
}
}
int ttyopen(char *devname)
{
int fd = 0;
if (strcmp(devname, "-") != 0) {
fd = open(devname, O_RDWR|O_NOCTTY|O_SYNC);
}
set_blocking(fd, 0);
mempty(fd);
// Communication with device is much more reliable if we
// begin by sending, asynchronously, a CR to make sure we
// empty any pending stuff on all sides.
write(fd, "\r", 1);
mempty(fd);
set_blocking(fd, 1);
return fd;
}