Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions libs/libc/stdio/lib_freopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,6 @@ FAR FILE *freopen(FAR const char *path, FAR const char *mode,
return NULL;
}

fd = open(path, oflags, 0666);
if (fd < 0)
{
return NULL;
}

/* Make sure that we have exclusive access to the stream */

flockfile(stream);
Expand All @@ -117,11 +111,20 @@ FAR FILE *freopen(FAR const char *path, FAR const char *mode,

funlockfile(stream);

/* Duplicate the new fd to the stream. */
/* close the old fd */

ret = dup2(fd, fileno(stream));
close(fd);
if (ret < 0)
close(fileno(stream));

/* Open the new file and reused the fd */

fd = open(path, oflags, 0666);
flockfile(stream);
stream->fs_cookie = (FAR void *)(intptr_t)fd;
funlockfile(stream);

/* To clear the stale fd */

if (fd < 0)
{
return NULL;
}
Expand Down
Loading