2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/stdio/lib_fopen.c
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2020-05-23 06:58:52 +02: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
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2020-05-23 06:58:52 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2020-05-23 06:58:52 +02: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.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2014-01-08 00:18:38 +01:00
|
|
|
#include <assert.h>
|
2012-11-10 17:06:01 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2023-05-08 01:37:45 +02:00
|
|
|
#ifdef CONFIG_FDSAN
|
|
|
|
# include <android/fdsan.h>
|
|
|
|
#endif
|
|
|
|
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "libc.h"
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-01-08 00:18:38 +01:00
|
|
|
/* Open mode flags */
|
|
|
|
|
|
|
|
#define MODE_R (1 << 0) /* Bit 0: "r{b|x|+}" open for reading */
|
|
|
|
#define MODE_W (1 << 1) /* Bit 1: "w{b|x|+}" open for writing, truncating,
|
|
|
|
* or creating file */
|
|
|
|
#define MODE_A (1 << 2) /* Bit 2: "a{b|x|+}" open for writing, appending
|
|
|
|
* the to file */
|
|
|
|
|
|
|
|
#define MODE_NONE 0 /* No access mode determined */
|
|
|
|
#define MODE_MASK (MODE_R | MODE_W | MODE_A)
|
|
|
|
|
2022-06-02 18:23:24 +02:00
|
|
|
#define FLAG_KEEP (O_TEXT | O_CLOEXEC | O_EXCL)
|
2022-06-02 08:26:49 +02:00
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2015-11-22 15:38:55 +01:00
|
|
|
* Public Functions
|
2012-11-10 17:06:01 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-11-22 15:38:55 +01:00
|
|
|
* Name: fdopen
|
2012-11-10 17:06:01 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-11-22 15:38:55 +01:00
|
|
|
FAR FILE *fdopen(int fd, FAR const char *mode)
|
|
|
|
{
|
2023-10-15 09:47:12 +02:00
|
|
|
FAR struct streamlist *list = lib_get_streams();
|
2020-05-23 06:58:52 +02:00
|
|
|
FAR FILE *filep = NULL;
|
2015-11-22 15:38:55 +01:00
|
|
|
int oflags;
|
2020-05-23 06:58:52 +02:00
|
|
|
int ret;
|
2015-11-22 15:38:55 +01:00
|
|
|
|
|
|
|
/* Map the open mode string to open flags */
|
|
|
|
|
|
|
|
oflags = lib_mode2oflags(mode);
|
2023-10-15 09:47:12 +02:00
|
|
|
if (oflags < 0)
|
2015-11-22 15:38:55 +01:00
|
|
|
{
|
2023-10-15 09:47:12 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate FILE structure */
|
|
|
|
|
|
|
|
if (fd >= 3)
|
|
|
|
{
|
|
|
|
filep = lib_zalloc(sizeof(FILE));
|
|
|
|
if (filep == NULL)
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add FILE structure to the stream list */
|
|
|
|
|
|
|
|
ret = nxmutex_lock(&list->sl_lock);
|
2020-05-23 06:58:52 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-10-15 09:47:12 +02:00
|
|
|
lib_free(filep);
|
|
|
|
goto errout;
|
2020-05-23 06:58:52 +02:00
|
|
|
}
|
2023-10-15 09:47:12 +02:00
|
|
|
|
|
|
|
if (list->sl_tail)
|
|
|
|
{
|
|
|
|
list->sl_tail->fs_next = filep;
|
|
|
|
list->sl_tail = filep;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
list->sl_head = filep;
|
|
|
|
list->sl_tail = filep;
|
|
|
|
}
|
|
|
|
|
|
|
|
nxmutex_unlock(&list->sl_lock);
|
|
|
|
|
|
|
|
/* Initialize the mutex the manages access to the buffer */
|
|
|
|
|
|
|
|
nxrmutex_init(&filep->fs_lock);
|
2015-11-22 15:38:55 +01:00
|
|
|
}
|
2023-10-15 09:47:12 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
filep = &list->sl_std[fd];
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(CONFIG_STDIO_DISABLE_BUFFERING) && CONFIG_STDIO_BUFFER_SIZE > 0
|
|
|
|
/* Set up pointers */
|
|
|
|
|
|
|
|
filep->fs_bufstart = filep->fs_buffer;
|
|
|
|
filep->fs_bufend = filep->fs_bufstart + CONFIG_STDIO_BUFFER_SIZE;
|
|
|
|
filep->fs_bufpos = filep->fs_bufstart;
|
|
|
|
filep->fs_bufread = filep->fs_bufstart;
|
|
|
|
filep->fs_flags = __FS_FLAG_UBF; /* Fake setvbuf and fclose */
|
|
|
|
|
|
|
|
# ifdef CONFIG_STDIO_LINEBUFFER
|
|
|
|
/* Setup buffer flags */
|
|
|
|
|
|
|
|
filep->fs_flags |= __FS_FLAG_LBF; /* Line buffering */
|
|
|
|
|
|
|
|
# endif /* CONFIG_STDIO_LINEBUFFER */
|
|
|
|
#endif /* !CONFIG_STDIO_DISABLE_BUFFERING && CONFIG_STDIO_BUFFER_SIZE > 0 */
|
|
|
|
|
|
|
|
/* Save the file description and open flags. Setting the
|
|
|
|
* file descriptor locks this stream.
|
|
|
|
*/
|
|
|
|
|
2023-08-27 21:33:44 +02:00
|
|
|
filep->fs_cookie = (FAR void *)(intptr_t)fd;
|
2023-10-15 09:47:12 +02:00
|
|
|
filep->fs_oflags = oflags;
|
2015-11-22 15:38:55 +01:00
|
|
|
|
2023-05-18 12:21:45 +02:00
|
|
|
#ifdef CONFIG_FDSAN
|
|
|
|
android_fdsan_exchange_owner_tag(fd, 0,
|
|
|
|
android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_FILE,
|
|
|
|
(uintptr_t)filep));
|
|
|
|
#endif
|
|
|
|
|
2023-08-27 21:33:44 +02:00
|
|
|
/* Assign custom callbacks to NULL. */
|
|
|
|
|
|
|
|
filep->fs_iofunc.read = NULL;
|
|
|
|
filep->fs_iofunc.write = NULL;
|
|
|
|
filep->fs_iofunc.seek = NULL;
|
|
|
|
filep->fs_iofunc.close = NULL;
|
|
|
|
|
2020-05-23 06:58:52 +02:00
|
|
|
return filep;
|
2023-10-15 09:47:12 +02:00
|
|
|
|
|
|
|
errout:
|
|
|
|
set_errno(-ret);
|
|
|
|
return NULL;
|
2015-11-22 15:38:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fopen
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR FILE *fopen(FAR const char *path, FAR const char *mode)
|
|
|
|
{
|
2020-05-23 06:58:52 +02:00
|
|
|
FAR FILE *filep = NULL;
|
2015-11-22 15:38:55 +01:00
|
|
|
int oflags;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* Map the open mode string to open flags */
|
|
|
|
|
|
|
|
oflags = lib_mode2oflags(mode);
|
|
|
|
if (oflags < 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the file */
|
|
|
|
|
|
|
|
fd = open(path, oflags, 0666);
|
|
|
|
|
2018-09-15 18:49:41 +02:00
|
|
|
/* If the open was successful, then call fdopen() using the file
|
2015-11-22 15:38:55 +01:00
|
|
|
* descriptor returned by open. If open failed, then just return the
|
|
|
|
* NULL stream -- open() has already set the errno.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
2023-10-15 09:47:12 +02:00
|
|
|
filep = fdopen(fd, mode);
|
|
|
|
if (filep == NULL)
|
2015-11-22 15:38:55 +01:00
|
|
|
{
|
|
|
|
/* Don't forget to close the file descriptor if any other
|
|
|
|
* failures are reported by fdopen().
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
close(fd);
|
2015-11-22 15:38:55 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-15 18:49:41 +02:00
|
|
|
|
2020-05-23 06:58:52 +02:00
|
|
|
return filep;
|
2015-11-22 15:38:55 +01:00
|
|
|
}
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: lib_mode2oflags
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-11-22 15:38:55 +01:00
|
|
|
int lib_mode2oflags(FAR const char *mode)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2014-01-08 00:18:38 +01:00
|
|
|
unsigned int state;
|
2012-11-10 17:06:01 +01:00
|
|
|
int oflags;
|
|
|
|
|
2014-01-08 00:18:38 +01:00
|
|
|
/* Verify that a mode string was provided. */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2014-01-08 00:18:38 +01:00
|
|
|
DEBUGASSERT(mode);
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* Parse the mode string to determine the corresponding open flags */
|
|
|
|
|
|
|
|
state = MODE_NONE;
|
|
|
|
oflags = 0;
|
|
|
|
|
|
|
|
for (; *mode; mode++)
|
|
|
|
{
|
|
|
|
switch (*mode)
|
|
|
|
{
|
2023-08-21 15:49:05 +02:00
|
|
|
/* Open for read access ("r{m|b|x|+}") */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
case 'r' :
|
|
|
|
if (state == MODE_NONE)
|
|
|
|
{
|
|
|
|
/* Open for read access */
|
|
|
|
|
2022-06-02 18:23:24 +02:00
|
|
|
oflags = O_RDOK | O_TEXT;
|
2012-11-10 17:06:01 +01:00
|
|
|
state = MODE_R;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-01-08 00:18:38 +01:00
|
|
|
/* Open for write access ("w{b|x|+}") */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
case 'w' :
|
|
|
|
if (state == MODE_NONE)
|
|
|
|
{
|
|
|
|
/* Open for write access, truncating any existing file */
|
|
|
|
|
2022-06-02 18:23:24 +02:00
|
|
|
oflags = O_WROK | O_CREAT | O_TRUNC | O_TEXT;
|
2012-11-10 17:06:01 +01:00
|
|
|
state = MODE_W;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-01-08 00:18:38 +01:00
|
|
|
/* Open for write/append access ("a{b|x|+}") */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
case 'a' :
|
|
|
|
if (state == MODE_NONE)
|
|
|
|
{
|
|
|
|
/* Write to the end of the file */
|
|
|
|
|
2022-06-02 18:23:24 +02:00
|
|
|
oflags = O_WROK | O_CREAT | O_APPEND | O_TEXT;
|
2012-11-10 17:06:01 +01:00
|
|
|
state = MODE_A;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-01-08 00:18:38 +01:00
|
|
|
/* Open for update access ("{r|w|a|b|x}+") */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
case '+' :
|
2014-01-08 00:18:38 +01:00
|
|
|
switch (state & MODE_MASK)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
|
|
|
case MODE_R:
|
|
|
|
{
|
2014-01-08 00:18:38 +01:00
|
|
|
/* Retain any binary and exclusive mode selections */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2022-06-02 08:26:49 +02:00
|
|
|
oflags &= FLAG_KEEP;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* Open for read/write access */
|
|
|
|
|
|
|
|
oflags |= O_RDWR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MODE_W:
|
|
|
|
{
|
2014-01-08 00:18:38 +01:00
|
|
|
/* Retain any binary and exclusive mode selections */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2022-06-02 08:26:49 +02:00
|
|
|
oflags &= FLAG_KEEP;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2020-05-23 06:58:52 +02:00
|
|
|
/* Open for write read/access, truncating any existing
|
|
|
|
* file.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2022-06-02 18:23:24 +02:00
|
|
|
oflags |= O_RDWR | O_CREAT | O_TRUNC;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MODE_A:
|
|
|
|
{
|
2014-01-08 00:18:38 +01:00
|
|
|
/* Retain any binary and exclusive mode selections */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2022-06-02 08:26:49 +02:00
|
|
|
oflags &= FLAG_KEEP;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2020-05-23 06:58:52 +02:00
|
|
|
/* Read from the beginning of the file; write to the
|
|
|
|
* end,
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2022-06-02 18:23:24 +02:00
|
|
|
oflags |= O_RDWR | O_CREAT | O_APPEND;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto errout;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-08-21 15:49:05 +02:00
|
|
|
/* Attempt to access the file using mmap. */
|
|
|
|
|
|
|
|
case 'm' :
|
|
|
|
if (state != MODE_R)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-01-08 00:18:38 +01:00
|
|
|
/* Open for binary access ("{r|w|a|x|+}b") */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
case 'b' :
|
2014-01-08 00:18:38 +01:00
|
|
|
if ((state & MODE_MASK) != MODE_NONE)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
|
|
|
/* The file is opened in binary mode */
|
|
|
|
|
2022-06-02 18:23:24 +02:00
|
|
|
oflags &= ~O_TEXT;
|
2014-01-08 00:18:38 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
break;
|
2022-06-02 08:30:50 +02:00
|
|
|
|
|
|
|
/* Open for close on execute */
|
|
|
|
|
|
|
|
case 'e' :
|
|
|
|
if ((state & MODE_MASK) != MODE_NONE)
|
|
|
|
{
|
|
|
|
/* The file will be closed on execute */
|
|
|
|
|
|
|
|
oflags |= O_CLOEXEC;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
break;
|
2014-01-08 00:18:38 +01:00
|
|
|
|
|
|
|
/* Open for exclusive access ("{r|w|a|b|+}x") */
|
|
|
|
|
2022-06-02 07:59:20 +02:00
|
|
|
case 'x' :
|
2014-01-08 00:18:38 +01:00
|
|
|
if ((state & MODE_MASK) != MODE_NONE)
|
|
|
|
{
|
|
|
|
/* The file is opened in exclusive mode */
|
|
|
|
|
|
|
|
oflags |= O_EXCL;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-07-28 04:13:00 +02:00
|
|
|
/* Open for text (translated) access ("{r|w|a|x|+}t") */
|
|
|
|
|
|
|
|
case 't' :
|
|
|
|
if ((state & MODE_MASK) != MODE_NONE)
|
|
|
|
{
|
|
|
|
/* The file is opened in text mode */
|
|
|
|
|
|
|
|
oflags |= O_TEXT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/* Unrecognized or unsupported mode */
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto errout;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return oflags;
|
|
|
|
|
|
|
|
/* Both fopen and fdopen should fail with errno == EINVAL if the mode
|
|
|
|
* string is invalid.
|
|
|
|
*/
|
|
|
|
|
|
|
|
errout:
|
|
|
|
set_errno(EINVAL);
|
|
|
|
return ERROR;
|
|
|
|
}
|