-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcalculate_path.2.7.c
97 lines (87 loc) · 3.29 KB
/
calculate_path.2.7.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* StaticPython */ /* StaticPython-appended */
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
static void calculate_path (void) {
extern char *Py_GetProgramName(void);
static char delimiter[2] = {DELIM, '\0'};
char *rtpypath = Py_GETENV("PYTHONPATH");
char *path = getenv("PATH");
char *prog = Py_GetProgramName();
static char proc_exe_path[] = "/proc/self/exe";
char *xzip_path;
char *buf;
/* If there is no slash in the argv0 path, then we have to
* assume python is on the user's $PATH, since there's no
* other way to find a directory to start the search from. If
* $PATH isn't exported, you lose.
*/
if (strchr(prog, SEP))
strncpy(progpath, prog, MAXPATHLEN);
else if (path) {
while (1) {
char *delim = strchr(path, DELIM);
if (delim) {
size_t len = delim - path;
if (len > MAXPATHLEN)
len = MAXPATHLEN;
strncpy(progpath, path, len);
*(progpath + len) = '\0';
}
else
strncpy(progpath, path, MAXPATHLEN);
joinpath(progpath, prog);
if (isxfile(progpath))
break;
if (!delim) {
progpath[0] = '\0';
break;
}
path = delim + 1;
}
}
else
progpath[0] = '\0';
if (progpath[0] != SEP && progpath[0] != '\0')
absolutize(progpath);
/**** pts ****/
{ int fd = open(proc_exe_path, O_RDONLY);
char hdr[22];
/* fprintf(stderr, "progpath=(%s)\n", progpath); */
if (fd < 0) { /* If /proc is not avaialbe, e.g. in chroot */
after_bad_proc_exe:
xzip_path = progpath; /* Use argv[0] for the .zip filename */
} else {
xzip_path = proc_exe_path;
if (lseek(fd, -22, SEEK_END) < 0) {
bad_proc_exe:
close(fd);
goto after_bad_proc_exe;
}
if (read(fd, hdr, 22) != 22) goto bad_proc_exe;
/* ZIP end-ef-central-directory header with comment size == 0. */
/* We check this because Linux i386 code running Docker on macOS
* Ventura 13 with Apple Silicon doesn't have a working
* /proc/self/exe, so we fall back to progpath (arg[0], sys.interpreter).
*/
if (memcmp(hdr, "PK\5\6", 4) != 0 || hdr[20] != 0 || hdr[21] != 0) goto bad_proc_exe;
close(fd);
}
}
/*fprintf(stderr, "info: xzip_path=(%s)\n", xzip_path);*/
/**** pts ****/
if (rtpypath == NULL || rtpypath[0] == '\0') {
module_search_path = xzip_path;
} else if (NULL == (buf = (char *)PyMem_Malloc(
2 + strlen(xzip_path) + strlen(rtpypath)))) {
/* We can't exit, so print a warning and limp along */
fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n");
fprintf(stderr, "Using default static PYTHONPATH.\n");
module_search_path = xzip_path;
} else {
strcpy(buf, rtpypath);
strcat(buf, delimiter);
strcat(buf, xzip_path);
module_search_path = buf;
}
}