2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/stdio/lib_puts.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 <stdio.h>
|
2021-11-14 20:25:15 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
2015-12-30 00:31:17 +01:00
|
|
|
#include "libc.h"
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: puts
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* puts() writes the string s and a trailing newline to stdout.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-06-02 20:57:39 +02:00
|
|
|
int puts(FAR const IPTR char *s)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2021-11-14 20:25:15 +01:00
|
|
|
#ifdef CONFIG_FILE_STREAM
|
2012-11-10 17:06:01 +01:00
|
|
|
FILE *stream = stdout;
|
|
|
|
int nwritten;
|
|
|
|
int nput = EOF;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Write the string (the next two steps must be atomic) */
|
|
|
|
|
2022-04-25 18:13:41 +02:00
|
|
|
flockfile(stream);
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/* Write the string without its trailing '\0' */
|
|
|
|
|
|
|
|
nwritten = fputs(s, stream);
|
|
|
|
if (nwritten > 0)
|
|
|
|
{
|
|
|
|
/* Followed by a newline */
|
|
|
|
|
|
|
|
char newline = '\n';
|
|
|
|
ret = lib_fwrite(&newline, 1, stream);
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
nput = nwritten + 1;
|
|
|
|
|
2017-02-08 18:28:24 +01:00
|
|
|
/* Flush the buffer after the newline is output if line buffering
|
|
|
|
* is enabled.
|
|
|
|
*/
|
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
|
|
|
ret = lib_fflush(stream, true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nput = EOF;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 18:13:41 +02:00
|
|
|
funlockfile(stdout);
|
2012-11-10 17:06:01 +01:00
|
|
|
return nput;
|
2021-11-14 20:25:15 +01: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-10 17:06:01 +01:00
|
|
|
}
|