delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2003/06/26/06:10:48

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Message-ID: <3EFAC669.30325C05@dessent.net>
Date: Thu, 26 Jun 2003 03:09:45 -0700
From: Brian Dessent <brian AT dessent DOT net>
Organization: My own little world...
X-Accept-Language: en,pdf
MIME-Version: 1.0
To: "Cygwin AT Cygwin DOT Com" <cygwin AT cygwin DOT com>
Subject: Re: _pread() _pwrite
References: <Pine DOT LNX DOT 4 DOT 44 DOT 0306261146310 DOT 2545-100000 AT localhost DOT localdomain>

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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019