Skip to content

Commit

Permalink
EINTR_RETRY macro to replace CHECK_ERRNO.
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim2266 committed Dec 28, 2021
1 parent 0fc9cff commit 7336216
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions str.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ void str_free(const str s)
})

// errno checker
#define CHECK_ERRNO() do { const int __err = errno; if(__err != EINTR) return __err; } while(0)
#define EINTR_RETRY(expr) \
while((expr) < 0) do { const int __err = errno; if(__err != EINTR) return __err; } while(0)

// swap
void str_swap(str* const s1, str* const s2)
Expand Down Expand Up @@ -207,8 +208,7 @@ int get_file_size(const int fd, off_t* const size)
// stat the file
struct stat info;

while(fstat(fd, &info) == -1)
CHECK_ERRNO();
EINTR_RETRY(fstat(fd, &info));

*size = info.st_size;

Expand All @@ -232,8 +232,7 @@ int read_from_fd(const int fd, void* p, off_t* const psize)

do
{
while((n = read(fd, p, end - p)) < 0)
CHECK_ERRNO();
EINTR_RETRY(n = read(fd, p, end - p));

p += n;
} while(n > 0 && p < end);
Expand Down Expand Up @@ -280,8 +279,7 @@ int str_from_file(str* const dest, const char* const file_name)
{
int fd;

while((fd = open(file_name, O_CLOEXEC | O_RDONLY)) < 0)
CHECK_ERRNO();
EINTR_RETRY(fd = open(file_name, O_CLOEXEC | O_RDONLY));

off_t size = 0;
int err = get_file_size(fd, &size);
Expand Down Expand Up @@ -355,8 +353,7 @@ int _str_cpy_to_fd(const int fd, const str s)
{
ssize_t m;

while((m = write(fd, p, n)) < 0)
CHECK_ERRNO();
EINTR_RETRY(m = write(fd, p, n));

n -= m;
p += m;
Expand All @@ -381,8 +378,7 @@ int write_iovec(const int fd, struct iovec* pv, unsigned nv)
{
ssize_t n;

while((n = writev(fd, pv, nv)) < 0)
CHECK_ERRNO();
EINTR_RETRY(n = writev(fd, pv, nv));

// discard items already written
for(; nv > 0; ++pv, --nv)
Expand Down

0 comments on commit 7336216

Please sign in to comment.