Reid Spencer | c3de952 | 2004-08-29 19:24:20 +0000 | [diff] [blame] | 1 | //===- llvm/System/Unix/Unix.h - Common Unix Include File -------*- C++ -*-===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 5 | // This file was developed by Reid Spencer and is distributed under the |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines things specific to Unix implementations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | 7c1e2f9 | 2004-12-27 06:17:50 +0000 | [diff] [blame] | 14 | #ifndef LLVM_SYSTEM_UNIX_UNIX_H |
| 15 | #define LLVM_SYSTEM_UNIX_UNIX_H |
| 16 | |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
| 18 | //=== WARNING: Implementation here must contain only generic UNIX code that |
| 19 | //=== is guaranteed to work on all UNIX variants. |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/Config/config.h" // Get autoconf configuration settings |
Reid Spencer | c3de952 | 2004-08-29 19:24:20 +0000 | [diff] [blame] | 23 | #include <cstdlib> |
| 24 | #include <cstdio> |
| 25 | #include <cstring> |
| 26 | #include <cerrno> |
Reid Spencer | 387e5ec | 2004-08-31 17:43:29 +0000 | [diff] [blame] | 27 | #include <string> |
Chris Lattner | 194ca80 | 2004-12-08 16:10:52 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 29 | |
Reid Spencer | 7c1e2f9 | 2004-12-27 06:17:50 +0000 | [diff] [blame] | 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> |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 40 | #endif |
Reid Spencer | 7c1e2f9 | 2004-12-27 06:17:50 +0000 | [diff] [blame] | 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 | |
Chris Lattner | bed22d8 | 2006-07-07 17:32:37 +0000 | [diff] [blame^] | 69 | inline bool GetErrno(const std::string &prefix, std::string *ErrDest, |
| 70 | int errnum = -1) { |
| 71 | char buffer[MAXPATHLEN]; |
| 72 | |
| 73 | if (ErrDest == 0) return true; |
| 74 | |
| 75 | buffer[0] = 0; |
| 76 | if (errnum == -1) |
| 77 | errnum = errno; |
Reid Spencer | 7c1e2f9 | 2004-12-27 06:17:50 +0000 | [diff] [blame] | 78 | #ifdef HAVE_STRERROR_R |
Chris Lattner | bed22d8 | 2006-07-07 17:32:37 +0000 | [diff] [blame^] | 79 | // strerror_r is thread-safe. |
| 80 | if (errnum) |
| 81 | strerror_r(errnum, buffer, MAXPATHLEN-1); |
Reid Spencer | 7c1e2f9 | 2004-12-27 06:17:50 +0000 | [diff] [blame] | 82 | #elif HAVE_STRERROR |
Chris Lattner | bed22d8 | 2006-07-07 17:32:37 +0000 | [diff] [blame^] | 83 | // Copy the thread un-safe result of strerror into |
| 84 | // the buffer as fast as possible to minimize impact |
| 85 | // of collision of strerror in multiple threads. |
| 86 | if (errnum) |
| 87 | strncpy(buffer, strerror(errnum), MAXPATHLEN-1); |
| 88 | buffer[MAXPATHLEN-1] = 0; |
Reid Spencer | 7c1e2f9 | 2004-12-27 06:17:50 +0000 | [diff] [blame] | 89 | #else |
Chris Lattner | bed22d8 | 2006-07-07 17:32:37 +0000 | [diff] [blame^] | 90 | // Strange that this system doesn't even have strerror |
| 91 | // but, oh well, just use a generic message |
| 92 | sprintf(buffer, "Error #%d", errnum); |
Reid Spencer | 7c1e2f9 | 2004-12-27 06:17:50 +0000 | [diff] [blame] | 93 | #endif |
Chris Lattner | bed22d8 | 2006-07-07 17:32:37 +0000 | [diff] [blame^] | 94 | *ErrDest = prefix + ": " + buffer; |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | inline void ThrowErrno(const std::string& prefix, int errnum = -1) { |
| 99 | char buffer[MAXPATHLEN]; |
| 100 | buffer[0] = 0; |
| 101 | if (errnum == -1) |
| 102 | errnum = errno; |
| 103 | #ifdef HAVE_STRERROR_R |
| 104 | // strerror_r is thread-safe. |
| 105 | if (errnum) |
| 106 | strerror_r(errnum,buffer,MAXPATHLEN-1); |
| 107 | #elif HAVE_STRERROR |
| 108 | // Copy the thread un-safe result of strerror into |
| 109 | // the buffer as fast as possible to minimize impact |
| 110 | // of collision of strerror in multiple threads. |
| 111 | if (errnum) |
| 112 | strncpy(buffer,strerror(errnum),MAXPATHLEN-1); |
| 113 | buffer[MAXPATHLEN-1] = 0; |
| 114 | #else |
| 115 | // Strange that this system doesn't even have strerror |
| 116 | // but, oh well, just use a generic message |
| 117 | sprintf(buffer, "Error #%d", errnum); |
| 118 | #endif |
| 119 | throw prefix + ": " + buffer; |
Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame] | 120 | } |
Reid Spencer | 7c1e2f9 | 2004-12-27 06:17:50 +0000 | [diff] [blame] | 121 | |
| 122 | #endif |