Fix fgets(3) handling EOF incorrectly.
This commit is contained in:
parent
6aa4eb3405
commit
df67725221
|
@ -27,20 +27,19 @@
|
||||||
|
|
||||||
extern "C" char* fgets(char* dest, int size, FILE* fp)
|
extern "C" char* fgets(char* dest, int size, FILE* fp)
|
||||||
{
|
{
|
||||||
if ( size <= 0 ) { errno = EINVAL; return NULL; }
|
if ( size <= 0 )
|
||||||
|
return errno = EINVAL, (char*) NULL;
|
||||||
int i;
|
int i;
|
||||||
for ( i = 0; i < size-1; i++ )
|
for ( i = 0; i < size-1; i++ )
|
||||||
{
|
{
|
||||||
int c = getc(fp);
|
int c = getc(fp);
|
||||||
if ( c == EOF )
|
if ( c == EOF )
|
||||||
{
|
break;
|
||||||
if ( ferror(fp) ) { return NULL; }
|
|
||||||
else { i++; break; } /* EOF */
|
|
||||||
}
|
|
||||||
dest[i] = c;
|
dest[i] = c;
|
||||||
if ( c == '\n' ) { i++; break; }
|
if ( c == '\n' ) { i++; break; }
|
||||||
}
|
}
|
||||||
|
if ( !i && (ferror(fp) || feof(fp)) )
|
||||||
|
return NULL;
|
||||||
dest[i] = '\0';
|
dest[i] = '\0';
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue