return read(fd, buf, count);
}
-ssize_t os_read_no_block(int fd, void *buf, size_t count)
-{
- const int flags = fcntl(fd, F_GETFL, 0);
-
- fcntl(fd, F_SETFL, flags | O_NONBLOCK);
- return os_read(fd, buf, count);
-}
-
ssize_t os_write(int fd, const void *buf, size_t count)
{
return write(fd, buf, count);
/* Restore tty state when we exit */
static struct termios orig_term;
static bool term_setup;
+static bool term_nonblock;
void os_fd_restore(void)
{
if (term_setup) {
+ int flags;
+
tcsetattr(0, TCSANOW, &orig_term);
+ if (term_nonblock) {
+ flags = fcntl(0, F_GETFL, 0);
+ fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
+ }
term_setup = false;
}
}
void os_tty_raw(int fd, bool allow_sigs)
{
struct termios term;
+ int flags;
if (term_setup)
return;
if (tcsetattr(fd, TCSANOW, &term))
return;
+ flags = fcntl(fd, F_GETFL, 0);
+ if (!(flags & O_NONBLOCK)) {
+ if (fcntl(fd, F_SETFL, flags | O_NONBLOCK))
+ return;
+ term_nonblock = true;
+ }
+
term_setup = true;
atexit(os_fd_restore);
}
if (next_index == serial_buf_read)
return 1; /* buffer full */
- count = os_read_no_block(0, &serial_buf[serial_buf_write], 1);
+ count = os_read(0, &serial_buf[serial_buf_write], 1);
if (count == 1)
serial_buf_write = next_index;
*/
ssize_t os_read(int fd, void *buf, size_t count);
-/**
- * Access to the OS read() system call with non-blocking access
- *
- * \param fd File descriptor as returned by os_open()
- * \param buf Buffer to place data
- * \param count Number of bytes to read
- * \return number of bytes read, or -1 on error
- */
-ssize_t os_read_no_block(int fd, void *buf, size_t count);
-
/**
* Access to the OS write() system call
*