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/Support/Errno.h"
15 #include "llvm/Config/config.h" // Get autoconf configuration settings
16 #include "llvm/Support/raw_ostream.h"
23 //===----------------------------------------------------------------------===//
24 //=== WARNING: Implementation here must contain only TRULY operating system
25 //=== independent code.
26 //===----------------------------------------------------------------------===//
32 std::string StrError() {
33 return StrError(errno);
35 #endif // HAVE_ERRNO_H
37 std::string StrError(int errnum) {
41 #if defined(HAVE_STRERROR_R) || HAVE_DECL_STRERROR_S
42 const int MaxErrStrLen = 2000;
43 char buffer[MaxErrStrLen];
47 #ifdef HAVE_STRERROR_R
48 // strerror_r is thread-safe.
49 #if defined(__GLIBC__) && defined(_GNU_SOURCE)
50 // glibc defines its own incompatible version of strerror_r
51 // which may not use the buffer supplied.
52 str = strerror_r(errnum, buffer, MaxErrStrLen - 1);
54 strerror_r(errnum, buffer, MaxErrStrLen - 1);
57 #elif HAVE_DECL_STRERROR_S // "Windows Secure API"
58 strerror_s(buffer, MaxErrStrLen - 1, errnum);
60 #elif defined(HAVE_STRERROR)
61 // Copy the thread un-safe result of strerror into
62 // the buffer as fast as possible to minimize impact
63 // of collision of strerror in multiple threads.
64 str = strerror(errnum);
66 // Strange that this system doesn't even have strerror
67 // but, oh well, just use a generic message
68 raw_string_ostream stream(str);
69 stream << "Error #" << errnum;