mirror of
https://git.planet-casio.com/Vhex-Kernel-Core/fxlibc.git
synced 2024-12-29 13:03:38 +01:00
stdio: fixes for initial fread() et fwrite() (TEST)
This commit is contained in:
parent
4461bdb96a
commit
909c7df815
7 changed files with 33 additions and 21 deletions
|
@ -6,6 +6,7 @@ FILE *fdopen(int fd, char const *mode)
|
||||||
{
|
{
|
||||||
FILE *fp = calloc(1, sizeof *fp);
|
FILE *fp = calloc(1, sizeof *fp);
|
||||||
if(!fp) goto err;
|
if(!fp) goto err;
|
||||||
|
fp->fd = -1;
|
||||||
|
|
||||||
int flags = __fp_parse_mode(mode, fp);
|
int flags = __fp_parse_mode(mode, fp);
|
||||||
if(flags < 0) goto err;
|
if(flags < 0) goto err;
|
||||||
|
@ -17,8 +18,6 @@ FILE *fdopen(int fd, char const *mode)
|
||||||
return fp;
|
return fp;
|
||||||
|
|
||||||
err:
|
err:
|
||||||
if(fp && fp->bufowned)
|
__fp_close(fp, true);
|
||||||
free(fp->buf);
|
|
||||||
free(fp);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,5 +2,12 @@
|
||||||
|
|
||||||
int feof(FILE *fp)
|
int feof(FILE *fp)
|
||||||
{
|
{
|
||||||
|
if(!fp->buf)
|
||||||
|
return fp->eof;
|
||||||
|
|
||||||
|
/* If there is read data to be accessed, the flag doesn't count */
|
||||||
|
if(fp->bufdir == __FILE_BUF_READ && fp->bufpos < fp->bufread)
|
||||||
|
return 0;
|
||||||
|
else
|
||||||
return fp->eof;
|
return fp->eof;
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,8 +25,9 @@ int fflush(FILE *fp)
|
||||||
/* In writing mode, write pending data */
|
/* In writing mode, write pending data */
|
||||||
if(fp->bufdir == __FILE_BUF_WRITE && fp->bufpos > 0) {
|
if(fp->bufdir == __FILE_BUF_WRITE && fp->bufpos > 0) {
|
||||||
ssize_t written = __fp_write(fp, fp->buf, fp->bufpos);
|
ssize_t written = __fp_write(fp, fp->buf, fp->bufpos);
|
||||||
|
int rc = (written == (ssize_t)fp->bufpos ? 0 : EOF);
|
||||||
fp->bufpos = 0;
|
fp->bufpos = 0;
|
||||||
return (written == (ssize_t)fp->bufpos ? 0 : EOF);
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -21,8 +21,13 @@ int __fp_open(FILE *fp, int fd, bool use_buffering)
|
||||||
|
|
||||||
void __fp_close(FILE *fp, bool free_fp)
|
void __fp_close(FILE *fp, bool free_fp)
|
||||||
{
|
{
|
||||||
|
if(!fp)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if(fp->fd >= 0) {
|
||||||
fflush(fp);
|
fflush(fp);
|
||||||
close(fp->fd);
|
close(fp->fd);
|
||||||
|
}
|
||||||
if(fp->bufowned)
|
if(fp->bufowned)
|
||||||
free(fp->buf);
|
free(fp->buf);
|
||||||
if(free_fp)
|
if(free_fp)
|
||||||
|
@ -31,20 +36,19 @@ void __fp_close(FILE *fp, bool free_fp)
|
||||||
|
|
||||||
void __fp_buffer_mode_read(FILE *fp)
|
void __fp_buffer_mode_read(FILE *fp)
|
||||||
{
|
{
|
||||||
if(fp->buf && fp->bufdir == __FILE_BUF_WRITE && fp->bufpos > 0) {
|
if(fp->bufmode != _IONBF && fp->bufdir == __FILE_BUF_WRITE &&
|
||||||
|
fp->bufpos > 0)
|
||||||
fflush(fp);
|
fflush(fp);
|
||||||
fp->bufdir = __FILE_BUF_READ;
|
fp->bufdir = __FILE_BUF_READ;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void __fp_buffer_mode_write(FILE *fp)
|
void __fp_buffer_mode_write(FILE *fp)
|
||||||
{
|
{
|
||||||
if(fp->buf && fp->bufdir == __FILE_BUF_READ
|
if(fp->bufmode != _IONBF && fp->bufdir == __FILE_BUF_READ &&
|
||||||
&& fp->bufpos < fp->bufread) {
|
fp->bufpos < fp->bufread)
|
||||||
fflush(fp);
|
fflush(fp);
|
||||||
fp->bufdir = __FILE_BUF_WRITE;
|
fp->bufdir = __FILE_BUF_WRITE;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t __fp_read(FILE *fp, void *data, size_t size)
|
ssize_t __fp_read(FILE *fp, void *data, size_t size)
|
||||||
{
|
{
|
||||||
|
@ -63,6 +67,7 @@ ssize_t __fp_read(FILE *fp, void *data, size_t size)
|
||||||
}
|
}
|
||||||
|
|
||||||
fp->fdpos += rc;
|
fp->fdpos += rc;
|
||||||
|
read_ += rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
return read_;
|
return read_;
|
||||||
|
@ -83,6 +88,7 @@ ssize_t __fp_write(FILE *fp, void const *data, size_t size)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
fp->fdpos += rc;
|
fp->fdpos += rc;
|
||||||
|
written += rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
return written;
|
return written;
|
||||||
|
|
|
@ -7,6 +7,7 @@ FILE *fopen(char const * restrict filename, char const * restrict mode)
|
||||||
{
|
{
|
||||||
FILE *fp = calloc(1, sizeof *fp);
|
FILE *fp = calloc(1, sizeof *fp);
|
||||||
if(!fp) goto err;
|
if(!fp) goto err;
|
||||||
|
fp->fd = -1;
|
||||||
|
|
||||||
int flags = __fp_parse_mode(mode, fp);
|
int flags = __fp_parse_mode(mode, fp);
|
||||||
if(flags < 0) goto err;
|
if(flags < 0) goto err;
|
||||||
|
@ -18,8 +19,6 @@ FILE *fopen(char const * restrict filename, char const * restrict mode)
|
||||||
return fp;
|
return fp;
|
||||||
|
|
||||||
err:
|
err:
|
||||||
if(fp && fp->bufowned)
|
__fp_close(fp, true);
|
||||||
free(fp->buf);
|
|
||||||
free(fp);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,8 @@ size_t fread(void *data, size_t membsize, size_t nmemb, FILE *fp)
|
||||||
|
|
||||||
size_t size_read = 0;
|
size_t size_read = 0;
|
||||||
while(size_read < size) {
|
while(size_read < size) {
|
||||||
/* Take what's available in the buffer (may be 0) */
|
/* Take what's available in the buffer (may be 0, but only
|
||||||
|
during the first iteration) */
|
||||||
size_t size_frag = fp->bufread - fp->bufpos;
|
size_t size_frag = fp->bufread - fp->bufpos;
|
||||||
if(size_frag > size - size_read)
|
if(size_frag > size - size_read)
|
||||||
size_frag = size - size_read;
|
size_frag = size - size_read;
|
||||||
|
|
|
@ -9,6 +9,7 @@ FILE *freopen(char const * restrict filename, char const * restrict mode,
|
||||||
{
|
{
|
||||||
__fp_close(fp, false);
|
__fp_close(fp, false);
|
||||||
memset(fp, 0, sizeof *fp);
|
memset(fp, 0, sizeof *fp);
|
||||||
|
fp->fd = -1;
|
||||||
|
|
||||||
int flags = __fp_parse_mode(mode, fp);
|
int flags = __fp_parse_mode(mode, fp);
|
||||||
if(flags < 0) goto err;
|
if(flags < 0) goto err;
|
||||||
|
@ -20,8 +21,6 @@ FILE *freopen(char const * restrict filename, char const * restrict mode,
|
||||||
return fp;
|
return fp;
|
||||||
|
|
||||||
err:
|
err:
|
||||||
if(fp && fp->bufowned)
|
__fp_close(fp, true);
|
||||||
free(fp->buf);
|
|
||||||
free(fp);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue