fseek returns offset upon success instead of 0
Posted: Sat Jul 28, 2007 10:07 pm
I noticed this when I had fseek in an if statement, if fseek succeeds it is supposed to return 0 but it instead returns the offset of the file from the return of fioLseek. I fixed it by checking whether fioLseek returned with an error and setting ret to 0 if it had not. I'm not sure if that's the correct way to do it as I'm not familiar with fioLseek();, e.g. if it returns -1 when it doesn't succeed. This should fix fsetpos(); as well. I didn't have any problems building ps2sdk afterwards, but any code that relied on fseek returning an offset or code added afterwards that used fseek in a conditional statement might need fixing.
Edit:
I just took a look using codeblocks and there doesn't seem to be any code anywhere in ps2sdk that this fix would break. It should also fix the tests in test_fseek_ftell(); in stdio_tests.c.
More information:
http://www.opengroup.org/onlinepubs/007 ... fseek.html
should be
Edit:
I just took a look using codeblocks and there doesn't seem to be any code anywhere in ps2sdk that this fix would break. It should also fix the tests in test_fseek_ftell(); in stdio_tests.c.
More information:
http://www.opengroup.org/onlinepubs/007 ... fseek.html
Code: Select all
int fseek(FILE *stream, long offset, int origin)
{
int ret;
stream->has_putback = 0;
switch(stream->type) {
case STD_IOBUF_TYPE_NONE:
case STD_IOBUF_TYPE_GS:
case STD_IOBUF_TYPE_SIO:
case STD_IOBUF_TYPE_STDOUTHOST:
/* cannot seek stdout or stderr. */
ret = -1;
break;
default:
/* attempt to seek to offset from origin. */
ret = fioLseek(stream->fd, (int)offset, origin);
}
return (ret);
}
Code: Select all
int fseek(FILE *stream, long offset, int origin)
{
int ret;
stream->has_putback = 0;
switch(stream->type) {
case STD_IOBUF_TYPE_NONE:
case STD_IOBUF_TYPE_GS:
case STD_IOBUF_TYPE_SIO:
case STD_IOBUF_TYPE_STDOUTHOST:
/* cannot seek stdout or stderr. */
ret = -1;
break;
default:
/* attempt to seek to offset from origin. */
if((fioLseek(stream->fd, (int)offset, origin)) != -1) {
ret = 0;
}
else
ret = -1;
}
return (ret);
}