Workshop o mikrokontrolérech na SKSP 2024.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.0 KiB

3 months ago
/*
* UCW Library -- Careful Read/Write
*
* (c) 2004--2012 Martin Mares <mj@ucw.cz>
* (c) 2020 Jan Filip Chadima <jfch@jagda.eu>
*
* This software may be freely distributed and used according to the terms
* of the GNU Lesser General Public License.
*/
#include <ucw/lib.h>
#include <ucw/io.h>
#include <errno.h>
#include <unistd.h>
/*
* Reads and writes on sockets and pipes can return partial results,
* so we implement an iterated read/write call.
*/
int
careful_read(int fd, void *buf, size_t len)
{
byte *pos = buf;
while (len)
{
ssize_t l = read(fd, pos, len);
if (l < 0)
{
if (errno == EINTR)
continue;
return -1;
}
if (!l)
return 0;
pos += l;
len -= l;
}
return 1;
}
int
careful_write(int fd, const void *buf, size_t len)
{
const byte *pos = buf;
while (len)
{
ssize_t l = write(fd, pos, len);
if (l < 0)
{
if (errno == EINTR)
continue;
return -1;
}
if (!l)
return 0;
pos += l;
len -= l;
}
return 1;
}