blob: c15866f3d90adaa83affcfb00ac3e4db72935f3d [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
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +000023#include "llvm/System/Errno.h"
Reid Spencerc3de9522004-08-29 19:24:20 +000024#include <cstdlib>
25#include <cstdio>
26#include <cstring>
27#include <cerrno>
Reid Spencer387e5ec2004-08-31 17:43:29 +000028#include <string>
Chris Lattner194ca802004-12-08 16:10:52 +000029#include <algorithm>
Reid Spencerb89a2232004-08-25 06:20:07 +000030
Reid Spencer7c1e2f92004-12-27 06:17:50 +000031#ifdef HAVE_UNISTD_H
32#include <unistd.h>
33#endif
34
35#ifdef HAVE_SYS_TYPES_H
36#include <sys/types.h>
37#endif
38
39#ifdef HAVE_SYS_PARAM_H
40#include <sys/param.h>
Misha Brukmanf976c852005-04-21 22:55:34 +000041#endif
Reid Spencer7c1e2f92004-12-27 06:17:50 +000042
43#ifdef HAVE_ASSERT_H
44#include <assert.h>
45#endif
46
47#ifdef TIME_WITH_SYS_TIME
48# include <sys/time.h>
49# include <time.h>
50#else
51# ifdef HAVE_SYS_TIME_H
52# include <sys/time.h>
53# else
54# include <time.h>
55# endif
56#endif
57
58#ifdef HAVE_SYS_WAIT_H
59# include <sys/wait.h>
60#endif
61
62#ifndef WEXITSTATUS
63# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
64#endif
65
66#ifndef WIFEXITED
67# define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
68#endif
69
Reid Spencer4ce5dc62006-08-21 06:02:44 +000070/// This function builds an error message into \p ErrMsg using the \p prefix
71/// string and the Unix error number given by \p errnum. If errnum is -1, the
72/// default then the value of errno is used.
73/// @brief Make an error message
Daniel Dunbara7f2a9e2009-04-20 20:50:13 +000074///
75/// If the error number can be converted to a string, it will be
76/// separated from prefix by ": ".
Dan Gohman844731a2008-05-13 00:00:25 +000077static inline bool MakeErrMsg(
Reid Spencer4ce5dc62006-08-21 06:02:44 +000078 std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
79 if (!ErrMsg)
Reid Spencer5a060772006-08-23 07:30:48 +000080 return true;
Reid Spencer4ce5dc62006-08-21 06:02:44 +000081 if (errnum == -1)
82 errnum = errno;
Jeffrey Yasskined1c0ff2009-07-01 18:11:20 +000083 *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum);
Reid Spencer5a060772006-08-23 07:30:48 +000084 return true;
Reid Spencer4ce5dc62006-08-21 06:02:44 +000085}
86
Reid Spencer7c1e2f92004-12-27 06:17:50 +000087#endif