2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/stdio/lib_libfwrite.c
|
2012-11-10 17:06:01 +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
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-10 17:06:01 +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.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-03-31 17:39:10 +02:00
|
|
|
#include <nuttx/config.h>
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2017-10-11 18:18:30 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "libc.h"
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lib_fwrite
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-08-30 09:40:39 +02:00
|
|
|
ssize_t lib_fwrite_unlocked(FAR const void *ptr, size_t count,
|
|
|
|
FAR FILE *stream)
|
2017-02-09 16:24:44 +01:00
|
|
|
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2023-08-27 21:33:44 +02:00
|
|
|
FAR const char *start = ptr;
|
|
|
|
FAR const char *src = ptr;
|
2012-11-10 17:06:01 +01:00
|
|
|
ssize_t ret = ERROR;
|
2022-06-02 17:53:14 +02:00
|
|
|
size_t gulp_size;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* Make sure that writing to this stream is allowed */
|
|
|
|
|
2015-09-11 02:59:43 +02:00
|
|
|
if (stream == NULL)
|
|
|
|
{
|
|
|
|
set_errno(EBADF);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
/* Check if write access is permitted */
|
|
|
|
|
2015-09-11 02:59:43 +02:00
|
|
|
if ((stream->fs_oflags & O_WROK) == 0)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
|
|
|
set_errno(EBADF);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
/* If there is no I/O buffer, then output data immediately */
|
|
|
|
|
|
|
|
if (stream->fs_bufstart == NULL)
|
2021-02-15 09:36:03 +01:00
|
|
|
{
|
2023-08-27 21:33:44 +02:00
|
|
|
if (stream->fs_iofunc.write != NULL)
|
|
|
|
{
|
|
|
|
ret = stream->fs_iofunc.write(stream->fs_cookie, ptr, count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = _NX_WRITE((int)(intptr_t)stream->fs_cookie, ptr, count);
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:36:03 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
_NX_SETERRNO(ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
2017-10-11 18:18:30 +02:00
|
|
|
|
2021-02-15 09:36:03 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
2017-02-09 16:24:44 +01:00
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/* If the buffer is currently being used for read access, then
|
|
|
|
* discard all of the read-ahead data. We do not support concurrent
|
|
|
|
* buffered read/write access.
|
|
|
|
*/
|
|
|
|
|
2023-08-24 05:43:57 +02:00
|
|
|
if (lib_rdflush_unlocked(stream) < 0)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2023-08-30 09:40:39 +02:00
|
|
|
goto errout;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
2022-06-02 17:53:14 +02:00
|
|
|
/* Determine the number of bytes left in the buffer */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2022-06-02 17:53:14 +02:00
|
|
|
gulp_size = stream->fs_bufend - stream->fs_bufpos;
|
|
|
|
if (gulp_size != CONFIG_STDIO_BUFFER_SIZE || count < gulp_size)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
|
|
|
if (gulp_size > count)
|
|
|
|
{
|
|
|
|
/* Yes, clip the gulp to the size of the user data */
|
|
|
|
|
|
|
|
gulp_size = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Adjust the number of bytes remaining to be transferred
|
|
|
|
* on the next pass through the loop (might be zero).
|
|
|
|
*/
|
|
|
|
|
|
|
|
count -= gulp_size;
|
|
|
|
|
|
|
|
/* Transfer the data into the buffer */
|
|
|
|
|
2022-06-02 17:53:14 +02:00
|
|
|
memcpy(stream->fs_bufpos, src, gulp_size);
|
|
|
|
stream->fs_bufpos += gulp_size;
|
|
|
|
src += gulp_size;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* Is the buffer full? */
|
|
|
|
|
2022-06-02 17:53:14 +02:00
|
|
|
if (stream->fs_bufpos >= stream->fs_bufend)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
|
|
|
/* Flush the buffered data to the IO stream */
|
|
|
|
|
2023-10-21 22:51:59 +02:00
|
|
|
int bytes_buffered = lib_fflush_unlocked(stream, true);
|
2012-11-10 17:06:01 +01:00
|
|
|
if (bytes_buffered < 0)
|
|
|
|
{
|
2023-08-30 09:40:39 +02:00
|
|
|
goto errout;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 17:53:14 +02:00
|
|
|
if (count >= CONFIG_STDIO_BUFFER_SIZE)
|
|
|
|
{
|
2023-08-27 21:33:44 +02:00
|
|
|
if (stream->fs_iofunc.write != NULL)
|
|
|
|
{
|
|
|
|
ret = stream->fs_iofunc.write(stream->fs_cookie, src, count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = _NX_WRITE((int)(intptr_t)stream->fs_cookie, src, count);
|
|
|
|
}
|
|
|
|
|
2022-06-02 17:53:14 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
_NX_SETERRNO(ret);
|
|
|
|
ret = ERROR;
|
2023-08-30 09:40:39 +02:00
|
|
|
goto errout;
|
2022-06-02 17:53:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
src += ret;
|
|
|
|
}
|
|
|
|
else if (count > 0)
|
|
|
|
{
|
|
|
|
memcpy(stream->fs_bufpos, src, count);
|
|
|
|
stream->fs_bufpos += count;
|
|
|
|
src += count;
|
|
|
|
}
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/* Return the number of bytes written */
|
|
|
|
|
2017-02-09 16:53:50 +01:00
|
|
|
ret = (uintptr_t)src - (uintptr_t)start;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
errout:
|
2014-03-31 17:39:10 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
stream->fs_flags |= __FS_FLAG_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
2023-08-27 21:33:44 +02:00
|
|
|
ssize_t ret;
|
|
|
|
if (stream->fs_iofunc.write != NULL)
|
|
|
|
{
|
|
|
|
ret = stream->fs_iofunc.write(stream->fs_cookie, ptr, count);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = _NX_WRITE((int)(intptr_t)stream->fs_cookie, ptr, count);
|
|
|
|
}
|
|
|
|
|
2014-03-31 17:39:10 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
stream->fs_flags |= __FS_FLAG_ERROR;
|
2017-10-11 18:18:30 +02:00
|
|
|
_NX_SETERRNO(ret);
|
2014-03-31 17:39:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
2017-02-09 16:24:44 +01:00
|
|
|
#endif /* CONFIG_STDIO_DISABLE_BUFFERING */
|
2023-08-30 09:40:39 +02:00
|
|
|
|
|
|
|
ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream)
|
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
flockfile(stream);
|
|
|
|
ret = lib_fwrite_unlocked(ptr, count, stream);
|
|
|
|
funlockfile(stream);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|