This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Increase page file quota advice in README.vms
[perl5.git] / caretx.c
index fe884e4..fc5ff93 100644 (file)
--- a/caretx.c
+++ b/caretx.c
@@ -56,8 +56,19 @@ Perl_set_caret_X(pTHX) {
     SV *const caret_x = GvSV(tmpgv);
 #if defined(OS2)
     sv_setpv(caret_x, os2_execname(aTHX));
+    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
-#  ifdef USE_KERN_PROC_PATHNAME
+    /* 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;
@@ -66,7 +77,7 @@ Perl_set_caret_X(pTHX) {
     mib[3] = -1;
 
     if (sysctl(mib, 4, NULL, &size, NULL, 0) == 0
-        && size > 0 && size < MAXPATHLEN * MAXPATHLEN) {
+        && inRANGE(size, 1, -1 + MAXPATHLEN * MAXPATHLEN)) {
         sv_grow(caret_x, size);
 
         if (sysctl(mib, 4, SvPVX(caret_x), &size, NULL, 0) == 0
@@ -77,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);
 
@@ -96,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,
@@ -126,7 +137,7 @@ Perl_set_caret_X(pTHX) {
         sv_setpvn(caret_x, buf, len);
         return;
     }
-#  endif
+# endif
     /* Fallback to this:  */
     sv_setpv(caret_x, PL_origargv[0]);
 #endif