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/Support/Errno.h"
00015 #include "llvm/Config/config.h"     // Get autoconf configuration settings
00016 #include "llvm/Support/raw_ostream.h"
00017 #include <string.h>
00018 
00019 #if HAVE_ERRNO_H
00020 #include <errno.h>
00021 #endif
00022 
00023 //===----------------------------------------------------------------------===//
00024 //=== WARNING: Implementation here must contain only TRULY operating system
00025 //===          independent code.
00026 //===----------------------------------------------------------------------===//
00027 
00028 namespace llvm {
00029 namespace sys {
00030 
00031 #if HAVE_ERRNO_H
00032 std::string StrError() {
00033   return StrError(errno);
00034 }
00035 #endif  // HAVE_ERRNO_H
00036 
00037 std::string StrError(int errnum) {
00038   const int MaxErrStrLen = 2000;
00039   char buffer[MaxErrStrLen];
00040   buffer[0] = '\0';
00041   std::string str;
00042   if (errnum == 0)
00043     return str;
00044 
00045 #ifdef HAVE_STRERROR_R
00046   // strerror_r is thread-safe.
00047 #if defined(__GLIBC__) && defined(_GNU_SOURCE)
00048   // glibc defines its own incompatible version of strerror_r
00049   // which may not use the buffer supplied.
00050   str = strerror_r(errnum, buffer, MaxErrStrLen - 1);
00051 #else
00052   strerror_r(errnum, buffer, MaxErrStrLen - 1);
00053   str = buffer;
00054 #endif
00055 #elif HAVE_DECL_STRERROR_S // "Windows Secure API"
00056   strerror_s(buffer, MaxErrStrLen - 1, errnum);
00057   str = buffer;
00058 #elif defined(HAVE_STRERROR)
00059   // Copy the thread un-safe result of strerror into
00060   // the buffer as fast as possible to minimize impact
00061   // of collision of strerror in multiple threads.
00062   str = strerror(errnum);
00063 #else
00064   // Strange that this system doesn't even have strerror
00065   // but, oh well, just use a generic message
00066   raw_string_ostream stream(str);
00067   stream << "Error #" << errnum;
00068   stream.flush();
00069 #endif
00070   return str;
00071 }
00072 
00073 }  // namespace sys
00074 }  // namespace llvm