2012-11-11 00:06:01 +08:00
|
|
|
/****************************************************************************
|
2018-05-30 03:21:26 +08:00
|
|
|
* libs/libc/stdio/lib_puts.c
|
2012-11-11 00:06:01 +08:00
|
|
|
*
|
2024-09-25 20:05:00 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-03-02 22:54:21 +08: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-11 00:06:01 +08:00
|
|
|
*
|
2021-03-02 22:54:21 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-11 00:06:01 +08:00
|
|
|
*
|
2021-03-02 22:54:21 +08: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-11 00:06:01 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2021-11-15 03:25:15 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
2015-12-30 07:31:17 +08:00
|
|
|
#include "libc.h"
|
2012-11-11 00:06:01 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: puts
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* puts() writes the string s and a trailing newline to stdout.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-06-03 02:57:39 +08:00
|
|
|
int puts(FAR const IPTR char *s)
|
2012-11-11 00:06:01 +08:00
|
|
|
{
|
2021-11-15 03:25:15 +08:00
|
|
|
#ifdef CONFIG_FILE_STREAM
|
2012-11-11 00:06:01 +08:00
|
|
|
FILE *stream = stdout;
|
|
|
|
int nwritten;
|
|
|
|
int nput = EOF;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Write the string (the next two steps must be atomic) */
|
|
|
|
|
2022-04-26 00:13:41 +08:00
|
|
|
flockfile(stream);
|
2012-11-11 00:06:01 +08:00
|
|
|
|
|
|
|
/* Write the string without its trailing '\0' */
|
|
|
|
|
2023-08-30 15:40:39 +08:00
|
|
|
nwritten = fputs_unlocked(s, stream);
|
2012-11-11 00:06:01 +08:00
|
|
|
if (nwritten > 0)
|
|
|
|
{
|
|
|
|
/* Followed by a newline */
|
|
|
|
|
|
|
|
char newline = '\n';
|
2023-08-30 15:40:39 +08:00
|
|
|
ret = lib_fwrite_unlocked(&newline, 1, stream);
|
2012-11-11 00:06:01 +08:00
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
nput = nwritten + 1;
|
|
|
|
|
2017-02-09 01:28:24 +08:00
|
|
|
/* Flush the buffer after the newline is output if line buffering
|
|
|
|
* is enabled.
|
|
|
|
*/
|
2012-11-11 00:06:01 +08:00
|
|
|
|
2017-02-09 01:28:24 +08:00
|
|
|
if ((stream->fs_flags & __FS_FLAG_LBF) != 0)
|
2012-11-11 00:06:01 +08:00
|
|
|
{
|
2023-10-22 04:47:22 +08:00
|
|
|
ret = lib_fflush_unlocked(stream);
|
2017-02-09 01:28:24 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nput = EOF;
|
|
|
|
}
|
2012-11-11 00:06:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 00:13:41 +08:00
|
|
|
funlockfile(stdout);
|
2012-11-11 00:06:01 +08:00
|
|
|
return nput;
|
2021-11-15 03:25:15 +08:00
|
|
|
#else
|
|
|
|
size_t len = strlen(s);
|
|
|
|
struct iovec iov[2];
|
|
|
|
|
|
|
|
iov[0].iov_base = (FAR void *)s;
|
|
|
|
iov[0].iov_len = len;
|
|
|
|
iov[1].iov_base = "\n";
|
|
|
|
iov[1].iov_len = 1;
|
|
|
|
|
|
|
|
return writev(STDOUT_FILENO, iov, 2) == ++len ? len : EOF;
|
|
|
|
#endif
|
2012-11-11 00:06:01 +08:00
|
|
|
}
|