//===-- VcSnprintf.cpp - Implement snprintf for VC++ ----------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements a snprintf for VC++ .
//
//===----------------------------------------------------------------------===//

#ifdef _MSC_VER
#include <cstdarg>
#include <cstdio>

// The Visual C++ runtime has no snprintf, only _snprintf
int snprintf(char *buffer, int buff_size, const char *format, ...) {
  va_list args;
  va_start(args, format);
  int res = vsnprintf(buffer, buff_size, format, args);
  va_end(args);
  // pre vc2005sp1 runtime versions return negative values in case of error
  if (res < 0)
    return res;
  if (res == buff_size) {
    // '\0' was not written, do it
    --res;
    buffer[res] = '\0';
  }
  return res;
}
#endif
