Fix Path::GetMainExecutable on cygwin, patch by Sam Bishop.
authorChris Lattner <sabre@nondot.org>
Thu, 13 Mar 2008 05:22:05 +0000 (05:22 +0000)
committerChris Lattner <sabre@nondot.org>
Thu, 13 Mar 2008 05:22:05 +0000 (05:22 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@48328 91177308-0d34-0410-b5e6-96231b3b80d8

lib/System/Unix/Path.inc

index 8e76b543ff3befbbd25b88e1506dbfff10da1bc2..e11213294c63c36064958241bf860f0db2045fed 100644 (file)
@@ -251,8 +251,17 @@ Path::GetCurrentDirectory() {
 /// 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(__CYGWIN__)
+  char exe_link[64];
+  snprintf(exe_link, sizeof(exe_link), "/proc/%d/exe", getpid());
+  char exe_path[MAXPATHLEN];
+  ssize_t len = readlink(exe_link, exe_path, sizeof(exe_path));
+  if (len > 0 && len < MAXPATHLEN - 1) {
+    exe_path[len] = '\0';
+    return Path(std::string(exe_path));
+  }
+#elif defined(HAVE_DLFCN_H)
   // Use dladdr to get executable path if available.
-#ifdef HAVE_DLFCN_H
   Dl_info DLInfo;
   int err = dladdr(MainAddr, &DLInfo);
   if (err != 0)