Skip to content

Commit

Permalink
Implement GetExecutablePath for Solaris
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Mar 9, 2016
1 parent 7ba5970 commit beac4b2
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions src/os.cc
Expand Up @@ -61,7 +61,25 @@ const char path::preferred_separator;

#ifndef _WIN32

#ifdef __APPLE__
# if defined(__linux__)

// Linux implementation.
path mp::GetExecutablePath() {
fmt::internal::MemoryBuffer<char, BUFFER_SIZE> buffer;
buffer.resize(BUFFER_SIZE);
ssize_t size = 0;
for (;;) {
size = readlink("/proc/self/exe", &buffer[0], buffer.size());
if (size < 0)
throw SystemError(errno, "cannot get executable path");
if (static_cast<size_t>(size) != buffer.size()) break;
buffer.resize(2 * buffer.size());
}
const char *s = &buffer[0];
return path(s, s + size);
}

# elif defined(__APPLE__)

// Mac OS X implementation.
path mp::GetExecutablePath() {
Expand All @@ -79,36 +97,27 @@ path mp::GetExecutablePath() {
return path(s, s + size);
}

#else
# elif defined(__sun)

// Linux implementation.
// Solaris implementation.
path mp::GetExecutablePath() {
fmt::internal::MemoryBuffer<char, BUFFER_SIZE> buffer;
buffer.resize(BUFFER_SIZE);
ssize_t size = 0;
for (;;) {
size = readlink("/proc/self/exe", &buffer[0], buffer.size());
if (size < 0)
throw SystemError(errno, "cannot get executable path");
if (static_cast<size_t>(size) != buffer.size()) break;
buffer.resize(2 * buffer.size());
}
const char *s = &buffer[0];
return path(s, s + size);
return path(getexecname());
}

#endif
# else
# error GetExecutablePath is not implemented for this system
# endif

// POSIX implementation.

path path::temp_directory_path() {
const char *dir = std::getenv("TMPDIR");
if (!dir) {
#ifdef P_tmpdir
# ifdef P_tmpdir
dir = P_tmpdir;
#else
# else
dir = "/tmp";
#endif
# endif
}
return path(dir);
}
Expand All @@ -129,7 +138,7 @@ void mp::internal::MemoryMappedFileBase::unmap() {
fmt::report_system_error(errno, "cannot unmap file");
}

#else
#else // _WIN32

// Windows implementation.

Expand Down Expand Up @@ -197,4 +206,4 @@ void mp::internal::MemoryMappedFileBase::unmap() {
throw WindowsError(GetLastError(), "cannot unmap file");
}

#endif
#endif // _WIN32

0 comments on commit beac4b2

Please sign in to comment.