Errno.cpp 2.1 KB

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