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"
21 //===----------------------------------------------------------------------===//
22 //=== WARNING: Implementation here must contain only TRULY operating system
23 //=== independent code.
24 //===----------------------------------------------------------------------===//
30 std::string
StrError() {
31 return StrError(errno
);
33 #endif // HAVE_ERRNO_H
35 std::string
StrError(int errnum
) {
39 #if defined(HAVE_STRERROR_R) || HAVE_DECL_STRERROR_S
40 const int MaxErrStrLen
= 2000;
41 char buffer
[MaxErrStrLen
];
45 #ifdef HAVE_STRERROR_R
46 // strerror_r is thread-safe.
47 #if defined(__GLIBC__) && defined(_GNU_SOURCE)
48 // glibc defines its own incompatible version of strerror_r
49 // which may not use the buffer supplied.
50 str
= strerror_r(errnum
, buffer
, MaxErrStrLen
- 1);
52 strerror_r(errnum
, buffer
, MaxErrStrLen
- 1);
55 #elif HAVE_DECL_STRERROR_S // "Windows Secure API"
56 strerror_s(buffer
, MaxErrStrLen
- 1, errnum
);
59 // Copy the thread un-safe result of strerror into
60 // the buffer as fast as possible to minimize impact
61 // of collision of strerror in multiple threads.
62 str
= strerror(errnum
);