LLVM API Documentation
00001 //===- llvm/System/Unix/Unix.h - Common Unix Include File -------*- 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 defines things specific to Unix implementations. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_SYSTEM_UNIX_UNIX_H 00015 #define LLVM_SYSTEM_UNIX_UNIX_H 00016 00017 //===----------------------------------------------------------------------===// 00018 //=== WARNING: Implementation here must contain only generic UNIX code that 00019 //=== is guaranteed to work on all UNIX variants. 00020 //===----------------------------------------------------------------------===// 00021 00022 #include "llvm/Config/config.h" // Get autoconf configuration settings 00023 #include <cstdlib> 00024 #include <cstdio> 00025 #include <cstring> 00026 #include <cerrno> 00027 #include <string> 00028 #include <algorithm> 00029 00030 #ifdef HAVE_UNISTD_H 00031 #include <unistd.h> 00032 #endif 00033 00034 #ifdef HAVE_SYS_TYPES_H 00035 #include <sys/types.h> 00036 #endif 00037 00038 #ifdef HAVE_SYS_PARAM_H 00039 #include <sys/param.h> 00040 #endif 00041 00042 #ifdef HAVE_ASSERT_H 00043 #include <assert.h> 00044 #endif 00045 00046 #ifdef TIME_WITH_SYS_TIME 00047 # include <sys/time.h> 00048 # include <time.h> 00049 #else 00050 # ifdef HAVE_SYS_TIME_H 00051 # include <sys/time.h> 00052 # else 00053 # include <time.h> 00054 # endif 00055 #endif 00056 00057 #ifdef HAVE_SYS_WAIT_H 00058 # include <sys/wait.h> 00059 #endif 00060 00061 #ifndef WEXITSTATUS 00062 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) 00063 #endif 00064 00065 #ifndef WIFEXITED 00066 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0) 00067 #endif 00068 00069 /// This function builds an error message into \p ErrMsg using the \p prefix 00070 /// string and the Unix error number given by \p errnum. If errnum is -1, the 00071 /// default then the value of errno is used. 00072 /// @brief Make an error message 00073 static inline bool MakeErrMsg( 00074 std::string* ErrMsg, const std::string& prefix, int errnum = -1) { 00075 if (!ErrMsg) 00076 return true; 00077 char buffer[MAXPATHLEN]; 00078 buffer[0] = 0; 00079 if (errnum == -1) 00080 errnum = errno; 00081 #ifdef HAVE_STRERROR_R 00082 // strerror_r is thread-safe. 00083 if (errnum) 00084 strerror_r(errnum,buffer,MAXPATHLEN-1); 00085 #elif HAVE_STRERROR 00086 // Copy the thread un-safe result of strerror into 00087 // the buffer as fast as possible to minimize impact 00088 // of collision of strerror in multiple threads. 00089 if (errnum) 00090 strncpy(buffer,strerror(errnum),MAXPATHLEN-1); 00091 buffer[MAXPATHLEN-1] = 0; 00092 #else 00093 // Strange that this system doesn't even have strerror 00094 // but, oh well, just use a generic message 00095 sprintf(buffer, "Error #%d", errnum); 00096 #endif 00097 *ErrMsg = prefix + buffer; 00098 return true; 00099 } 00100 00101 #endif