RT#133573: $^X fallback when platform-specific technique fails
authorAaron Crane <arc@cpan.org>
Tue, 9 Oct 2018 13:41:10 +0000 (14:41 +0100)
committerAaron Crane <arc@cpan.org>
Tue, 9 Oct 2018 13:47:40 +0000 (14:47 +0100)
caretx.c

index d758f73..247708d 100644 (file)
--- a/caretx.c
+++ b/caretx.c
@@ -56,7 +56,19 @@ Perl_set_caret_X(pTHX) {
     SV *const caret_x = GvSV(tmpgv);
 #if defined(OS2)
     sv_setpv(caret_x, os2_execname(aTHX));
-#elif defined(USE_KERN_PROC_PATHNAME)
+    return;
+#elif defined(WIN32)
+    char *ansi;
+    WCHAR widename[MAX_PATH];
+    GetModuleFileNameW(NULL, widename, sizeof(widename)/sizeof(WCHAR));
+    ansi = win32_ansipath(widename);
+    sv_setpv(caret_x, ansi);
+    win32_free(ansi);
+    return;
+#else
+    /* We can try a platform-specific one if possible; if it fails, or we
+     * aren't running on a suitable platform, we'll fall back to argv[0]. */
+# ifdef USE_KERN_PROC_PATHNAME
     size_t size = 0;
     int mib[4];
     mib[0] = CTL_KERN;
@@ -76,7 +88,7 @@ Perl_set_caret_X(pTHX) {
             return;
         }
     }
-#elif defined(USE_NSGETEXECUTABLEPATH)
+# elif defined(USE_NSGETEXECUTABLEPATH)
     char buf[1];
     uint32_t size = sizeof(buf);
 
@@ -95,7 +107,7 @@ Perl_set_caret_X(pTHX) {
             return;
         }
     }
-#elif defined(HAS_PROCSELFEXE)
+# elif defined(HAS_PROCSELFEXE)
     char buf[MAXPATHLEN];
     SSize_t len = readlink(PROCSELFEXE_PATH, buf, sizeof(buf) - 1);
     /* NOTE: if the length returned by readlink() is sizeof(buf) - 1,
@@ -125,15 +137,7 @@ Perl_set_caret_X(pTHX) {
         sv_setpvn(caret_x, buf, len);
         return;
     }
-#elif defined(WIN32)
-    char *ansi;
-    WCHAR widename[MAX_PATH];
-    GetModuleFileNameW(NULL, widename, sizeof(widename)/sizeof(WCHAR));
-    ansi = win32_ansipath(widename);
-    sv_setpv(caret_x, ansi);
-    win32_free(ansi);
-    return;
-#else
+# endif
     /* Fallback to this:  */
     sv_setpv(caret_x, PL_origargv[0]);
 #endif