19f1d905fSMichael Jones //===-- Implementation of snprintf ------------------------------*- C++ -*-===// 29f1d905fSMichael Jones // 39f1d905fSMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 49f1d905fSMichael Jones // See https://llvm.org/LICENSE.txt for license information. 59f1d905fSMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 69f1d905fSMichael Jones // 79f1d905fSMichael Jones //===----------------------------------------------------------------------===// 89f1d905fSMichael Jones 99f1d905fSMichael Jones #include "src/stdio/snprintf.h" 109f1d905fSMichael Jones 119f1d905fSMichael Jones #include "src/__support/arg_list.h" 12*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h" 139f1d905fSMichael Jones #include "src/stdio/printf_core/printf_main.h" 149f1d905fSMichael Jones #include "src/stdio/printf_core/writer.h" 159f1d905fSMichael Jones 169f1d905fSMichael Jones #include <stdarg.h> 179f1d905fSMichael Jones #include <stddef.h> 189f1d905fSMichael Jones 19*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL { 209f1d905fSMichael Jones 219f1d905fSMichael Jones LLVM_LIBC_FUNCTION(int, snprintf, 229f1d905fSMichael Jones (char *__restrict buffer, size_t buffsz, 239f1d905fSMichael Jones const char *__restrict format, ...)) { 249f1d905fSMichael Jones va_list vlist; 259f1d905fSMichael Jones va_start(vlist, format); 269f1d905fSMichael Jones internal::ArgList args(vlist); // This holder class allows for easier copying 279f1d905fSMichael Jones // and pointer semantics, as well as handling 289f1d905fSMichael Jones // destruction automatically. 299f1d905fSMichael Jones va_end(vlist); 30b9f6c208SMichael Jones printf_core::WriteBuffer wb(buffer, (buffsz > 0 ? buffsz - 1 : 0)); 31b9f6c208SMichael Jones printf_core::Writer writer(&wb); 329f1d905fSMichael Jones 339f1d905fSMichael Jones int ret_val = printf_core::printf_main(&writer, format, args); 349f1d905fSMichael Jones if (buffsz > 0) // if the buffsz is 0 the buffer may be a null pointer. 35b9f6c208SMichael Jones wb.buff[wb.buff_cur] = '\0'; 369f1d905fSMichael Jones return ret_val; 379f1d905fSMichael Jones } 389f1d905fSMichael Jones 39*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL 40