4 * by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
15 /* This file contains a single function, set_caret_X, to set the $^X
16 * variable. It's only used in perl.c, but has various OS dependencies,
17 * so its been moved to its own file to reduce header pollution.
18 * See RT 120314 for details.
21 #if defined(PERL_IS_MINIPERL) && !defined(USE_SITECUSTOMIZE)
22 # define USE_SITECUSTOMIZE
33 #ifdef USE_KERN_PROC_PATHNAME
34 # include <sys/sysctl.h>
37 #ifdef USE_NSGETEXECUTABLEPATH
38 # include <mach-o/dyld.h>
42 Perl_set_caret_X(pTHX) {
44 GV* tmpgv = gv_fetchpvs("\030", GV_ADD|GV_NOTQUAL, SVt_PV); /* $^X */
46 SV *const caret_x = GvSV(tmpgv);
48 sv_setpv(caret_x, os2_execname(aTHX));
50 # ifdef USE_KERN_PROC_PATHNAME
55 mib[2] = KERN_PROC_PATHNAME;
58 if (sysctl(mib, 4, NULL, &size, NULL, 0) == 0
59 && size > 0 && size < MAXPATHLEN * MAXPATHLEN) {
60 sv_grow(caret_x, size);
62 if (sysctl(mib, 4, SvPVX(caret_x), &size, NULL, 0) == 0
65 SvCUR_set(caret_x, size - 1);
70 # elif defined(USE_NSGETEXECUTABLEPATH)
72 uint32_t size = sizeof(buf);
74 _NSGetExecutablePath(buf, &size);
75 if (size < MAXPATHLEN * MAXPATHLEN) {
76 sv_grow(caret_x, size);
77 if (_NSGetExecutablePath(SvPVX(caret_x), &size) == 0) {
78 char *const tidied = realpath(SvPVX(caret_x), NULL);
80 sv_setpv(caret_x, tidied);
84 SvCUR_set(caret_x, size);
89 # elif defined(HAS_PROCSELFEXE)
91 int len = readlink(PROCSELFEXE_PATH, buf, sizeof(buf) - 1);
93 /* On Playstation2 Linux V1.0 (kernel 2.2.1) readlink(/proc/self/exe)
94 includes a spurious NUL which will cause $^X to fail in system
95 or backticks (this will prevent extensions from being built and
96 many tests from working). readlink is not meant to add a NUL.
97 Normal readlink works fine.
99 if (len > 0 && buf[len-1] == '\0') {
103 /* FreeBSD's implementation is acknowledged to be imperfect, sometimes
104 returning the text "unknown" from the readlink rather than the path
105 to the executable (or returning an error from the readlink). Any
106 valid path has a '/' in it somewhere, so use that to validate the
107 result. See http://www.freebsd.org/cgi/query-pr.cgi?pr=35703
109 if (len > 0 && memchr(buf, '/', len)) {
110 sv_setpvn(caret_x, buf, len);
114 /* Fallback to this: */
115 sv_setpv(caret_x, PL_origargv[0]);
122 * c-indentation-style: bsd
124 * indent-tabs-mode: nil
127 * ex: set ts=8 sts=4 sw=4 et: