util: add a errno wrapper

Converts into a negative errno or the respective value

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2020-08-12 13:48:10 +10:00
parent 8cdbcda7a7
commit ce91763683

View file

@ -32,6 +32,17 @@
#include <unistd.h>
#include <sys/socket.h>
/**
* Wrapper to convert an errno-setting syscall into a
* value-or-negative-errno.
*
* Use: int rc = xerrno(foo(bar));
*/
static inline int
xerrno(int value) {
return value < 0 ? -errno : value;
}
/**
* Wrapper around close(). It checks for fd != -1 to satisfy coverity and
* friends and always returns -1.
@ -49,8 +60,7 @@ xclose(int fd) {
static inline int
xread(int fd, void *buf, size_t count)
{
int rc = read(fd, buf, count);
return rc == -1 ? -errno : rc;
return xerrno(read(fd, buf, count));
}
/**
@ -60,8 +70,7 @@ xread(int fd, void *buf, size_t count)
static inline int
xwrite(int fd, const void *buf, size_t count)
{
int rc = write(fd, buf, count);
return rc == -1 ? -errno : rc;
return xerrno(write(fd, buf, count));
}
/**
@ -71,8 +80,7 @@ xwrite(int fd, const void *buf, size_t count)
static inline int
xsend(int fd, const void *buf, size_t len)
{
int rc = send(fd, buf, len, MSG_NOSIGNAL);
return rc == -1 ? -errno : rc;
return xerrno(send(fd, buf, len, MSG_NOSIGNAL));
}
/* consider this struct opaque */