Unix.h revision 218885
1218885Sdim//===- llvm/Support/Unix/Unix.h - Common Unix Include File -------*- C++ -*-===// 2218885Sdim// 3218885Sdim// The LLVM Compiler Infrastructure 4218885Sdim// 5218885Sdim// This file is distributed under the University of Illinois Open Source 6218885Sdim// License. See LICENSE.TXT for details. 7218885Sdim// 8218885Sdim//===----------------------------------------------------------------------===// 9218885Sdim// 10218885Sdim// This file defines things specific to Unix implementations. 11218885Sdim// 12218885Sdim//===----------------------------------------------------------------------===// 13218885Sdim 14218885Sdim#ifndef LLVM_SYSTEM_UNIX_UNIX_H 15218885Sdim#define LLVM_SYSTEM_UNIX_UNIX_H 16218885Sdim 17218885Sdim//===----------------------------------------------------------------------===// 18218885Sdim//=== WARNING: Implementation here must contain only generic UNIX code that 19218885Sdim//=== is guaranteed to work on all UNIX variants. 20218885Sdim//===----------------------------------------------------------------------===// 21218885Sdim 22218885Sdim#include "llvm/Config/config.h" // Get autoconf configuration settings 23218885Sdim#include "llvm/Support/Errno.h" 24218885Sdim#include <cstdlib> 25218885Sdim#include <cstdio> 26218885Sdim#include <cstring> 27218885Sdim#include <cerrno> 28218885Sdim#include <string> 29218885Sdim#include <algorithm> 30218885Sdim 31218885Sdim#ifdef HAVE_UNISTD_H 32218885Sdim#include <unistd.h> 33218885Sdim#endif 34218885Sdim 35218885Sdim#ifdef HAVE_SYS_TYPES_H 36218885Sdim#include <sys/types.h> 37218885Sdim#endif 38218885Sdim 39218885Sdim#ifdef HAVE_SYS_PARAM_H 40218885Sdim#include <sys/param.h> 41218885Sdim#endif 42218885Sdim 43218885Sdim#ifdef HAVE_ASSERT_H 44218885Sdim#include <assert.h> 45218885Sdim#endif 46218885Sdim 47218885Sdim#ifdef TIME_WITH_SYS_TIME 48218885Sdim# include <sys/time.h> 49218885Sdim# include <time.h> 50218885Sdim#else 51218885Sdim# ifdef HAVE_SYS_TIME_H 52218885Sdim# include <sys/time.h> 53218885Sdim# else 54218885Sdim# include <time.h> 55218885Sdim# endif 56218885Sdim#endif 57218885Sdim 58218885Sdim#ifdef HAVE_SYS_WAIT_H 59218885Sdim# include <sys/wait.h> 60218885Sdim#endif 61218885Sdim 62218885Sdim#ifndef WEXITSTATUS 63218885Sdim# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8) 64218885Sdim#endif 65218885Sdim 66218885Sdim#ifndef WIFEXITED 67218885Sdim# define WIFEXITED(stat_val) (((stat_val) & 255) == 0) 68218885Sdim#endif 69218885Sdim 70218885Sdim/// This function builds an error message into \p ErrMsg using the \p prefix 71218885Sdim/// string and the Unix error number given by \p errnum. If errnum is -1, the 72218885Sdim/// default then the value of errno is used. 73218885Sdim/// @brief Make an error message 74218885Sdim/// 75218885Sdim/// If the error number can be converted to a string, it will be 76218885Sdim/// separated from prefix by ": ". 77218885Sdimstatic inline bool MakeErrMsg( 78218885Sdim std::string* ErrMsg, const std::string& prefix, int errnum = -1) { 79218885Sdim if (!ErrMsg) 80218885Sdim return true; 81218885Sdim if (errnum == -1) 82218885Sdim errnum = errno; 83218885Sdim *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum); 84218885Sdim return true; 85218885Sdim} 86218885Sdim 87218885Sdim#endif 88