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 bool GetErrno(const std::string &prefix, std::string *ErrDest,
00070                      int errnum = -1) {
00071   char buffer[MAXPATHLEN];
00072   
00073   if (ErrDest == 0) return true;
00074   
00075   buffer[0] = 0;
00076   if (errnum == -1)
00077     errnum = errno;
00078 #ifdef HAVE_STRERROR_R
00079   // strerror_r is thread-safe.
00080   if (errnum)
00081     strerror_r(errnum, buffer, MAXPATHLEN-1);
00082 #elif HAVE_STRERROR
00083   // Copy the thread un-safe result of strerror into
00084   // the buffer as fast as possible to minimize impact
00085   // of collision of strerror in multiple threads.
00086   if (errnum)
00087     strncpy(buffer, strerror(errnum), MAXPATHLEN-1);
00088   buffer[MAXPATHLEN-1] = 0;
00089 #else
00090   // Strange that this system doesn't even have strerror
00091   // but, oh well, just use a generic message
00092   sprintf(buffer, "Error #%d", errnum);
00093 #endif
00094   *ErrDest = prefix + ": " + buffer;
00095   return true;
00096 }
00097 
00098 inline void ThrowErrno(const std::string& prefix, int errnum = -1) {
00099   char buffer[MAXPATHLEN];
00100   buffer[0] = 0;
00101   if (errnum == -1)
00102     errnum = errno;
00103 #ifdef HAVE_STRERROR_R
00104   // strerror_r is thread-safe.
00105   if (errnum)
00106     strerror_r(errnum,buffer,MAXPATHLEN-1);
00107 #elif HAVE_STRERROR
00108   // Copy the thread un-safe result of strerror into
00109   // the buffer as fast as possible to minimize impact
00110   // of collision of strerror in multiple threads.
00111   if (errnum)
00112     strncpy(buffer,strerror(errnum),MAXPATHLEN-1);
00113   buffer[MAXPATHLEN-1] = 0;
00114 #else
00115   // Strange that this system doesn't even have strerror
00116   // but, oh well, just use a generic message
00117   sprintf(buffer, "Error #%d", errnum);
00118 #endif
00119   throw prefix + ": " + buffer;
00120 }
00121 
00122 #endif