LLVM API Documentation

Errno.cpp

Go to the documentation of this file.
00001 //===- Errno.cpp - errno support --------------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the errno wrappers.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/System/Errno.h"
00015 #include "llvm/Config/config.h"     // Get autoconf configuration settings
00016 
00017 #if HAVE_STRING_H
00018 #include <string.h>
00019 
00020 #if HAVE_ERRNO_H
00021 #include <errno.h>
00022 #endif
00023 
00024 //===----------------------------------------------------------------------===//
00025 //=== WARNING: Implementation here must contain only TRULY operating system
00026 //===          independent code.
00027 //===----------------------------------------------------------------------===//
00028 
00029 namespace llvm {
00030 namespace sys {
00031 
00032 #if HAVE_ERRNO_H
00033 std::string StrError() {
00034   return StrError(errno);
00035 }
00036 #endif  // HAVE_ERRNO_H
00037 
00038 std::string StrError(int errnum) {
00039   const int MaxErrStrLen = 2000;
00040   char buffer[MaxErrStrLen];
00041   buffer[0] = '\0';
00042   char* str = buffer;
00043 #ifdef HAVE_STRERROR_R
00044   // strerror_r is thread-safe.
00045   if (errnum)
00046 # if defined(__GLIBC__) && defined(_GNU_SOURCE)
00047     // glibc defines its own incompatible version of strerror_r
00048     // which may not use the buffer supplied.
00049     str = strerror_r(errnum,buffer,MaxErrStrLen-1);
00050 # else
00051     strerror_r(errnum,buffer,MaxErrStrLen-1);
00052 # endif
00053 #elif defined(HAVE_STRERROR_S)  // Windows.
00054     if (errnum)
00055       strerror_s(buffer, errnum);
00056 #elif defined(HAVE_STRERROR)
00057   // Copy the thread un-safe result of strerror into
00058   // the buffer as fast as possible to minimize impact
00059   // of collision of strerror in multiple threads.
00060   if (errnum)
00061     strncpy(buffer,strerror(errnum),MaxErrStrLen-1);
00062   buffer[MaxErrStrLen-1] = '\0';
00063 #else
00064   // Strange that this system doesn't even have strerror
00065   // but, oh well, just use a generic message
00066   sprintf(buffer, "Error #%d", errnum);
00067 #endif
00068   return str;
00069 }
00070 
00071 }  // namespace sys
00072 }  // namespace llvm
00073 
00074 #endif  // HAVE_STRING_H