Mail Archives: djgpp-workers/2000/01/16/11:58:12
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 -