forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

fgetc() returns EOF before end of file.

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development
View previous topic :: View next topic  
Author Message
Mega Man



Joined: 18 Jun 2005
Posts: 274

PostPosted: Mon Sep 18, 2006 12:39 am    Post subject: fgetc() returns EOF before end of file. Reply with quote

When I open a binary file and try to read 0xFF with fgetc(), I get EOF (= 0xFFFFFFFF) instead.

The problem is in ps2sdk/ee/libc/src/stdio.c:
Code:

int fgetc(FILE *stream)
{
  char c;
  int ret;

  switch(stream->type) {
    case STD_IOBUF_TYPE_GS:
    case STD_IOBUF_TYPE_SIO:
    case STD_IOBUF_TYPE_STDOUTHOST:
      /* cannot read from stdout or stderr. */
      ret = EOF;
      break;
    default:
      ret = ((fread(&c, 1, 1, stream) == 1) ? (int)c : EOF);
  }
  return (ret);
}


"char c;" need to be replaced by "unsigned char c;", because casting to int will extend the sign. All negative values will be wrongly converted. Can someone change it in the svn repository?
Back to top
View user's profile Send private message Visit poster's website
evilo



Joined: 22 Apr 2004
Posts: 230

PostPosted: Mon Sep 18, 2006 7:59 pm    Post subject: Reply with quote

I'll check that tonight.

-> same thing also for getc()

did you run the non regression test present in the ps2sdk ?

regards,
Evilo.
_________________
http://psxdev.info/evilo/


Last edited by evilo on Tue Sep 19, 2006 5:19 am; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website
evilo



Joined: 22 Apr 2004
Posts: 230

PostPosted: Mon Sep 18, 2006 9:37 pm    Post subject: Reply with quote

Following your remark, I also noticed that fputc could be wrong as well:

Code:

int fputc(int c, FILE *stream)
{
  char ch;

  ch = (char)c;
  return ((fwrite(&ch, 1, 1, stream) == 1) ? 0 : EOF);
}
#endif


should better be :

Code:

int fputc(int c, FILE *stream)
{
  unsigned char ch = (unsigned char) c;

  return ((fwrite(&ch, 1, 1, stream) == 1) ? 0 : EOF);
}
#endif


do you confirm ?

[EDIT] While reviewing stdio.c, I also noticed that EOF is never raised in case the number of byte read (in fread) is smaller that the number of bytes requested... I guess nobody had an issue with that until now :)
_________________
http://psxdev.info/evilo/
Back to top
View user's profile Send private message Visit poster's website
Mega Man



Joined: 18 Jun 2005
Posts: 274

PostPosted: Tue Sep 19, 2006 7:31 am    Post subject: Reply with quote

evilo wrote:

do you confirm ?

Yes, because the linux programmer's manual says:
fputc() writes the character c, cast to an unsigned char, to stream. I think it is a good idea to be compatible to linux.

I didn't know anything about the testsuite before. Did you mean the test program in "ee/libc/regress". I run it and it is working, only stat() is not implemented. I think it should be possible to implement stat() by using fioGetstat() or fileXioGetStat().
Back to top
View user's profile Send private message Visit poster's website
evilo



Joined: 22 Apr 2004
Posts: 230

PostPosted: Wed Sep 20, 2006 6:28 am    Post subject: Reply with quote

I found the same error in ee/kernel/stdio.c

Code:

#ifdef F_fio_getc
int fioGetc(int fd)
{
   int c;

   fioRead(fd,&c,1);
   return c;
}
#endif



Else, back to stdio.c I also previously added a constant for the USB_MASS device

Code:

#define STD_IOBUF_TYPE_MASS           64


Code:

static struct {
  char * prefix;
  int len;
  int ret;
} __prefix_types[] = {
    { "cdrom0:", 7, STD_IOBUF_TYPE_CDROM },
    { "cdrom:",  6, STD_IOBUF_TYPE_CDROM },
    { "mc0:",    4, STD_IOBUF_TYPE_MC },
    { "mc1:",    4, STD_IOBUF_TYPE_MC },
    { "host0:",  6, STD_IOBUF_TYPE_HOST },
    { "host:",   5, STD_IOBUF_TYPE_HOST },
    { "mass0:",  6, STD_IOBUF_TYPE_MASS },
    { "mass:",   5, STD_IOBUF_TYPE_MASS },
    { 0, 0 }
};


so __stdio_get_fd_type() doesn't return -1 for that device. I know that this is not really required (since it's working the same anyway), but i find it a little cleaner like this :)

and yes I know, I should use "patch" to specify my files diff, but you know, i'm lazy !


any remarks from a "ps2sdk guru" before I commit everything ?
_________________
http://psxdev.info/evilo/
Back to top
View user's profile Send private message Visit poster's website
evilo



Joined: 22 Apr 2004
Posts: 230

PostPosted: Mon Nov 27, 2006 2:04 am    Post subject: Reply with quote

commited !
_________________
http://psxdev.info/evilo/
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group