xref: /llvm-project/libc/src/stdio/vsnprintf.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
116d5c242SMichael Jones //===-- Implementation of vsnprintf -----------------------------*- C++ -*-===//
216d5c242SMichael Jones //
316d5c242SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
416d5c242SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
516d5c242SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
616d5c242SMichael Jones //
716d5c242SMichael Jones //===----------------------------------------------------------------------===//
816d5c242SMichael Jones 
916d5c242SMichael Jones #include "src/stdio/vsnprintf.h"
1016d5c242SMichael Jones 
1116d5c242SMichael Jones #include "src/__support/arg_list.h"
12*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
1316d5c242SMichael Jones #include "src/stdio/printf_core/printf_main.h"
1416d5c242SMichael Jones #include "src/stdio/printf_core/writer.h"
1516d5c242SMichael Jones 
1616d5c242SMichael Jones #include <stdarg.h>
1716d5c242SMichael Jones #include <stddef.h>
1816d5c242SMichael Jones 
19*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
2016d5c242SMichael Jones 
2116d5c242SMichael Jones LLVM_LIBC_FUNCTION(int, vsnprintf,
2216d5c242SMichael Jones                    (char *__restrict buffer, size_t buffsz,
2316d5c242SMichael Jones                     const char *__restrict format, va_list vlist)) {
2416d5c242SMichael Jones   internal::ArgList args(vlist); // This holder class allows for easier copying
2516d5c242SMichael Jones                                  // and pointer semantics, as well as handling
2616d5c242SMichael Jones                                  // destruction automatically.
2716d5c242SMichael Jones   printf_core::WriteBuffer wb(buffer, (buffsz > 0 ? buffsz - 1 : 0));
2816d5c242SMichael Jones   printf_core::Writer writer(&wb);
2916d5c242SMichael Jones 
3016d5c242SMichael Jones   int ret_val = printf_core::printf_main(&writer, format, args);
3116d5c242SMichael Jones   if (buffsz > 0) // if the buffsz is 0 the buffer may be a null pointer.
3216d5c242SMichael Jones     wb.buff[wb.buff_cur] = '\0';
3316d5c242SMichael Jones   return ret_val;
3416d5c242SMichael Jones }
3516d5c242SMichael Jones 
36*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
37