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