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