delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2000/01/16/11:58:12

From: Martin Str|mberg <ams AT ludd DOT luth DOT se>
Message-Id: <200001161638.RAA19014@father.ludd.luth.se>
Subject: Re: _lleek
To: djgpp-workers AT delorie DOT com
Date: Sun, 16 Jan 100 17:38:32 +0100 (MET)
Cc: eliz AT is DOT elta DOT co DOT il (Eli Zaretskii)
In-Reply-To: <Pine.SUN.3.91.1000116161406.13764F-100000@is> from Eli Zaretskii at "Jan 16, 0 04:17:27 pm"
X-Mailer: ELM [version 2.4ME+ PL15 (25)]
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: dj-admin AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

According to Eli Zaretskii:
> If whence == SEEK_CUR and offset is positive, you could avoid the double 
> call, no?

Perhaps, but look at this and try to make sense out of it:

#include <unistd.h>
#include <stdio.h>
#include <limits.h>
#include <fcntl.h>
#include <sys/stat.h>

int main(int argc, char *argv[])
{
  char ch;
  char new_ch;
  int fd;
  long long ret;

  if( argc != 2
   || sscanf(argv[1], "%c", &new_ch) != 1
      )
  {
    fprintf(stderr, "%s: run like this '%s <char_to_write>'.\n",
            argv[0], argv[0]
            );
    exit(1);
  }

  fd = open("g:/ggg.grr", O_RDWR|O_CREAT, S_IWUSR);
  if( 0 <= fd )
  {
    printf("fd = %d.\n", fd);

    ret = lseek(fd, INT_MAX, SEEK_SET);
    printf("1: ret = %lld.\n", ret);
    ret = lseek(fd, INT_MAX, SEEK_CUR);
    printf("2: ret = %lld.\n", ret);
    ret = lseek(fd, INT_MAX, SEEK_CUR);
    printf("3: ret = %lld.\n", ret);

  }

  return(0);
}


Running it:
bash$ ./2 b
fd = 8.
1: ret = 2147483647.
2: ret = -2.
3: ret = 2147483645.
bash$

So it seems the file pointer wraps around at 2^32. Note particularly
that I never passed a negative offset, but still the file pointer went
from 2^32-2 to 2147483645 (-2 is really 2^32-2).

> >   if( whence == SEEK_SET )
> >   {
> >     if( offset < 0 )
> >     {
> >       offset = 0;
> >     }
> >     else if( MAX_FILE_POINTER_POSITION < offset )
> >     {
> >       offset = MAX_FILE_POINTER_POSITION;
> >     }
> >   }
> 
> What happens if you pass the offset unaltered to DOS?  Why should we 
> silently change the arguments, unless they somehow crash the system or 
> wipe the disk?

The total number of bits there are place for in the call (INT21,
AH=0x42) is 32. So passing bigger values than 2^32 will be the same as
passing the value modulus 2^32. The biggest size of a file in FAT is
2^32-1 <=> biggest offset is 2^32-2. Hence bigger values than 2^32-2
doesn't make sense.

So the clamping of offsets is done so the file pointer will be put in
a place that make sense. At least this way it makes sense to me.

As a matter of fact I think this is so much better that I'll suggest
that lseek() calls _llseek() internally.


Glass, Satyagraha,

							MartinS

- Raw text -


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