xref: /reactos/sdk/lib/ucrt/stdio/puts.cpp (revision 53d808d2)
1 //
2 // puts.cpp
3 //
4 //      Copyright (c) Microsoft Corporation.  All rights reserved.
5 //
6 // Defines puts(), which writes a string to stdout.
7 //
8 #include <corecrt_internal_stdio.h>
9 #include <corecrt_internal_ptd_propagation.h>
10 
11 
12 
13 // Writes a string to stdout.  Does not write the string's null terminator, but
14 // _does_ append a newline to the output.  Return 0 on success; EOF on failure.
15 static int __cdecl _puts_internal(char const* const string, __crt_cached_ptd_host& ptd)
16 {
17     _UCRT_VALIDATE_RETURN(ptd, string != nullptr,  EINVAL, EOF);
18 
19     FILE* const stream = stdout;
20     _UCRT_VALIDATE_STREAM_ANSI_RETURN(ptd, stream, EINVAL, EOF);
21 
22     size_t const length = strlen(string);
23 
24     return __acrt_lock_stream_and_call(stream, [&]() -> int
25     {
26         __acrt_stdio_temporary_buffering_guard const buffering(stream, ptd);
27 
28         size_t const bytes_written = _fwrite_nolock_internal(string, 1, length, stream, ptd);
29 
30         // If we failed to write the entire string, or if we fail to write the
31         // newline, reset the buffering and return failure:
32         if (bytes_written != length || _fputc_nolock_internal('\n', stream, ptd) == EOF)
33         {
34             return EOF;
35         }
36 
37         return 0;
38     });
39 }
40 
41 extern "C" int __cdecl puts(char const* const string)
42 {
43     __crt_cached_ptd_host ptd;
44     return _puts_internal(string, ptd);
45 }
46