2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2021-12-13 14:28:23 +01:00
|
|
|
* libs/libc/stream/lib_stdoutstream.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
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2023-03-01 14:03:07 +01:00
|
|
|
#include <stdio.h>
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "libc.h"
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2021-11-24 06:09:31 +01:00
|
|
|
* Name: stdoutstream_putc
|
2012-11-10 17:06:01 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2023-09-10 21:32:34 +02:00
|
|
|
static void stdoutstream_putc(FAR struct lib_outstream_s *self, int ch)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2023-09-10 21:32:34 +02:00
|
|
|
FAR struct lib_stdoutstream_s *stream =
|
|
|
|
(FAR struct lib_stdoutstream_s *)self;
|
2021-11-24 06:09:31 +01:00
|
|
|
int result;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2023-09-10 21:32:34 +02:00
|
|
|
DEBUGASSERT(self && stream->handle);
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2021-11-24 06:09:31 +01:00
|
|
|
/* Loop until the character is successfully transferred or an irrecoverable
|
|
|
|
* error occurs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2023-09-10 21:32:34 +02:00
|
|
|
result = fputc(ch, stream->handle);
|
2021-11-24 06:09:31 +01:00
|
|
|
if (result != EOF)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2023-09-10 21:32:34 +02:00
|
|
|
self->nput++;
|
2021-11-24 06:09:31 +01:00
|
|
|
return;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
2021-11-24 06:09:31 +01:00
|
|
|
/* EINTR (meaning that fputc was interrupted by a signal) is the only
|
|
|
|
* recoverable error.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
2021-11-24 06:09:31 +01:00
|
|
|
while (get_errno() == EINTR);
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
2023-03-01 14:03:07 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: stdoutstream_puts
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-09-10 21:32:34 +02:00
|
|
|
static int stdoutstream_puts(FAR struct lib_outstream_s *self,
|
2023-03-01 14:03:07 +01:00
|
|
|
FAR const void *buffer, int len)
|
|
|
|
{
|
2023-09-10 21:32:34 +02:00
|
|
|
FAR struct lib_stdoutstream_s *stream =
|
|
|
|
(FAR struct lib_stdoutstream_s *)self;
|
2023-03-01 14:03:07 +01:00
|
|
|
int result;
|
|
|
|
|
2023-09-10 21:32:34 +02:00
|
|
|
DEBUGASSERT(self && stream->handle);
|
2023-03-01 14:03:07 +01:00
|
|
|
|
|
|
|
/* Loop until the buffer is successfully transferred or an irrecoverable
|
|
|
|
* error occurs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2023-09-10 21:32:34 +02:00
|
|
|
result = fwrite(buffer, len, 1, stream->handle);
|
2023-03-01 14:03:07 +01:00
|
|
|
if (result >= 0)
|
|
|
|
{
|
2023-09-10 21:32:34 +02:00
|
|
|
self->nput += result;
|
2023-03-01 14:03:07 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = _NX_GETERRVAL(result);
|
|
|
|
|
|
|
|
/* EINTR (meaning that fputc was interrupted by a signal) is the only
|
|
|
|
* recoverable error.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
while (result == -EINTR);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: stdoutstream_flush
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
|
2023-09-10 21:32:34 +02:00
|
|
|
static int stdoutstream_flush(FAR struct lib_outstream_s *self)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2023-09-10 21:32:34 +02:00
|
|
|
FAR struct lib_stdoutstream_s *stream =
|
|
|
|
(FAR struct lib_stdoutstream_s *)self;
|
2017-02-09 16:24:44 +01:00
|
|
|
|
2023-09-10 21:32:34 +02:00
|
|
|
DEBUGASSERT(stream != NULL && stream->handle != NULL);
|
2023-10-21 22:47:22 +02:00
|
|
|
return lib_fflush(stream->handle);
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lib_stdoutstream
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initializes a stream for use with a FILE instance.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2014-06-15 00:19:42 +02:00
|
|
|
* outstream - User allocated, uninitialized instance of struct
|
|
|
|
* lib_stdoutstream_s to be initialized.
|
2023-09-10 21:32:34 +02:00
|
|
|
* handle - User provided FILE instance (must have been opened for
|
2014-06-15 00:19:42 +02:00
|
|
|
* write access).
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None (User allocated instance initialized).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-09-10 21:32:34 +02:00
|
|
|
void lib_stdoutstream(FAR struct lib_stdoutstream_s *stream,
|
|
|
|
FAR FILE *handle)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2022-12-04 10:35:13 +01:00
|
|
|
/* Select the putc operation */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2023-09-10 21:32:34 +02:00
|
|
|
stream->common.putc = stdoutstream_putc;
|
|
|
|
stream->common.puts = stdoutstream_puts;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* Select the correct flush operation. This flush is only called when
|
2023-09-10 21:32:34 +02:00
|
|
|
* a newline is encountered in the output file stream. However, we do not
|
|
|
|
* want to support this line buffering behavior if the file was
|
2012-11-10 17:06:01 +01:00
|
|
|
* opened in binary mode. In binary mode, the newline has no special
|
|
|
|
* meaning.
|
|
|
|
*/
|
|
|
|
|
2017-02-09 16:24:44 +01:00
|
|
|
#ifndef CONFIG_STDIO_DISABLE_BUFFERING
|
2023-09-10 21:32:34 +02:00
|
|
|
if (handle->fs_bufstart != NULL && (handle->fs_oflags & O_TEXT) != 0)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2023-09-10 21:32:34 +02:00
|
|
|
stream->common.flush = stdoutstream_flush;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2023-09-10 21:32:34 +02:00
|
|
|
stream->common.flush = lib_noflush;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
2023-09-10 21:32:34 +02:00
|
|
|
/* Set the number of bytes put to zero and remember the handle */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2023-09-10 21:32:34 +02:00
|
|
|
stream->common.nput = 0;
|
|
|
|
stream->handle = handle;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|