Implement get_own_executable_path for SunOS (#36514)
authorAdeel Mujahid <adeelbm@outlook.com>
Fri, 15 May 2020 09:11:39 +0000 (12:11 +0300)
committerGitHub <noreply@github.com>
Fri, 15 May 2020 09:11:39 +0000 (11:11 +0200)
src/installer/corehost/cli/hostmisc/pal.unix.cpp

index 20612a1..b097216 100644 (file)
 #define DT_LNK 10
 #endif
 
+#ifdef __linux__
+#define PAL_CWD_SIZE 0
+#elif defined(MAXPATHLEN)
+#define PAL_CWD_SIZE MAXPATHLEN
+#elif defined(PATH_MAX)
+#define PAL_CWD_SIZE PATH_MAX
+#else
+#error "Don't know how to obtain max path on this platform"
+#endif
+
 pal::string_t pal::to_lower(const pal::string_t& in)
 {
     pal::string_t ret = in;
@@ -118,7 +128,7 @@ void* pal::mmap_copy_on_write(const string_t& path, size_t* length)
 bool pal::getcwd(pal::string_t* recv)
 {
     recv->clear();
-    pal::char_t* buf = ::getcwd(nullptr, 0);
+    pal::char_t* buf = ::getcwd(nullptr, PAL_CWD_SIZE);
     if (buf == nullptr)
     {
         if (errno == ENOENT)
@@ -129,6 +139,7 @@ bool pal::getcwd(pal::string_t* recv)
         trace::error(_X("getcwd() failed: %s"), strerror(errno));
         return false;
     }
+
     recv->assign(buf);
     ::free(buf);
     return true;
@@ -768,6 +779,28 @@ bool pal::get_own_executable_path(pal::string_t* recv)
     }
     return false;
 }
+#elif defined(__sun)
+bool pal::get_own_executable_path(pal::string_t* recv)
+{
+    const char *path;
+    if ((path = getexecname()) == NULL)
+    {
+        return false;
+    }
+    else if (*path != '/')
+    {
+        if (!getcwd(recv))
+        {
+            return false;
+        }
+
+        recv->append("/").append(path);
+        return true;
+    }
+
+    recv->assign(path);
+    return true;
+}
 #else
 bool pal::get_own_executable_path(pal::string_t* recv)
 {