2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/stdio/lib_fputs.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 <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "libc.h"
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: fputs
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* fputs() writes the string s to stream, without its trailing '\0'.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_ARCH_ROMGETC)
|
2023-08-30 09:40:39 +02:00
|
|
|
int fputs_unlocked(FAR const IPTR char *s, FAR FILE *stream)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
|
|
|
int nput;
|
|
|
|
int ret;
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
/* Write the string. Loop until the null terminator is encountered */
|
|
|
|
|
|
|
|
for (nput = 0, ch = up_romgetc(s); ch; nput++, s++, ch = up_romgetc(s))
|
|
|
|
{
|
|
|
|
/* Write the next character to the stream buffer */
|
|
|
|
|
2023-08-30 09:40:39 +02:00
|
|
|
ret = lib_fwrite_unlocked(&ch, 1, stream);
|
2012-11-10 17:06:01 +01:00
|
|
|
if (ret <= 0)
|
|
|
|
{
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush the buffer if a newline was written to the buffer */
|
|
|
|
|
2017-02-08 18:28:24 +01:00
|
|
|
if (ch == '\n' && (stream->fs_flags & __FS_FLAG_LBF) != 0)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2023-10-21 22:47:22 +02:00
|
|
|
ret = lib_fflush_unlocked(stream);
|
2012-11-10 17:06:01 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nput;
|
|
|
|
}
|
|
|
|
|
2017-02-08 18:28:24 +01:00
|
|
|
#else
|
2023-08-30 09:40:39 +02:00
|
|
|
int fputs_unlocked(FAR const IPTR char *s, FAR FILE *stream)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
|
|
|
int nput;
|
|
|
|
|
2017-02-08 18:28:24 +01:00
|
|
|
/* If line buffering is enabled, then we will have to output one character
|
|
|
|
* at a time, checking for a newline character each time.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-08 18:28:24 +01:00
|
|
|
if ((stream->fs_flags & __FS_FLAG_LBF) != 0)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2017-02-08 18:28:24 +01:00
|
|
|
int ret;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-08 18:28:24 +01:00
|
|
|
/* Write the string. Loop until the null terminator is encountered */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-08 18:28:24 +01:00
|
|
|
for (nput = 0; *s; nput++, s++)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2017-02-08 18:28:24 +01:00
|
|
|
/* Write the next character to the stream buffer */
|
|
|
|
|
2023-08-30 09:40:39 +02:00
|
|
|
ret = lib_fwrite_unlocked(s, 1, stream);
|
2017-02-08 18:28:24 +01:00
|
|
|
if (ret <= 0)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
|
2017-02-08 18:28:24 +01:00
|
|
|
/* Flush the buffer if a newline was written to the buffer */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-08 18:28:24 +01:00
|
|
|
if (*s == '\n')
|
|
|
|
{
|
2023-10-21 22:47:22 +02:00
|
|
|
ret = lib_fflush_unlocked(stream);
|
2017-02-08 18:28:24 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2020-06-02 20:57:39 +02:00
|
|
|
/* We can write the whole string in one operation without line buffering */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-08 18:28:24 +01:00
|
|
|
else
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2017-02-08 18:28:24 +01:00
|
|
|
int ntowrite;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-08 18:28:24 +01:00
|
|
|
/* Get the length of the string. */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-08 18:28:24 +01:00
|
|
|
ntowrite = strlen(s);
|
|
|
|
if (ntowrite == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-02-08 18:28:24 +01:00
|
|
|
/* Write the string */
|
|
|
|
|
2023-08-30 09:40:39 +02:00
|
|
|
nput = lib_fwrite_unlocked(s, ntowrite, stream);
|
2017-02-08 18:28:24 +01:00
|
|
|
if (nput < 0)
|
|
|
|
{
|
|
|
|
return EOF;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
2017-02-08 18:28:24 +01:00
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
return nput;
|
|
|
|
}
|
|
|
|
#endif
|
2023-08-30 09:40:39 +02:00
|
|
|
|
|
|
|
int fputs(FAR const IPTR char *s, FAR FILE *stream)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
flockfile(stream);
|
|
|
|
ret = fputs_unlocked(s, stream);
|
|
|
|
funlockfile(stream);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|