Skip to content

Commit

Permalink
Implement snprintf for MSVC with correct return value.
Browse files Browse the repository at this point in the history
Reviewers: zturner

Subscribers: lldb-commits

Differential Revision: http://reviews.llvm.org/D10048

llvm-svn: 238599
  • Loading branch information
chaoren committed May 29, 2015
1 parent 2229d33 commit 0ef0027
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lldb/include/lldb/Host/windows/win32.h
Expand Up @@ -52,7 +52,7 @@ typedef unsigned short mode_t;

#ifdef LLDB_DISABLE_PYTHON
typedef uint32_t pid_t;
#endif
#endif // LLDB_DISABLE_PYTHON

int usleep(uint32_t useconds);

Expand All @@ -63,6 +63,7 @@ char *dirname(char *path);

int strcasecmp(const char* s1, const char* s2);
int strncasecmp(const char* s1, const char* s2, size_t n);
int snprintf(char *buffer, size_t count, const char *format, ...);

#define STDIN_FILENO 0
#define STDOUT_FILENO 1
Expand All @@ -73,8 +74,7 @@ int strncasecmp(const char* s1, const char* s2, size_t n);
#define S_IFDIR _S_IFDIR
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)

#define snprintf _snprintf
#endif
#endif // _MSC_VER

// timespec
struct timespec
Expand Down
29 changes: 27 additions & 2 deletions lldb/source/Host/windows/Windows.cpp
Expand Up @@ -198,8 +198,33 @@ int strncasecmp(const char* s1, const char* s2, size_t n)

int usleep(uint32_t useconds)
{
Sleep(useconds / 1000);
return 0;
Sleep(useconds / 1000);
return 0;
}

int snprintf(char *buffer, size_t count, const char *format, ...)
{
int old_errno = errno;
va_list argptr;
va_start(argptr, format);
int r = vsnprintf(buffer, count, format, argptr);
int new_errno = errno;
buffer[count-1] = '\0';
if (r == -1 || r == count)
{
FILE *nul = fopen("nul", "w");
int bytes_written = vfprintf(nul, format, argptr);
fclose(nul);
if (bytes_written < count)
errno = new_errno;
else
{
errno = old_errno;
r = bytes_written;
}
}
va_end(argptr);
return r;
}

#endif // _MSC_VER

0 comments on commit 0ef0027

Please sign in to comment.