aboutsummaryrefslogtreecommitdiff
path: root/lib/System/Unix
diff options
context:
space:
mode:
authorMichael J. Spencer <bigcheesegs@gmail.com>2010-11-29 18:16:10 +0000
committerMichael J. Spencer <bigcheesegs@gmail.com>2010-11-29 18:16:10 +0000
commit1f6efa3996dd1929fbc129203ce5009b620e6969 (patch)
tree6b782914982f90d3a983bcefef98b8ef68ab2961 /lib/System/Unix
parent9363f739cdc3bd02e8516a25de0090f52ae12fbb (diff)
Merge System into Support.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120298 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/System/Unix')
-rw-r--r--lib/System/Unix/Alarm.inc72
-rw-r--r--lib/System/Unix/Host.inc96
-rw-r--r--lib/System/Unix/Memory.inc151
-rw-r--r--lib/System/Unix/Mutex.inc43
-rw-r--r--lib/System/Unix/Path.inc896
-rw-r--r--lib/System/Unix/Process.inc295
-rw-r--r--lib/System/Unix/Program.inc422
-rw-r--r--lib/System/Unix/README.txt16
-rw-r--r--lib/System/Unix/RWMutex.inc43
-rw-r--r--lib/System/Unix/Signals.inc303
-rw-r--r--lib/System/Unix/ThreadLocal.inc26
-rw-r--r--lib/System/Unix/TimeValue.inc56
-rw-r--r--lib/System/Unix/Unix.h87
-rw-r--r--lib/System/Unix/system_error.inc34
14 files changed, 0 insertions, 2540 deletions
diff --git a/lib/System/Unix/Alarm.inc b/lib/System/Unix/Alarm.inc
deleted file mode 100644
index fb42b6c65d..0000000000
--- a/lib/System/Unix/Alarm.inc
+++ /dev/null
@@ -1,72 +0,0 @@
-//===-- Alarm.inc - Implement Unix Alarm Support ----------------*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file implements the UNIX Alarm support.
-//
-//===----------------------------------------------------------------------===//
-
-#include <signal.h>
-#include <unistd.h>
-#include <cassert>
-using namespace llvm;
-
-/// AlarmCancelled - This flag is set by the SIGINT signal handler if the
-/// user presses CTRL-C.
-static volatile bool AlarmCancelled = false;
-
-/// AlarmTriggered - This flag is set by the SIGALRM signal handler if the
-/// alarm was triggered.
-static volatile bool AlarmTriggered = false;
-
-/// NestedSOI - Sanity check. Alarms cannot be nested or run in parallel.
-/// This ensures that they never do.
-static bool NestedSOI = false;
-
-static RETSIGTYPE SigIntHandler(int Sig) {
- AlarmCancelled = true;
- signal(SIGINT, SigIntHandler);
-}
-
-static RETSIGTYPE SigAlarmHandler(int Sig) {
- AlarmTriggered = true;
-}
-
-static void (*OldSigIntHandler) (int);
-
-void sys::SetupAlarm(unsigned seconds) {
- assert(!NestedSOI && "sys::SetupAlarm calls cannot be nested!");
- NestedSOI = true;
- AlarmCancelled = false;
- AlarmTriggered = false;
- ::signal(SIGALRM, SigAlarmHandler);
- OldSigIntHandler = ::signal(SIGINT, SigIntHandler);
- ::alarm(seconds);
-}
-
-void sys::TerminateAlarm() {
- assert(NestedSOI && "sys::TerminateAlarm called without sys::SetupAlarm!");
- ::alarm(0);
- ::signal(SIGALRM, SIG_DFL);
- ::signal(SIGINT, OldSigIntHandler);
- AlarmCancelled = false;
- AlarmTriggered = false;
- NestedSOI = false;
-}
-
-int sys::AlarmStatus() {
- if (AlarmCancelled)
- return -1;
- if (AlarmTriggered)
- return 1;
- return 0;
-}
-
-void sys::Sleep(unsigned n) {
- ::sleep(n);
-}
diff --git a/lib/System/Unix/Host.inc b/lib/System/Unix/Host.inc
deleted file mode 100644
index c76d6a4e18..0000000000
--- a/lib/System/Unix/Host.inc
+++ /dev/null
@@ -1,96 +0,0 @@
- //===- llvm/System/Unix/Host.inc -------------------------------*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file implements the UNIX Host support.
-//
-//===----------------------------------------------------------------------===//
-
-//===----------------------------------------------------------------------===//
-//=== WARNING: Implementation here must contain only generic UNIX code that
-//=== is guaranteed to work on *all* UNIX variants.
-//===----------------------------------------------------------------------===//
-
-#include "llvm/Config/config.h"
-#include "llvm/ADT/StringRef.h"
-#include "Unix.h"
-#include <sys/utsname.h>
-#include <string>
-
-using namespace llvm;
-
-static std::string getOSVersion() {
- struct utsname info;
-
- if (uname(&info))
- return "";
-
- return info.release;
-}
-
-std::string sys::getHostTriple() {
- // FIXME: Derive directly instead of relying on the autoconf generated
- // variable.
-
- StringRef HostTripleString(LLVM_HOSTTRIPLE);
- std::pair<StringRef, StringRef> ArchSplit = HostTripleString.split('-');
-
- // Normalize the arch, since the host triple may not actually match the host.
- std::string Arch = ArchSplit.first;
-
- // It would be nice to do this in terms of llvm::Triple, but that is in
- // Support which is layered above us.
-#if defined(__x86_64__)
- Arch = "x86_64";
-#elif defined(__i386__)
- Arch = "i386";
-#elif defined(__ppc64__)
- Arch = "powerpc64";
-#elif defined(__ppc__)
- Arch = "powerpc";
-#elif defined(__arm__)
-
- // FIXME: We need to pick the right ARM triple (which involves querying the
- // chip). However, for now this is most important for LLVM arch selection, so
- // we only need to make sure to distinguish ARM and Thumb.
-# if defined(__thumb__)
- Arch = "thumb";
-# else
- Arch = "arm";
-# endif
-
-#else
-
- // FIXME: When enough auto-detection is in place, this should just
- // #error. Then at least the arch selection is done, and we only need the OS
- // etc selection to kill off the use of LLVM_HOSTTRIPLE.
-
-#endif
-
- std::string Triple(Arch);
- Triple += '-';
- Triple += ArchSplit.second;
-
- // Force i<N>86 to i386.
- if (Triple[0] == 'i' && isdigit(Triple[1]) &&
- Triple[2] == '8' && Triple[3] == '6')
- Triple[1] = '3';
-
- // On darwin, we want to update the version to match that of the
- // host.
- std::string::size_type DarwinDashIdx = Triple.find("-darwin");
- if (DarwinDashIdx != std::string::npos) {
- Triple.resize(DarwinDashIdx + strlen("-darwin"));
-
- // Only add the major part of the os version.
- std::string Version = getOSVersion();
- Triple += Version.substr(0, Version.find('.'));
- }
-
- return Triple;
-}
diff --git a/lib/System/Unix/Memory.inc b/lib/System/Unix/Memory.inc
deleted file mode 100644
index 1b038f9c6e..0000000000
--- a/lib/System/Unix/Memory.inc
+++ /dev/null
@@ -1,151 +0,0 @@
-//===- Unix/Memory.cpp - Generic UNIX System Configuration ------*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file defines some functions for various memory management utilities.
-//
-//===----------------------------------------------------------------------===//
-
-#include "Unix.h"
-#include "llvm/System/DataTypes.h"
-#include "llvm/System/Process.h"
-
-#ifdef HAVE_SYS_MMAN_H
-#include <sys/mman.h>
-#endif
-
-#ifdef __APPLE__
-#include <mach/mach.h>
-#endif
-
-/// AllocateRWX - Allocate a slab of memory with read/write/execute
-/// permissions. This is typically used for JIT applications where we want
-/// to emit code to the memory then jump to it. Getting this type of memory
-/// is very OS specific.
-///
-llvm::sys::MemoryBlock
-llvm::sys::Memory::AllocateRWX(size_t NumBytes, const MemoryBlock* NearBlock,
- std::string *ErrMsg) {
- if (NumBytes == 0) return MemoryBlock();
-
- size_t pageSize = Process::GetPageSize();
- size_t NumPages = (NumBytes+pageSize-1)/pageSize;
-
- int fd = -1;
-#ifdef NEED_DEV_ZERO_FOR_MMAP
- static int zero_fd = open("/dev/zero", O_RDWR);
- if (zero_fd == -1) {
- MakeErrMsg(ErrMsg, "Can't open /dev/zero device");
- return MemoryBlock();
- }
- fd = zero_fd;
-#endif
-
- int flags = MAP_PRIVATE |
-#ifdef HAVE_MMAP_ANONYMOUS
- MAP_ANONYMOUS
-#else
- MAP_ANON
-#endif
- ;
-
- void* start = NearBlock ? (unsigned char*)NearBlock->base() +
- NearBlock->size() : 0;
-
-#if defined(__APPLE__) && defined(__arm__)
- void *pa = ::mmap(start, pageSize*NumPages, PROT_READ|PROT_EXEC,
- flags, fd, 0);
-#else
- void *pa = ::mmap(start, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
- flags, fd, 0);
-#endif
- if (pa == MAP_FAILED) {
- if (NearBlock) //Try again without a near hint
- return AllocateRWX(NumBytes, 0);
-
- MakeErrMsg(ErrMsg, "Can't allocate RWX Memory");
- return MemoryBlock();
- }
-
-#if defined(__APPLE__) && defined(__arm__)
- kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)pa,
- (vm_size_t)(pageSize*NumPages), 0,
- VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY);
- if (KERN_SUCCESS != kr) {
- MakeErrMsg(ErrMsg, "vm_protect max RX failed");
- return sys::MemoryBlock();
- }
-
- kr = vm_protect(mach_task_self(), (vm_address_t)pa,
- (vm_size_t)(pageSize*NumPages), 0,
- VM_PROT_READ | VM_PROT_WRITE);
- if (KERN_SUCCESS != kr) {
- MakeErrMsg(ErrMsg, "vm_protect RW failed");
- return sys::MemoryBlock();
- }
-#endif
-
- MemoryBlock result;
- result.Address = pa;
- result.Size = NumPages*pageSize;
-
- return result;
-}
-
-bool llvm::sys::Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
- if (M.Address == 0 || M.Size == 0) return false;
- if (0 != ::munmap(M.Address, M.Size))
- return MakeErrMsg(ErrMsg, "Can't release RWX Memory");
- return false;
-}
-
-bool llvm::sys::Memory::setWritable (MemoryBlock &M, std::string *ErrMsg) {
-#if defined(__APPLE__) && defined(__arm__)
- if (M.Address == 0 || M.Size == 0) return false;
- sys::Memory::InvalidateInstructionCache(M.Address, M.Size);
- kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)M.Address,
- (vm_size_t)M.Size, 0, VM_PROT_READ | VM_PROT_WRITE);
- return KERN_SUCCESS == kr;
-#else
- return true;
-#endif
-}
-
-bool llvm::sys::Memory::setExecutable (MemoryBlock &M, std::string *ErrMsg) {
-#if defined(__APPLE__) && defined(__arm__)
- if (M.Address == 0 || M.Size == 0) return false;
- sys::Memory::InvalidateInstructionCache(M.Address, M.Size);
- kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)M.Address,
- (vm_size_t)M.Size, 0, VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY);
- return KERN_SUCCESS == kr;
-#else
- return false;
-#endif
-}
-
-bool llvm::sys::Memory::setRangeWritable(const void *Addr, size_t Size) {
-#if defined(__APPLE__) && defined(__arm__)
- kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)Addr,
- (vm_size_t)Size, 0,
- VM_PROT_READ | VM_PROT_WRITE);
- return KERN_SUCCESS == kr;
-#else
- return true;
-#endif
-}
-
-bool llvm::sys::Memory::setRangeExecutable(const void *Addr, size_t Size) {
-#if defined(__APPLE__) && defined(__arm__)
- kern_return_t kr = vm_protect(mach_task_self(), (vm_address_t)Addr,
- (vm_size_t)Size, 0,
- VM_PROT_READ | VM_PROT_EXECUTE | VM_PROT_COPY);
- return KERN_SUCCESS == kr;
-#else
- return true;
-#endif
-}
diff --git a/lib/System/Unix/Mutex.inc b/lib/System/Unix/Mutex.inc
deleted file mode 100644
index 4a5e28de27..0000000000
--- a/lib/System/Unix/Mutex.inc
+++ /dev/null
@@ -1,43 +0,0 @@
-//===- llvm/System/Unix/Mutex.inc - Unix Mutex Implementation ---*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file implements the Unix specific (non-pthread) Mutex class.
-//
-//===----------------------------------------------------------------------===//
-
-//===----------------------------------------------------------------------===//
-//=== WARNING: Implementation here must contain only generic UNIX code that
-//=== is guaranteed to work on *all* UNIX variants.
-//===----------------------------------------------------------------------===//
-
-namespace llvm
-{
-using namespace sys;
-
-MutexImpl::MutexImpl( bool recursive)
-{
-}
-
-MutexImpl::~MutexImpl()
-{
-}
-
-bool
-MutexImpl::release()
-{
- return true;
-}
-
-bool
-MutexImpl::tryacquire( void )
-{
- return true;
-}
-
-}
diff --git a/lib/System/Unix/Path.inc b/lib/System/Unix/Path.inc
deleted file mode 100644
index 5ee3adc4d2..0000000000
--- a/lib/System/Unix/Path.inc
+++ /dev/null
@@ -1,896 +0,0 @@
-//===- llvm/System/Unix/Path.cpp - Unix Path Implementation -----*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file implements the Unix specific portion of the Path class.
-//
-//===----------------------------------------------------------------------===//
-
-//===----------------------------------------------------------------------===//
-//=== WARNING: Implementation here must contain only generic UNIX code that
-//=== is guaranteed to work on *all* UNIX variants.
-//===----------------------------------------------------------------------===//
-
-#include "Unix.h"
-#if HAVE_SYS_STAT_H
-#include <sys/stat.h>
-#endif
-#if HAVE_FCNTL_H
-#include <fcntl.h>
-#endif
-#ifdef HAVE_SYS_MMAN_H
-#include <sys/mman.h>
-#endif
-#ifdef HAVE_SYS_STAT_H
-#include <sys/stat.h>
-#endif
-#if HAVE_UTIME_H
-#include <utime.h>
-#endif
-#if HAVE_TIME_H
-#include <time.h>
-#endif
-#if HAVE_DIRENT_H
-# include <dirent.h>
-# define NAMLEN(dirent) strlen((dirent)->d_name)
-#else
-# define dirent direct
-# define NAMLEN(dirent) (dirent)->d_namlen
-# if HAVE_SYS_NDIR_H
-# include <sys/ndir.h>
-# endif
-# if HAVE_SYS_DIR_H
-# include <sys/dir.h>
-# endif
-# if HAVE_NDIR_H
-# include <ndir.h>
-# endif
-#endif
-
-#if HAVE_DLFCN_H
-#include <dlfcn.h>
-#endif
-
-#ifdef __APPLE__
-#include <mach-o/dyld.h>
-#endif
-
-// Put in a hack for Cygwin which falsely reports that the mkdtemp function
-// is available when it is not.
-#ifdef __CYGWIN__
-# undef HAVE_MKDTEMP
-#endif
-
-namespace {
-inline bool lastIsSlash(const std::string& path) {
- return !path.empty() && path[path.length() - 1] == '/';
-}
-
-}
-
-namespace llvm {
-using namespace sys;
-
-const char sys::PathSeparator = ':';
-
-StringRef Path::GetEXESuffix() {
- return StringRef();
-}
-
-Path::Path(StringRef p)
- : path(p) {}
-
-Path::Path(const char *StrStart, unsigned StrLen)
- : path(StrStart, StrLen) {}
-
-Path&
-Path::operator=(StringRef that) {
- path.assign(that.data(), that.size());
- return *this;
-}
-
-bool
-Path::isValid() const {
- // Empty paths are considered invalid here.
- // This code doesn't check MAXPATHLEN because there's no need. Nothing in
- // LLVM manipulates Paths with fixed-sizes arrays, and if the OS can't
- // handle names longer than some limit, it'll report this on demand using
- // ENAMETOLONG.
- return !path.empty();
-}
-
-bool
-Path::isAbsolute(const char *NameStart, unsigned NameLen) {
- assert(NameStart);
- if (NameLen == 0)
- return false;
- return NameStart[0] == '/';
-}
-
-bool
-Path::isAbsolute() const {
- if (path.empty())
- return false;
- return path[0] == '/';
-}
-
-void Path::makeAbsolute() {
- if (isAbsolute())
- return;
-
- Path CWD = Path::GetCurrentDirectory();
- assert(CWD.isAbsolute() && "GetCurrentDirectory returned relative path!");
-
- CWD.appendComponent(path);
-
- path = CWD.str();
-}
-
-Path
-Path::GetRootDirectory() {
- Path result;
- result.set("/");
- return result;
-}
-
-Path
-Path::GetTemporaryDirectory(std::string *ErrMsg) {
-#if defined(HAVE_MKDTEMP)
- // The best way is with mkdtemp but that's not available on many systems,
- // Linux and FreeBSD have it. Others probably won't.
- char pathname[] = "/tmp/llvm_XXXXXX";
- if (0 == mkdtemp(pathname)) {
- MakeErrMsg(ErrMsg,
- std::string(pathname) + ": can't create temporary directory");
- return Path();
- }
- return Path(pathname);
-#elif defined(HAVE_MKSTEMP)
- // If no mkdtemp is available, mkstemp can be used to create a temporary file
- // which is then removed and created as a directory. We prefer this over
- // mktemp because of mktemp's inherent security and threading risks. We still
- // have a slight race condition from the time the temporary file is created to
- // the time it is re-created as a directoy.
- char pathname[] = "/tmp/llvm_XXXXXX";
- int fd = 0;
- if (-1 == (fd = mkstemp(pathname))) {
- MakeErrMsg(ErrMsg,
- std::string(pathname) + ": can't create temporary directory");
- return Path();
- }
- ::close(fd);
- ::unlink(pathname); // start race condition, ignore errors
- if (-1 == ::mkdir(pathname, S_IRWXU)) { // end race condition
- MakeErrMsg(ErrMsg,
- std::string(pathname) + ": can't create temporary directory");
- return Path();
- }
- return Path(pathname);
-#elif defined(HAVE_MKTEMP)
- // If a system doesn't have mkdtemp(3) or mkstemp(3) but it does have
- // mktemp(3) then we'll assume that system (e.g. AIX) has a reasonable
- // implementation of mktemp(3) and doesn't follow BSD 4.3's lead of replacing
- // the XXXXXX with the pid of the process and a letter. That leads to only
- // twenty six temporary files that can be generated.
- char pathname[] = "/tmp/llvm_XXXXXX";
- char *TmpName = ::mktemp(pathname);
- if (TmpName == 0) {
- MakeErrMsg(ErrMsg,
- std::string(TmpName) + ": can't create unique directory name");
- return Path();
- }
- if (-1 == ::mkdir(TmpName, S_IRWXU)) {
- MakeErrMsg(ErrMsg,
- std::string(TmpName) + ": can't create temporary directory");
- return Path();
- }
- return Path(TmpName);
-#else
- // This is the worst case implementation. tempnam(3) leaks memory unless its
- // on an SVID2 (or later) system. On BSD 4.3 it leaks. tmpnam(3) has thread
- // issues. The mktemp(3) function doesn't have enough variability in the
- // temporary name generated. So, we provide our own implementation that
- // increments an integer from a random number seeded by the current time. This
- // should be sufficiently unique that we don't have many collisions between
- // processes. Generally LLVM processes don't run very long and don't use very
- // many temporary files so this shouldn't be a big issue for LLVM.
- static time_t num = ::time(0);
- char pathname[MAXPATHLEN];
- do {
- num++;
- sprintf(pathname, "/tmp/llvm_%010u", unsigned(num));
- } while ( 0 == access(pathname, F_OK ) );
- if (-1 == ::mkdir(pathname, S_IRWXU)) {
- MakeErrMsg(ErrMsg,
- std::string(pathname) + ": can't create temporary directory");
- return Path();
- }
- return Path(pathname);
-#endif
-}
-
-void
-Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
-#ifdef LTDL_SHLIBPATH_VAR
- char* env_var = getenv(LTDL_SHLIBPATH_VAR);
- if (env_var != 0) {
- getPathList(env_var,Paths);
- }
-#endif
- // FIXME: Should this look at LD_LIBRARY_PATH too?
- Paths.push_back(sys::Path("/usr/local/lib/"));
- Paths.push_back(sys::Path("/usr/X11R6/lib/"));
- Paths.push_back(sys::Path("/usr/lib/"));
- Paths.push_back(sys::Path("/lib/"));
-}
-
-void
-Path::GetBitcodeLibraryPaths(std::vector<sys::Path>& Paths) {
- char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
- if (env_var != 0) {
- getPathList(env_var,Paths);
- }
-#ifdef LLVM_LIBDIR
- {
- Path tmpPath;
- if (tmpPath.set(LLVM_LIBDIR))
- if (tmpPath.canRead())
- Paths.push_back(tmpPath);
- }
-#endif
- GetSystemLibraryPaths(Paths);
-}
-
-Path
-Path::GetLLVMDefaultConfigDir() {
- return Path("/etc/llvm/");
-}
-
-Path
-Path::GetUserHomeDirectory() {
- const char* home = getenv("HOME");
- if (home) {
- Path result;
- if (result.set(home))
- return result;
- }
- return GetRootDirectory();
-}
-
-Path
-Path::GetCurrentDirectory() {
- char pathname[MAXPATHLEN];
- if (!getcwd(pathname,MAXPATHLEN)) {
- assert (false && "Could not query current working directory.");
- return Path();
- }
-
- return Path(pathname);
-}
-
-#if defined(__FreeBSD__) || defined (__NetBSD__) || defined(__minix)
-static int
-test_dir(char buf[PATH_MAX], char ret[PATH_MAX],
- const char *dir, const char *bin)
-{
- struct stat sb;
-
- snprintf(buf, PATH_MAX, "%s/%s", dir, bin);
- if (realpath(buf, ret) == NULL)
- return (1);
- if (stat(buf, &sb) != 0)
- return (1);
-
- return (0);
-}
-
-static char *
-getprogpath(char ret[PATH_MAX], const char *bin)
-{
- char *pv, *s, *t, buf[PATH_MAX];
-
- /* First approach: absolute path. */
- if (bin[0] == '/') {
- if (test_dir(buf, ret, "/", bin) == 0)
- return (ret);
- return (NULL);
- }
-
- /* Second approach: relative path. */
- if (strchr(bin, '/') != NULL) {
- if (getcwd(buf, PATH_MAX) == NULL)
- return (NULL);
- if (test_dir(buf, ret, buf, bin) == 0)
- return (ret);
- return (NULL);
- }
-
- /* Third approach: $PATH */
- if ((pv = getenv("PATH")) == NULL)
- return (NULL);
- s = pv = strdup(pv);
- if (pv == NULL)
- return (NULL);
- while ((t = strsep(&s, ":")) != NULL) {
- if (test_dir(buf, ret, t, bin) == 0) {
- free(pv);
- return (ret);
- }
- }
- free(pv);
- return (NULL);
-}
-#endif // __FreeBSD__ || __NetBSD__
-
-/// GetMainExecutable - Return the path to the main executable, given the
-/// value of argv[0] from program startup.
-Path Path::GetMainExecutable(const char *argv0, void *MainAddr) {
-#if defined(__APPLE__)
- // On OS X the executable path is saved to the stack by dyld. Reading it
- // from there is much faster than calling dladdr, especially for large
- // binaries with symbols.
- char exe_path[MAXPATHLEN];
- uint32_t size = sizeof(exe_path);
- if (_NSGetExecutablePath(exe_path, &size) == 0) {
- char link_path[MAXPATHLEN];
- if (realpath(exe_path, link_path))
- return Path(link_path);
- }
-#elif defined(__FreeBSD__) || defined (__NetBSD__) || defined(__minix)
- char exe_path[PATH_MAX];
-
- if (getprogpath(exe_path, argv0) != NULL)
- return Path(exe_path);
-#elif defined(__linux__) || defined(__CYGWIN__)
- char exe_path[MAXPATHLEN];
- ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path));
- if (len >= 0)
- return Path(StringRef(exe_path, len));
-#elif defined(HAVE_DLFCN_H)
- // Use dladdr to get executable path if available.
- Dl_info DLInfo;
- int err = dladdr(MainAddr, &DLInfo);
- if (err == 0)
- return Path();
-
- // If the filename is a symlink, we need to resolve and return the location of
- // the actual executable.
- char link_path[MAXPATHLEN];
- if (realpath(DLInfo.dli_fname, link_path))
- return Path(link_path);
-#else
-#error GetMainExecutable is not implemented on this host yet.
-#endif
- return Path();
-}
-
-
-StringRef Path::getDirname() const {
- return getDirnameCharSep(path, "/");
-}
-
-StringRef
-Path::getBasename() const {
- // Find the last slash
- std::string::size_type slash = path.rfind('/');
- if (slash == std::string::npos)
- slash = 0;
- else
- slash++;
-
- std::string::size_type dot = path.rfind('.');
- if (dot == std::string::npos || dot < slash)
- return StringRef(path).substr(slash);
- else
- return StringRef(path).substr(slash, dot - slash);
-}
-
-StringRef
-Path::getSuffix() const {
- // Find the last slash
- std::string::size_type slash = path.rfind('/');
- if (slash == std::string::npos)
- slash = 0;
- else
- slash++;
-
- std::string::size_type dot = path.rfind('.');
- if (dot == std::string::npos || dot < slash)
- return StringRef();
- else
- return StringRef(path).substr(dot + 1);
-}
-
-bool Path::getMagicNumber(std::string &Magic, unsigned len) const {
- assert(len < 1024 && "Request for magic string too long");
- char Buf[1025];
- int fd = ::open(path.c_str(), O_RDONLY);
- if (fd < 0)
- return false;
- ssize_t bytes_read = ::read(fd, Buf, len);
- ::close(fd);
- if (ssize_t(len) != bytes_read)
- return false;
- Magic.assign(Buf, len);
- return true;
-}
-
-bool
-Path::exists() const {
- return 0 == access(path.c_str(), F_OK );
-}
-
-bool
-Path::isDirectory() const {
- struct stat buf;
- if (0 != stat(path.c_str(), &buf))
- return false;
- return ((buf.st_mode & S_IFMT) == S_IFDIR) ? true : false;
-}
-
-bool
-Path::isSymLink() const {
- struct stat buf;
- if (0 != lstat(path.c_str(), &buf))
- return false;
- return S_ISLNK(buf.st_mode);
-}
-
-
-bool
-Path::canRead() const {
- return 0 == access(path.c_str(), R_OK);
-}
-
-bool
-Path::canWrite() const {
- return 0 == access(path.c_str(), W_OK);
-}
-
-bool
-Path::isRegularFile() const {
- // Get the status so we can determine if it's a file or directory
- struct stat buf;
-
- if (0 != stat(path.c_str(), &buf))
- return false;
-
- if (S_ISREG(buf.st_mode))
- return true;
-
- return false;
-}
-
-bool
-Path::canExecute() const {
- if (0 != access(path.c_str(), R_OK | X_OK ))
- return false;
- struct stat buf;
- if (0 != stat(path.c_str(), &buf))
- return false;
- if (!S_ISREG(buf.st_mode))
- return false;
- return true;
-}
-
-StringRef
-Path::getLast() const {
- // Find the last slash
- size_t pos = path.rfind('/');
-
- // Handle the corner cases
- if (pos == std::string::npos)
- return path;
-
- // If the last character is a slash
- if (pos == path.length()-1) {
- // Find the second to last slash
- size_t pos2 = path.rfind('/', pos-1);
- if (pos2 == std::string::npos)
- return StringRef(path).substr(0,pos);
- else
- return StringRef(path).substr(pos2+1,pos-pos2-1);
- }
- // Return everything after the last slash
- return StringRef(path).substr(pos+1);
-}
-
-const FileStatus *
-PathWithStatus::getFileStatus(bool update, std::string *ErrStr) const {
- if (!fsIsValid || update) {
- struct stat buf;
- if (0 != stat(path.c_str(), &buf)) {
- MakeErrMsg(ErrStr, path + ": can't get status of file");
- return 0;
- }
- status.fileSize = buf.st_size;
- status.modTime.fromEpochTime(buf.st_mtime);
- status.mode = buf.st_mode;
- status.user = buf.st_uid;
- status.group = buf.st_gid;
- status.uniqueID = uint64_t(buf.st_ino);
- status.isDir = S_ISDIR(buf.st_mode);
- status.isFile = S_ISREG(buf.st_mode);
- fsIsValid = true;
- }
- return &status;
-}
-
-static bool AddPermissionBits(const Path &File, int bits) {
- // Get the umask value from the operating system. We want to use it
- // when changing the file's permissions. Since calling umask() sets
- // the umask and returns its old value, we must call it a second
- // time to reset it to the user's preference.
- int mask = umask(0777); // The arg. to umask is arbitrary.
- umask(mask); // Restore the umask.
-
- // Get the file's current mode.
- struct stat buf;
- if (0 != stat(File.c_str(), &buf))
- return false;
- // Change the file to have whichever permissions bits from 'bits'
- // that the umask would not disable.
- if ((chmod(File.c_str(), (buf.st_mode | (bits & ~mask)))) == -1)
- return false;
- return true;
-}
-
-bool Path::makeReadableOnDisk(std::string* ErrMsg) {
- if (!AddPermissionBits(*this, 0444))
- return MakeErrMsg(ErrMsg, path + ": can't make file readable");
- return false;
-}
-
-bool Path::makeWriteableOnDisk(std::string* ErrMsg) {
- if (!AddPermissionBits(*this, 0222))
- return MakeErrMsg(ErrMsg, path + ": can't make file writable");
- return false;
-}
-
-bool Path::makeExecutableOnDisk(std::string* ErrMsg) {
- if (!AddPermissionBits(*this, 0111))
- return MakeErrMsg(ErrMsg, path + ": can't make file executable");
- return false;
-}
-
-bool
-Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
- DIR* direntries = ::opendir(path.c_str());
- if (direntries == 0)
- return MakeErrMsg(ErrMsg, path + ": can't open directory");
-
- std::string dirPath = path;
- if (!lastIsSlash(dirPath))
- dirPath += '/';
-
- result.clear();
- struct dirent* de = ::readdir(direntries);
- for ( ; de != 0; de = ::readdir(direntries)) {
- if (de->d_name[0] != '.') {
- Path aPath(dirPath + (const char*)de->d_name);
- struct stat st;
- if (0 != lstat(aPath.path.c_str(), &st)) {
- if (S_ISLNK(st.st_mode))
- continue; // dangling symlink -- ignore
- return MakeErrMsg(ErrMsg,
- aPath.path + ": can't determine file object type");
- }
- result.insert(aPath);
- }
- }
-
- closedir(direntries);
- return false;
-}
-
-bool
-Path::set(StringRef a_path) {
- if (a_path.empty())
- return false;
- path = a_path;
- return true;
-}
-
-bool
-Path::appendComponent(StringRef name) {
- if (name.empty())
- return false;
- if (!lastIsSlash(path))
- path += '/';
- path += name;
- return true;
-}
-
-bool
-Path::eraseComponent() {
- size_t slashpos = path.rfind('/',path.size());
- if (slashpos == 0 || slashpos == std::string::npos) {
- path.erase();
- return true;
- }
- if (slashpos == path.size() - 1)
- slashpos = path.rfind('/',slashpos-1);
- if (slashpos == std::string::npos) {
- path.erase();
- return true;
- }
- path.erase(slashpos);
- return true;
-}
-
-bool
-Path::eraseSuffix() {
- size_t dotpos = path.rfind('.',path.size());
- size_t slashpos = path.rfind('/',path.size());
- if (dotpos != std::string::npos) {
- if (slashpos == std::string::npos || dotpos > slashpos+1) {
- path.erase(dotpos, path.size()-dotpos);
- return true;
- }
- }
- return false;
-}
-
-static bool createDirectoryHelper(char* beg, char* end, bool create_parents) {
-