Make Unix.h:MakeErrMsg separate the prefix and errno string, so we get:
[oota-llvm.git] / lib / System / Unix / Unix.h
1 //===- llvm/System/Unix/Unix.h - Common Unix Include File -------*- 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 defines things specific to Unix implementations.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SYSTEM_UNIX_UNIX_H
15 #define LLVM_SYSTEM_UNIX_UNIX_H
16
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only generic UNIX code that
19 //===          is guaranteed to work on all UNIX variants.
20 //===----------------------------------------------------------------------===//
21
22 #include "llvm/Config/config.h"     // Get autoconf configuration settings
23 #include <cstdlib>
24 #include <cstdio>
25 #include <cstring>
26 #include <cerrno>
27 #include <string>
28 #include <algorithm>
29
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37
38 #ifdef HAVE_SYS_PARAM_H
39 #include <sys/param.h>
40 #endif
41
42 #ifdef HAVE_ASSERT_H
43 #include <assert.h>
44 #endif
45
46 #ifdef TIME_WITH_SYS_TIME
47 # include <sys/time.h>
48 # include <time.h>
49 #else
50 # ifdef HAVE_SYS_TIME_H
51 #  include <sys/time.h>
52 # else
53 #  include <time.h>
54 # endif
55 #endif
56
57 #ifdef HAVE_SYS_WAIT_H
58 # include <sys/wait.h>
59 #endif
60
61 #ifndef WEXITSTATUS
62 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
63 #endif
64
65 #ifndef WIFEXITED
66 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
67 #endif
68
69 /// This function builds an error message into \p ErrMsg using the \p prefix
70 /// string and the Unix error number given by \p errnum. If errnum is -1, the
71 /// default then the value of errno is used.
72 /// @brief Make an error message
73 ///
74 /// If the error number can be converted to a string, it will be
75 /// separated from prefix by ": ".
76 static inline bool MakeErrMsg(
77   std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
78   if (!ErrMsg)
79     return true;
80   char buffer[MAXPATHLEN];
81   buffer[0] = 0;
82   if (errnum == -1)
83     errnum = errno;
84 #ifdef HAVE_STRERROR_R
85   // strerror_r is thread-safe.
86   if (errnum)
87     strerror_r(errnum,buffer,MAXPATHLEN-1);
88 #elif HAVE_STRERROR
89   // Copy the thread un-safe result of strerror into
90   // the buffer as fast as possible to minimize impact
91   // of collision of strerror in multiple threads.
92   if (errnum)
93     strncpy(buffer,strerror(errnum),MAXPATHLEN-1);
94   buffer[MAXPATHLEN-1] = 0;
95 #else
96   // Strange that this system doesn't even have strerror
97   // but, oh well, just use a generic message
98   sprintf(buffer, "Error #%d", errnum);
99 #endif
100   *ErrMsg = prefix + ": " + buffer;
101   return true;
102 }
103
104 #endif