Path: blob/main/contrib/llvm-project/llvm/lib/Support/Errno.cpp
35233 views
//===- Errno.cpp - errno support --------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the errno wrappers.9//10//===----------------------------------------------------------------------===//1112#include "llvm/Support/Errno.h"13#include "llvm/Config/config.h"14#include <cstring>1516#if HAVE_ERRNO_H17#include <errno.h>18#endif1920//===----------------------------------------------------------------------===//21//=== WARNING: Implementation here must contain only TRULY operating system22//=== independent code.23//===----------------------------------------------------------------------===//2425namespace llvm {26namespace sys {2728#if HAVE_ERRNO_H29std::string StrError() {30return StrError(errno);31}32#endif // HAVE_ERRNO_H3334std::string StrError(int errnum) {35std::string str;36if (errnum == 0)37return str;38#if defined(HAVE_STRERROR_R) || HAVE_DECL_STRERROR_S39const int MaxErrStrLen = 2000;40char buffer[MaxErrStrLen];41buffer[0] = '\0';42#endif4344#ifdef HAVE_STRERROR_R45// strerror_r is thread-safe.46#if defined(__GLIBC__) && defined(_GNU_SOURCE)47// glibc defines its own incompatible version of strerror_r48// which may not use the buffer supplied.49str = strerror_r(errnum, buffer, MaxErrStrLen - 1);50#else51strerror_r(errnum, buffer, MaxErrStrLen - 1);52str = buffer;53#endif54#elif HAVE_DECL_STRERROR_S // "Windows Secure API"55strerror_s(buffer, MaxErrStrLen - 1, errnum);56str = buffer;57#else58// Copy the thread un-safe result of strerror into59// the buffer as fast as possible to minimize impact60// of collision of strerror in multiple threads.61str = strerror(errnum);62#endif63return str;64}6566} // namespace sys67} // namespace llvm686970