blob: 730220f47d924b354cc967a7354e51a8be357bef [file] [log] [blame]
Jeffrey Yasskined1c0ff2009-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. Spencer1f6efa32010-11-29 18:16:10 +000014#include "llvm/Support/Errno.h"
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +000015#include "llvm/Config/config.h" // Get autoconf configuration settings
Dmitri Gribenko04cb5642012-09-28 14:15:28 +000016#include "llvm/Support/raw_ostream.h"
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +000017
18#if HAVE_STRING_H
19#include <string.h>
20
Jeffrey Yasskin4733e742009-07-06 16:50:27 +000021#if HAVE_ERRNO_H
22#include <errno.h>
23#endif
24
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +000025//===----------------------------------------------------------------------===//
26//=== WARNING: Implementation here must contain only TRULY operating system
27//=== independent code.
28//===----------------------------------------------------------------------===//
29
30namespace llvm {
31namespace sys {
32
33#if HAVE_ERRNO_H
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +000034std::string StrError() {
35 return StrError(errno);
36}
37#endif // HAVE_ERRNO_H
38
39std::string StrError(int errnum) {
40 const int MaxErrStrLen = 2000;
41 char buffer[MaxErrStrLen];
42 buffer[0] = '\0';
Dmitri Gribenko04cb5642012-09-28 14:15:28 +000043 std::string str;
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +000044#ifdef HAVE_STRERROR_R
45 // strerror_r is thread-safe.
46 if (errnum)
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);
51# else
52 strerror_r(errnum,buffer,MaxErrStrLen-1);
Dmitri Gribenko04cb5642012-09-28 14:15:28 +000053 str = buffer;
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +000054# endif
NAKAMURA Takumie29b0ac2011-02-09 04:18:48 +000055#elif HAVE_DECL_STRERROR_S // "Windows Secure API"
Michael J. Spencer08e712b2012-11-01 00:34:09 +000056 if (errnum) {
NAKAMURA Takumi33f21392012-06-24 03:51:04 +000057 strerror_s(buffer, MaxErrStrLen - 1, errnum);
Michael J. Spencer08e712b2012-11-01 00:34:09 +000058 str = buffer;
59 }
Duncan Sands9170d592009-07-02 12:09:50 +000060#elif defined(HAVE_STRERROR)
Jeffrey Yasskined1c0ff2009-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.
64 if (errnum)
Dmitri Gribenko04cb5642012-09-28 14:15:28 +000065 str = strerror(errnum);
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +000066#else
67 // Strange that this system doesn't even have strerror
68 // but, oh well, just use a generic message
Dmitri Gribenko04cb5642012-09-28 14:15:28 +000069 raw_string_ostream stream(str);
70 stream << "Error #" << errnum;
71 stream.flush();
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +000072#endif
73 return str;
74}
75
76} // namespace sys
77} // namespace llvm
78
79#endif // HAVE_STRING_H