This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: _pread() _pwrite


Ronald Landheer-Cieslak wrote:
> 
> pread and pwrite are not in Cygwin (or at least are not exported by
> cygwin1.dll). They are hardly the most portable functions in the world. I
> suggest you write a wrapper. Something like:
> 
> <UNTESTED CODE>
> ssize_t pread(int fd, void *buf, size_t count, off_t offset)
> {
>         if (lseek(fd, offset, SEEK_SET) == (off_t)-1)
>         {
>                 return(-1);
>         }
>         return(read(fd, buf, count));
> }
> 
> and
> ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset)
> {
>         if (lseek(fd, offset, SEEK_SET) == (off_t)-1)
>         {
>                 return(-1);
>         }
>         return(write(fd, buf, count));
> }
> </UNTESTED CODE>

You'll want to save the original file position and restore it after the
operation, e.g.

ssize_t
pread(int fd, void *p, size_t n, off_t off)
{
        off_t ooff;
        int oerrno;

        if ((ooff  = lseek(fd, off, SEEK_SET)) == -1)
                return -1;

        n = read(fd, p, n);

        oerrno = errno;
        lseek(fd, ooff, SEEK_SET);
        errno = oerrno;

        return n;
}

ssize_t
pwrite(int fd, const void *p, size_t n, off_t off)
{
        off_t ooff;
        int oerrno;

        if ((ooff  = lseek(fd, off, SEEK_SET)) == -1)
                return -1;

        n = write(fd, p, n);

        oerrno = errno;
        lseek(fd, ooff, SEEK_SET);
        errno = oerrno;

        return n;
}

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]