LLVM API Documentation

Unix.h

Go to the documentation of this file.
00001 //===- llvm/System/Unix/Unix.h - Common Unix Include File -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by Reid Spencer and is distributed under the
00006 // University of Illinois Open Source 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 inline void ThrowErrno(const std::string& prefix, int errnum = -1) {
00070     char buffer[MAXPATHLEN];
00071     buffer[0] = 0;
00072     if (errnum == -1)
00073       errnum = errno;
00074 #ifdef HAVE_STRERROR_R
00075     // strerror_r is thread-safe.
00076     if (errnum)
00077       strerror_r(errnum,buffer,MAXPATHLEN-1);
00078 #elif HAVE_STRERROR
00079     // Copy the thread un-safe result of strerror into
00080     // the buffer as fast as possible to minimize impact
00081     // of collision of strerror in multiple threads.
00082     if (errnum)
00083       strncpy(buffer,strerror(errnum),MAXPATHLEN-1);
00084     buffer[MAXPATHLEN-1] = 0;
00085 #else
00086     // Strange that this system doesn't even have strerror
00087     // but, oh well, just use a generic message
00088     sprintf(buffer, "Error #%d", errnum);
00089 #endif
00090     throw prefix + ": " + buffer;
00091 }
00092 
00093 #endif