Errno.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //===- 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. #include "llvm/Support/Errno.h"
  14. #include "llvm/Config/config.h" // Get autoconf configuration settings
  15. #include "llvm/Support/raw_ostream.h"
  16. #include <string.h>
  17. #if HAVE_ERRNO_H
  18. #include <errno.h>
  19. #endif
  20. //===----------------------------------------------------------------------===//
  21. //=== WARNING: Implementation here must contain only TRULY operating system
  22. //=== independent code.
  23. //===----------------------------------------------------------------------===//
  24. namespace llvm {
  25. namespace sys {
  26. #if HAVE_ERRNO_H
  27. std::string StrError() {
  28. return StrError(errno);
  29. }
  30. #endif // HAVE_ERRNO_H
  31. std::string StrError(int errnum) {
  32. std::string str;
  33. if (errnum == 0)
  34. return str;
  35. #if defined(HAVE_STRERROR_R) || HAVE_DECL_STRERROR_S
  36. const int MaxErrStrLen = 2000;
  37. char buffer[MaxErrStrLen];
  38. buffer[0] = '\0';
  39. #endif
  40. #ifdef HAVE_STRERROR_R
  41. // strerror_r is thread-safe.
  42. #if defined(__GLIBC__) && defined(_GNU_SOURCE)
  43. // glibc defines its own incompatible version of strerror_r
  44. // which may not use the buffer supplied.
  45. str = strerror_r(errnum, buffer, MaxErrStrLen - 1);
  46. #else
  47. strerror_r(errnum, buffer, MaxErrStrLen - 1);
  48. str = buffer;
  49. #endif
  50. #elif HAVE_DECL_STRERROR_S // "Windows Secure API"
  51. strerror_s(buffer, MaxErrStrLen - 1, errnum);
  52. str = buffer;
  53. #elif defined(HAVE_STRERROR)
  54. // Copy the thread un-safe result of strerror into
  55. // the buffer as fast as possible to minimize impact
  56. // of collision of strerror in multiple threads.
  57. str = strerror(errnum);
  58. #else
  59. // Strange that this system doesn't even have strerror
  60. // but, oh well, just use a generic message
  61. raw_string_ostream stream(str);
  62. stream << "Error #" << errnum;
  63. stream.flush();
  64. #endif
  65. return str;
  66. }
  67. } // namespace sys
  68. } // namespace llvm