stdio: fixes for initial fread() et fwrite() (TEST)

This commit is contained in:
Lephenixnoir 2022-01-10 17:10:49 +01:00
parent 4461bdb96a
commit 909c7df815
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495
7 changed files with 33 additions and 21 deletions

View file

@ -6,6 +6,7 @@ FILE *fdopen(int fd, char const *mode)
{
FILE *fp = calloc(1, sizeof *fp);
if(!fp) goto err;
fp->fd = -1;
int flags = __fp_parse_mode(mode, fp);
if(flags < 0) goto err;
@ -17,8 +18,6 @@ FILE *fdopen(int fd, char const *mode)
return fp;
err:
if(fp && fp->bufowned)
free(fp->buf);
free(fp);
__fp_close(fp, true);
return NULL;
}

View file

@ -2,5 +2,12 @@
int feof(FILE *fp)
{
return fp->eof;
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;
}

View file

@ -25,8 +25,9 @@ int fflush(FILE *fp)
/* In writing mode, write pending data */
if(fp->bufdir == __FILE_BUF_WRITE && fp->bufpos > 0) {
ssize_t written = __fp_write(fp, fp->buf, fp->bufpos);
int rc = (written == (ssize_t)fp->bufpos ? 0 : EOF);
fp->bufpos = 0;
return (written == (ssize_t)fp->bufpos ? 0 : EOF);
return rc;
}
return 0;

View file

@ -21,8 +21,13 @@ int __fp_open(FILE *fp, int fd, bool use_buffering)
void __fp_close(FILE *fp, bool free_fp)
{
fflush(fp);
close(fp->fd);
if(!fp)
return;
if(fp->fd >= 0) {
fflush(fp);
close(fp->fd);
}
if(fp->bufowned)
free(fp->buf);
if(free_fp)
@ -31,19 +36,18 @@ void __fp_close(FILE *fp, bool free_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);
fp->bufdir = __FILE_BUF_READ;
}
fp->bufdir = __FILE_BUF_READ;
}
void __fp_buffer_mode_write(FILE *fp)
{
if(fp->buf && fp->bufdir == __FILE_BUF_READ
&& fp->bufpos < fp->bufread) {
if(fp->bufmode != _IONBF && fp->bufdir == __FILE_BUF_READ &&
fp->bufpos < fp->bufread)
fflush(fp);
fp->bufdir = __FILE_BUF_WRITE;
}
fp->bufdir = __FILE_BUF_WRITE;
}
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;
read_ += rc;
}
return read_;
@ -83,6 +88,7 @@ ssize_t __fp_write(FILE *fp, void const *data, size_t size)
break;
fp->fdpos += rc;
written += rc;
}
return written;

View file

@ -7,6 +7,7 @@ FILE *fopen(char const * restrict filename, char const * restrict mode)
{
FILE *fp = calloc(1, sizeof *fp);
if(!fp) goto err;
fp->fd = -1;
int flags = __fp_parse_mode(mode, fp);
if(flags < 0) goto err;
@ -18,8 +19,6 @@ FILE *fopen(char const * restrict filename, char const * restrict mode)
return fp;
err:
if(fp && fp->bufowned)
free(fp->buf);
free(fp);
__fp_close(fp, true);
return NULL;
}

View file

@ -19,7 +19,8 @@ size_t fread(void *data, size_t membsize, size_t nmemb, FILE *fp)
size_t size_read = 0;
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;
if(size_frag > size - size_read)
size_frag = size - size_read;

View file

@ -9,6 +9,7 @@ FILE *freopen(char const * restrict filename, char const * restrict mode,
{
__fp_close(fp, false);
memset(fp, 0, sizeof *fp);
fp->fd = -1;
int flags = __fp_parse_mode(mode, fp);
if(flags < 0) goto err;
@ -20,8 +21,6 @@ FILE *freopen(char const * restrict filename, char const * restrict mode,
return fp;
err:
if(fp && fp->bufowned)
free(fp->buf);
free(fp);
__fp_close(fp, true);
return NULL;
}