1 //===- Errno.cpp - errno support --------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the errno wrappers.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/System/Errno.h"
15 #include "llvm/Config/config.h" // Get autoconf configuration settings
20 //===----------------------------------------------------------------------===//
21 //=== WARNING: Implementation here must contain only TRULY operating system
22 //=== independent code.
23 //===----------------------------------------------------------------------===//
30 std::string StrError() {
31 return StrError(errno);
33 #endif // HAVE_ERRNO_H
35 std::string StrError(int errnum) {
36 const int MaxErrStrLen = 2000;
37 char buffer[MaxErrStrLen];
40 #ifdef HAVE_STRERROR_R
41 // strerror_r is thread-safe.
43 # if defined(__GLIBC__) && defined(_GNU_SOURCE)
44 // glibc defines its own incompatible version of strerror_r
45 // which may not use the buffer supplied.
46 str = strerror_r(errnum,buffer,MaxErrStrLen-1);
48 strerror_r(errnum,buffer,MaxErrStrLen-1);
50 #elif HAVE_STRERROR_S // Windows.
52 strerror_s(buffer, errnum);
54 // Copy the thread un-safe result of strerror into
55 // the buffer as fast as possible to minimize impact
56 // of collision of strerror in multiple threads.
58 strncpy(buffer,strerror(errnum),MaxErrStrLen-1);
59 buffer[MaxErrStrLen-1] = '\0';
61 // Strange that this system doesn't even have strerror
62 // but, oh well, just use a generic message
63 sprintf(buffer, "Error #%d", errnum);
71 #endif // HAVE_STRING_H