This is the mail archive of the
mailing list for the Cygwin project.
Re: lseek + read = ENOENT
- From: Cliff Hones <cliff at hones dot org dot uk>
- To: cygwin at cygwin dot com
- Date: Fri, 20 Jan 2006 00:59:40 +0000
- Subject: Re: lseek + read = ENOENT
- References: <firstname.lastname@example.org>
Sam Steingold wrote:
> I cannot read the last 4-byte word in a file using lseek + read:
> /* file "foo" exists and is large enough - say, 4 MB */
> int fd = open("foo",O_RDONLY|O_BINARY);
> uint32 data;
> /* this succeeds and correctly returns the size of file "foo" minus 4 */
> /* this returns 0 -- instead of the expected 4 -- and sets errno to ENOENT */
> if I run this under gdb and type
> several times, eventually read() starts to return 4 and set data to the
> value I actually wrote into "foo" last.
> I observe this on linux, cygwin and solaris -- what am I doing wrong?
This seems to be a bug in gcc. The off_t argument to lseek is a 64-bit
type, but instead of being sign-extended to 64 bits, the value passed
(-sizeof(data)) passed is only extended to 32-bits, so is actually +4294967292.
If you write:
int n = -sizeof(data);
lseek(fd, n, SEEK_END);
it works as expected.
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Problem reports: http://cygwin.com/problems.html