2015-11-22 15:38:55 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/stdio/lib_freopen.c
|
2015-11-22 15:38:55 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2015-11-22 15:38:55 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-11-22 15:38:55 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2015-11-22 15:38:55 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "libc.h"
|
2015-11-22 15:38:55 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: freopen
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Reuses stream to either open the file specified by path or to change
|
|
|
|
* its access mode.
|
|
|
|
*
|
|
|
|
* If a new path is specified, the function first attempts to close
|
|
|
|
* any file already associated with stream (third parameter) and
|
|
|
|
* disassociates it. Then, independently of whether that stream was
|
2020-02-22 19:31:14 +01:00
|
|
|
* successfully closed or not, freopen opens the file specified by path
|
2015-11-22 15:38:55 +01:00
|
|
|
* and associates it with the stream just as fopen would do using the
|
|
|
|
* specified mode.
|
|
|
|
*
|
|
|
|
* If path is a null pointer, the function attempts to change the mode
|
|
|
|
* of the stream. Although a particular library implementation is allowed
|
|
|
|
* to restrict the changes permitted, and under which circumstances.
|
|
|
|
*
|
|
|
|
* The error indicator and eof indicator are automatically cleared (as if
|
|
|
|
* clearerr was called).
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2015-11-22 15:38:55 +01:00
|
|
|
* path - If non-NULL, refers to the name of the file to be opened.
|
|
|
|
* mode - String describing the new file access mode
|
|
|
|
* stream - Pointer to the type FILE to be reopened.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* If the file is successfully reopened, the function returns the pointer
|
|
|
|
* passed as parameter stream, which can be used to identify the reopened
|
|
|
|
* stream. Otherwise, a null pointer is returned and the errno variable
|
|
|
|
* is also set to a system-specific error code on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR FILE *freopen(FAR const char *path, FAR const char *mode,
|
|
|
|
FAR FILE *stream)
|
|
|
|
{
|
|
|
|
int oflags;
|
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* Was a file name provided? */
|
|
|
|
|
|
|
|
if (path != NULL)
|
|
|
|
{
|
2020-10-15 15:39:10 +02:00
|
|
|
/* Yes, open the file directly if no stream is opened yet */
|
2015-11-22 15:38:55 +01:00
|
|
|
|
2020-10-15 15:39:10 +02:00
|
|
|
if (stream == NULL)
|
2015-11-22 15:38:55 +01:00
|
|
|
{
|
2020-10-15 15:39:10 +02:00
|
|
|
return fopen(path, mode);
|
2015-11-22 15:38:55 +01:00
|
|
|
}
|
|
|
|
|
2020-10-15 15:39:10 +02:00
|
|
|
/* Otherwise, open the file */
|
2015-11-22 15:38:55 +01:00
|
|
|
|
2020-10-15 15:39:10 +02:00
|
|
|
oflags = lib_mode2oflags(mode);
|
|
|
|
if (oflags < 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(path, oflags, 0666);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-08-24 05:43:57 +02:00
|
|
|
/* Make sure that we have exclusive access to the stream */
|
|
|
|
|
|
|
|
flockfile(stream);
|
|
|
|
|
2022-10-18 12:19:10 +02:00
|
|
|
/* Flush the stream and invalidate the read buffer. */
|
2020-10-15 15:39:10 +02:00
|
|
|
|
2023-08-24 05:43:57 +02:00
|
|
|
lib_fflush_unlocked(stream, true);
|
2022-10-18 12:19:10 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
|
2023-08-24 05:43:57 +02:00
|
|
|
lib_rdflush_unlocked(stream);
|
2022-10-18 12:19:10 +02:00
|
|
|
#endif
|
|
|
|
|
2023-08-24 05:43:57 +02:00
|
|
|
funlockfile(stream);
|
|
|
|
|
2022-10-18 12:19:10 +02:00
|
|
|
/* Duplicate the new fd to the stream. */
|
|
|
|
|
2020-10-15 15:39:10 +02:00
|
|
|
ret = dup2(fd, fileno(stream));
|
|
|
|
close(fd);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearerr(stream);
|
|
|
|
return stream;
|
2015-11-22 15:38:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, we are just changing the mode of the current file. */
|
|
|
|
|
|
|
|
if (stream)
|
|
|
|
{
|
|
|
|
/* Convert the mode string into standard file open mode flags. */
|
|
|
|
|
|
|
|
oflags = lib_mode2oflags(mode);
|
2017-12-21 19:04:17 +01:00
|
|
|
if (oflags < 0)
|
2015-11-22 15:38:55 +01:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the underlying file descriptor from the stream */
|
|
|
|
|
|
|
|
fd = fileno(stream);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the new file mode for the file descriptor */
|
|
|
|
|
|
|
|
ret = fcntl(fd, F_SETFL, oflags);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearerr(stream);
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_errno(EINVAL);
|
|
|
|
return NULL;
|
|
|
|
}
|