blob: 361f297d364233d7a73aa9458afa31319db7458b [file] [log] [blame]
Charles Davis54c9eb62010-11-29 19:44:50 +00001//===- llvm/Support/Unix/Unix.h - Common Unix Include File -------*- C++ -*-===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Reid Spencer814ba572004-08-25 06:20:07 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman10468d82005-04-21 22:55:34 +00007//
Reid Spencer814ba572004-08-25 06:20:07 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines things specific to Unix implementations.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencere925d8b2004-12-27 06:17:50 +000014#ifndef LLVM_SYSTEM_UNIX_UNIX_H
15#define LLVM_SYSTEM_UNIX_UNIX_H
16
Reid Spencer814ba572004-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 Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/Config/config.h" // Get autoconf configuration settings
Michael J. Spencer4d2e1502010-11-29 18:29:55 +000023#include "llvm/Support/Errno.h"
Reid Spencer2f209c42004-08-29 19:24:20 +000024#include <cstdlib>
25#include <cstdio>
26#include <cstring>
27#include <cerrno>
Reid Spencer4f484702004-08-31 17:43:29 +000028#include <string>
Chris Lattner6ef473f2004-12-08 16:10:52 +000029#include <algorithm>
Reid Spencer814ba572004-08-25 06:20:07 +000030
Reid Spencere925d8b2004-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 Brukman10468d82005-04-21 22:55:34 +000041#endif
Reid Spencere925d8b2004-12-27 06:17:50 +000042
43#ifdef HAVE_ASSERT_H
44#include <assert.h>
45#endif
46
Michael J. Spencer04b795b2012-04-23 19:00:27 +000047#ifdef HAVE_SYS_TIME_H
Reid Spencere925d8b2004-12-27 06:17:50 +000048# include <sys/time.h>
Reid Spencere925d8b2004-12-27 06:17:50 +000049#endif
Michael J. Spencer04b795b2012-04-23 19:00:27 +000050#include <time.h>
Reid Spencere925d8b2004-12-27 06:17:50 +000051
52#ifdef HAVE_SYS_WAIT_H
53# include <sys/wait.h>
54#endif
55
56#ifndef WEXITSTATUS
57# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
58#endif
59
60#ifndef WIFEXITED
61# define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
62#endif
63
Reid Spencer42bcf6e2006-08-21 06:02:44 +000064/// This function builds an error message into \p ErrMsg using the \p prefix
65/// string and the Unix error number given by \p errnum. If errnum is -1, the
66/// default then the value of errno is used.
67/// @brief Make an error message
Daniel Dunbar3222b9b2009-04-20 20:50:13 +000068///
69/// If the error number can be converted to a string, it will be
70/// separated from prefix by ": ".
Dan Gohmand78c4002008-05-13 00:00:25 +000071static inline bool MakeErrMsg(
Reid Spencer42bcf6e2006-08-21 06:02:44 +000072 std::string* ErrMsg, const std::string& prefix, int errnum = -1) {
73 if (!ErrMsg)
Reid Spencer879ed5a2006-08-23 07:30:48 +000074 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000075 if (errnum == -1)
76 errnum = errno;
Jeffrey Yasskin1a933302009-07-01 18:11:20 +000077 *ErrMsg = prefix + ": " + llvm::sys::StrError(errnum);
Reid Spencer879ed5a2006-08-23 07:30:48 +000078 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000079}
80
Reid Spencere925d8b2004-12-27 06:17:50 +000081#endif