blob: c2c06dd114e507eb58c00efaa6a7b73cb5e1923a [file] [log] [blame]
Reid Spencerc3de9522004-08-29 19:24:20 +00001//===- llvm/System/Unix/Unix.h - Common Unix Include File -------*- C++ -*-===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Reid Spencerb89a2232004-08-25 06:20:07 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Reid Spencerb89a2232004-08-25 06:20:07 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines things specific to Unix implementations.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer7c1e2f92004-12-27 06:17:50 +000014#ifndef LLVM_SYSTEM_UNIX_UNIX_H
15#define LLVM_SYSTEM_UNIX_UNIX_H
16
Reid Spencerb89a2232004-08-25 06:20:07 +000017//===----------------------------------------------------------------------===//
18//=== WARNING: Implementation here must contain only generic UNIX code that
19//=== is guaranteed to work on all UNIX variants.
20//===----------------------------------------------------------------------===//
21
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Config/config.h" // Get autoconf configuration settings
Reid Spencerc3de9522004-08-29 19:24:20 +000023#include <cstdlib>
24#include <cstdio>
25#include <cstring>
26#include <cerrno>
Reid Spencer387e5ec2004-08-31 17:43:29 +000027#include <string>
Chris Lattner194ca802004-12-08 16:10:52 +000028#include <algorithm>
Reid Spencerb89a2232004-08-25 06:20:07 +000029
Reid Spencer7c1e2f92004-12-27 06:17:50 +000030#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 Brukmanf976c852005-04-21 22:55:34 +000040#endif
Reid Spencer7c1e2f92004-12-27 06:17:50 +000041
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
Reid Spencer4ce5dc62006-08-21 06:02:44 +000069/// This function builds an error message into \p ErrMsg using the \p prefix
70/// string and the Unix error number given by \p errnum. If errnum is -1, the
71/// default then the value of errno is used.
72/// @brief Make an error message
Daniel Dunbara7f2a9e2009-04-20 20:50:13 +000073///
74/// If the error number can be converted to a string, it will be
75/// separated from prefix by ": ".
Dan Gohman844731a2008-05-13 00:00:25 +000076static inline bool MakeErrMsg(
Reid Spencer4ce5dc62006-08-21 06:02:44 +000077 std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
78 if (!ErrMsg)
Reid Spencer5a060772006-08-23 07:30:48 +000079 return true;
Reid Spencer4ce5dc62006-08-21 06:02:44 +000080 char buffer[MAXPATHLEN];
81 buffer[0] = 0;
Dan Gohman070c42f2009-06-15 18:05:46 +000082 char* str = buffer;
Reid Spencer4ce5dc62006-08-21 06:02:44 +000083 if (errnum == -1)
84 errnum = errno;
85#ifdef HAVE_STRERROR_R
86 // strerror_r is thread-safe.
87 if (errnum)
Dan Gohman070c42f2009-06-15 18:05:46 +000088# if defined(__GLIBC__) && defined(_GNU_SOURCE)
89 // glibc defines its own incompatible version of strerror_r
90 // which may not use the buffer supplied.
91 str = strerror_r(errnum,buffer,MAXPATHLEN-1);
92# else
Reid Spencer4ce5dc62006-08-21 06:02:44 +000093 strerror_r(errnum,buffer,MAXPATHLEN-1);
Dan Gohman070c42f2009-06-15 18:05:46 +000094# endif
Reid Spencer4ce5dc62006-08-21 06:02:44 +000095#elif HAVE_STRERROR
96 // Copy the thread un-safe result of strerror into
97 // the buffer as fast as possible to minimize impact
98 // of collision of strerror in multiple threads.
99 if (errnum)
100 strncpy(buffer,strerror(errnum),MAXPATHLEN-1);
101 buffer[MAXPATHLEN-1] = 0;
102#else
103 // Strange that this system doesn't even have strerror
104 // but, oh well, just use a generic message
105 sprintf(buffer, "Error #%d", errnum);
106#endif
Dan Gohman070c42f2009-06-15 18:05:46 +0000107 *ErrMsg = prefix + ": " + str;
Reid Spencer5a060772006-08-23 07:30:48 +0000108 return true;
Reid Spencer4ce5dc62006-08-21 06:02:44 +0000109}
110
Reid Spencer7c1e2f92004-12-27 06:17:50 +0000111#endif