blob: 3ba2a1277d05f2c16b4f4d6b4fc77f794b4638c9 [file] [log] [blame]
Jeffrey Yasskin1a933302009-07-01 18:11:20 +00001//===- Errno.cpp - errno support --------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the errno wrappers.
11//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencer447762d2010-11-29 18:16:10 +000014#include "llvm/Support/Errno.h"
Jeffrey Yasskin1a933302009-07-01 18:11:20 +000015#include "llvm/Config/config.h" // Get autoconf configuration settings
Dmitri Gribenko91c06da2012-09-28 14:15:28 +000016#include "llvm/Support/raw_ostream.h"
Jeffrey Yasskin1a933302009-07-01 18:11:20 +000017#include <string.h>
18
Jeffrey Yasskin5a2e5212009-07-06 16:50:27 +000019#if HAVE_ERRNO_H
20#include <errno.h>
21#endif
22
Jeffrey Yasskin1a933302009-07-01 18:11:20 +000023//===----------------------------------------------------------------------===//
24//=== WARNING: Implementation here must contain only TRULY operating system
25//=== independent code.
26//===----------------------------------------------------------------------===//
27
28namespace llvm {
29namespace sys {
30
31#if HAVE_ERRNO_H
Jeffrey Yasskin1a933302009-07-01 18:11:20 +000032std::string StrError() {
33 return StrError(errno);
34}
35#endif // HAVE_ERRNO_H
36
37std::string StrError(int errnum) {
Dmitri Gribenko91c06da2012-09-28 14:15:28 +000038 std::string str;
Chandler Carruth14aae042013-09-02 05:55:10 +000039 if (errnum == 0)
40 return str;
Yaron Kerend9089412014-12-04 21:36:38 +000041#if defined(HAVE_STRERROR_R) || HAVE_DECL_STRERROR_S
42 const int MaxErrStrLen = 2000;
43 char buffer[MaxErrStrLen];
44 buffer[0] = '\0';
45#endif
Chandler Carruth14aae042013-09-02 05:55:10 +000046
Jeffrey Yasskin1a933302009-07-01 18:11:20 +000047#ifdef HAVE_STRERROR_R
48 // strerror_r is thread-safe.
Chandler Carruth14aae042013-09-02 05:55:10 +000049#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);
53#else
54 strerror_r(errnum, buffer, MaxErrStrLen - 1);
55 str = buffer;
56#endif
NAKAMURA Takumi189111802011-02-09 04:18:48 +000057#elif HAVE_DECL_STRERROR_S // "Windows Secure API"
Chandler Carruth14aae042013-09-02 05:55:10 +000058 strerror_s(buffer, MaxErrStrLen - 1, errnum);
59 str = buffer;
Duncan Sandsf9cf4ff2009-07-02 12:09:50 +000060#elif defined(HAVE_STRERROR)
Jeffrey Yasskin1a933302009-07-01 18:11:20 +000061 // 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.
Chandler Carruth14aae042013-09-02 05:55:10 +000064 str = strerror(errnum);
Jeffrey Yasskin1a933302009-07-01 18:11:20 +000065#else
66 // Strange that this system doesn't even have strerror
67 // but, oh well, just use a generic message
Dmitri Gribenko91c06da2012-09-28 14:15:28 +000068 raw_string_ostream stream(str);
69 stream << "Error #" << errnum;
70 stream.flush();
Jeffrey Yasskin1a933302009-07-01 18:11:20 +000071#endif
72 return str;
73}
74
75} // namespace sys
76} // namespace llvm