mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
Add timeout feature to expect()
This commit is contained in:
parent
3ad090caf7
commit
51300cc4f0
6 changed files with 60 additions and 34 deletions
29
src/misc.c
29
src/misc.c
|
|
@ -28,6 +28,7 @@
|
|||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include <sys/poll.h>
|
||||
#include "error.h"
|
||||
#include "print.h"
|
||||
#include "options.h"
|
||||
|
|
@ -112,3 +113,31 @@ bool regex_match(const char *string, const char *pattern)
|
|||
// Match
|
||||
return true;
|
||||
}
|
||||
|
||||
int read_poll(int fd, void *data, size_t len, int timeout)
|
||||
{
|
||||
struct pollfd fds;
|
||||
int ret = 0;
|
||||
|
||||
fds.events = POLLIN;
|
||||
fds.fd = fd;
|
||||
|
||||
/* Wait data available */
|
||||
ret = poll(&fds, 1, timeout);
|
||||
if (ret < 0)
|
||||
{
|
||||
tio_error_print("%s", strerror(errno));
|
||||
return ret;
|
||||
}
|
||||
else if (ret > 0)
|
||||
{
|
||||
if (fds.revents & POLLIN)
|
||||
{
|
||||
// Read ready data
|
||||
return read(fd, data, len);
|
||||
}
|
||||
}
|
||||
|
||||
/* Timeout */
|
||||
return ret;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue