Reactos
1//
2// fputs.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Defines fputs(), which writes a string to a stream.
7//
8#include <corecrt_internal_stdio.h>
9#include <corecrt_internal_ptd_propagation.h>
10
11
12
13// Writes the given string to the given stream. Does not write the string's null
14// terminator, and does not append a '\n' to the file. Returns a nonnegative
15// value on success; EOF on failure.
16static int __cdecl _fputs_internal(char const* const string, FILE* const stream, __crt_cached_ptd_host& ptd)
17{
18 _UCRT_VALIDATE_RETURN(ptd, string != nullptr, EINVAL, EOF);
19 _UCRT_VALIDATE_RETURN(ptd, stream != nullptr, EINVAL, EOF);
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 if (bytes_written == length)
30 {
31 return 0;
32 }
33
34 return EOF;
35 });
36}
37
38extern "C" int __cdecl fputs(char const* const string, FILE* const stream)
39{
40 __crt_cached_ptd_host ptd;
41 return _fputs_internal(string, stream, ptd);
42}