libc: Don't fclose and fopen file in freopen

to avoid FILE pointer change after this commit:
commit b0797263ca
Author: Xiang Xiao <xiaoxiang@xiaomi.com>
Date:   Thu Aug 13 18:17:29 2020 +0800

    libc/stdio: Allocate file_struct dynamically

    1.Reduce the default size of task_group_s(~512B each task)
    2.Scale better between simple and complex application

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2020-10-15 21:39:10 +08:00 committed by Brennan Ashton
parent bc14716d03
commit 9928088868

View File

@ -94,16 +94,39 @@ FAR FILE *freopen(FAR const char *path, FAR const char *mode,
if (path != NULL)
{
/* Yes, close the stream */
/* Yes, open the file directly if no stream is opened yet */
if (stream)
if (stream == NULL)
{
fclose(stream);
return fopen(path, mode);
}
/* And attempt to reopen the file at the provided path */
/* Otherwise, open the file */
return fopen(path, mode);
oflags = lib_mode2oflags(mode);
if (oflags < 0)
{
return NULL;
}
fd = open(path, oflags, 0666);
if (fd < 0)
{
return NULL;
}
/* Flush the stream and duplicate the new fd to it */
fflush(stream);
ret = dup2(fd, fileno(stream));
close(fd);
if (ret < 0)
{
return NULL;
}
clearerr(stream);
return stream;
}
/* Otherwise, we are just changing the mode of the current file. */