1 //===- Errno.cpp - errno support --------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the errno wrappers.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/Errno.h"
14 #include "llvm/Config/config.h"
15 #include "llvm/Support/raw_ostream.h"
22 //===----------------------------------------------------------------------===//
23 //=== WARNING: Implementation here must contain only TRULY operating system
24 //=== independent code.
25 //===----------------------------------------------------------------------===//
31 std::string StrError() {
32 return StrError(errno);
34 #endif // HAVE_ERRNO_H
36 std::string StrError(int errnum) {
40 #if defined(HAVE_STRERROR_R) || HAVE_DECL_STRERROR_S
41 const int MaxErrStrLen = 2000;
42 char buffer[MaxErrStrLen];
46 #ifdef HAVE_STRERROR_R
47 // strerror_r is thread-safe.
48 #if defined(__GLIBC__) && defined(_GNU_SOURCE)
49 // glibc defines its own incompatible version of strerror_r
50 // which may not use the buffer supplied.
51 str = strerror_r(errnum, buffer, MaxErrStrLen - 1);
53 strerror_r(errnum, buffer, MaxErrStrLen - 1);
56 #elif HAVE_DECL_STRERROR_S // "Windows Secure API"
57 strerror_s(buffer, MaxErrStrLen - 1, errnum);
59 #elif defined(HAVE_STRERROR)
60 // Copy the thread un-safe result of strerror into
61 // the buffer as fast as possible to minimize impact
62 // of collision of strerror in multiple threads.
63 str = strerror(errnum);
65 // Strange that this system doesn't even have strerror
66 // but, oh well, just use a generic message
67 raw_string_ostream stream(str);
68 stream << "Error #" << errnum;