2021-12-10 07:25:00 +01:00
|
|
|
#include <unistd.h>
|
2021-12-13 18:38:47 +01:00
|
|
|
#include <gint/fs.h>
|
|
|
|
#include <errno.h>
|
2021-12-10 07:25:00 +01:00
|
|
|
|
|
|
|
ssize_t pread(int fd, void *buf, size_t size, off_t offset)
|
|
|
|
{
|
2022-01-06 14:05:52 +01:00
|
|
|
off_t current = lseek(fd, 0, SEEK_CUR);
|
|
|
|
if(current == (off_t)-1)
|
2021-12-13 18:38:47 +01:00
|
|
|
return (ssize_t)-1;
|
|
|
|
|
2022-01-06 14:05:52 +01:00
|
|
|
ssize_t rc = -1;
|
2021-12-13 18:38:47 +01:00
|
|
|
|
2022-01-06 14:05:52 +01:00
|
|
|
if(lseek(fd, offset, SEEK_SET) == (off_t)-1)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
rc = read(fd, buf, size);
|
|
|
|
if(rc < 0)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
end:
|
|
|
|
/* At the end, always try to restore the current position */
|
|
|
|
lseek(fd, current, SEEK_CUR);
|
|
|
|
return rc;
|
2021-12-10 07:25:00 +01:00
|
|
|
}
|