Errno.cpp revision 263508
1248590Smm//===- Errno.cpp - errno support --------------------------------*- C++ -*-===//
2248590Smm//
3248590Smm//                     The LLVM Compiler Infrastructure
4248590Smm//
5248590Smm// This file is distributed under the University of Illinois Open Source
6248590Smm// License. See LICENSE.TXT for details.
7248590Smm//
8248590Smm//===----------------------------------------------------------------------===//
9248590Smm//
10248590Smm// This file implements the errno wrappers.
11248590Smm//
12248590Smm//===----------------------------------------------------------------------===//
13248590Smm
14248590Smm#include "llvm/Support/Errno.h"
15248590Smm#include "llvm/Config/config.h"     // Get autoconf configuration settings
16248590Smm#include "llvm/Support/raw_ostream.h"
17248590Smm#include <string.h>
18248590Smm
19248590Smm#if HAVE_ERRNO_H
20248590Smm#include <errno.h>
21248590Smm#endif
22248590Smm
23248590Smm//===----------------------------------------------------------------------===//
24248590Smm//=== WARNING: Implementation here must contain only TRULY operating system
25248590Smm//===          independent code.
26248590Smm//===----------------------------------------------------------------------===//
27248590Smm
28248590Smmnamespace llvm {
29248590Smmnamespace sys {
30248590Smm
31248590Smm#if HAVE_ERRNO_H
32248590Smmstd::string StrError() {
33248590Smm  return StrError(errno);
34248590Smm}
35248590Smm#endif  // HAVE_ERRNO_H
36248590Smm
37248590Smmstd::string StrError(int errnum) {
38248590Smm  const int MaxErrStrLen = 2000;
39248590Smm  char buffer[MaxErrStrLen];
40248590Smm  buffer[0] = '\0';
41248590Smm  std::string str;
42248590Smm  if (errnum == 0)
43248590Smm    return str;
44248590Smm
45248590Smm#ifdef HAVE_STRERROR_R
46248590Smm  // strerror_r is thread-safe.
47248590Smm#if defined(__GLIBC__) && defined(_GNU_SOURCE)
48248590Smm  // glibc defines its own incompatible version of strerror_r
49248590Smm  // which may not use the buffer supplied.
50248590Smm  str = strerror_r(errnum, buffer, MaxErrStrLen - 1);
51248590Smm#else
52248590Smm  strerror_r(errnum, buffer, MaxErrStrLen - 1);
53248590Smm  str = buffer;
54248590Smm#endif
55248590Smm#elif HAVE_DECL_STRERROR_S // "Windows Secure API"
56248590Smm  strerror_s(buffer, MaxErrStrLen - 1, errnum);
57248590Smm  str = buffer;
58248590Smm#elif defined(HAVE_STRERROR)
59248590Smm  // Copy the thread un-safe result of strerror into
60248590Smm  // the buffer as fast as possible to minimize impact
61248590Smm  // of collision of strerror in multiple threads.
62248590Smm  str = strerror(errnum);
63248590Smm#else
64248590Smm  // Strange that this system doesn't even have strerror
65248590Smm  // but, oh well, just use a generic message
66248590Smm  raw_string_ostream stream(str);
67248590Smm  stream << "Error #" << errnum;
68248590Smm  stream.flush();
69248590Smm#endif
70248590Smm  return str;
71248590Smm}
72248590Smm
73248590Smm}  // namespace sys
74248590Smm}  // namespace llvm
75248590Smm