Be more rigorous about making pathnames absolute, to address SF bug

#424002.

Refactor init_path_from_argv0() and rename to copy_absolute(); add
absolutize() which does the same in-place.

Clean up whitespace (leading tabs -> spaces, delete trailing
spaces/tabs).
This commit is contained in:
Guido van Rossum 2001-09-28 20:00:29 +00:00
parent 746fe0fae5
commit e296cedef9

View file

@ -113,7 +113,7 @@
#ifndef PYTHONPATH #ifndef PYTHONPATH
#define PYTHONPATH PREFIX "/lib/python" VERSION ":" \ #define PYTHONPATH PREFIX "/lib/python" VERSION ":" \
EXEC_PREFIX "/lib/python" VERSION "/lib-dynload" EXEC_PREFIX "/lib/python" VERSION "/lib-dynload"
#endif #endif
#ifndef LANDMARK #ifndef LANDMARK
@ -137,7 +137,7 @@ reduce(char *dir)
static int static int
isfile(char *filename) /* Is file, not directory */ isfile(char *filename) /* Is file, not directory */
{ {
struct stat buf; struct stat buf;
if (stat(filename, &buf) != 0) if (stat(filename, &buf) != 0)
@ -149,7 +149,7 @@ isfile(char *filename) /* Is file, not directory */
static int static int
ismodule(char *filename) /* Is module -- check for .pyc/.pyo too */ ismodule(char *filename) /* Is module -- check for .pyc/.pyo too */
{ {
if (isfile(filename)) if (isfile(filename))
return 1; return 1;
@ -165,7 +165,7 @@ ismodule(char *filename) /* Is module -- check for .pyc/.pyo too */
static int static int
isxfile(char *filename) /* Is executable file */ isxfile(char *filename) /* Is executable file */
{ {
struct stat buf; struct stat buf;
if (stat(filename, &buf) != 0) if (stat(filename, &buf) != 0)
@ -179,7 +179,7 @@ isxfile(char *filename) /* Is executable file */
static int static int
isdir(char *filename) /* Is directory */ isdir(char *filename) /* Is directory */
{ {
struct stat buf; struct stat buf;
if (stat(filename, &buf) != 0) if (stat(filename, &buf) != 0)
@ -213,28 +213,33 @@ joinpath(char *buffer, char *stuff)
buffer[n+k] = '\0'; buffer[n+k] = '\0';
} }
/* init_path_from_argv0 requires that path be allocated at least /* copy_absolute requires that path be allocated at least
MAXPATHLEN + 1 bytes and that argv0_path be no more than MAXPATHLEN MAXPATHLEN + 1 bytes and that p be no more than MAXPATHLEN bytes. */
bytes.
*/
static void static void
init_path_from_argv0(char *path, char *argv0_path) copy_absolute(char *path, char *p)
{ {
if (argv0_path[0] == '/') if (p[0] == SEP)
strcpy(path, argv0_path); strcpy(path, p);
else if (argv0_path[0] == '.') {
getcwd(path, MAXPATHLEN);
if (argv0_path[1] == '/')
joinpath(path, argv0_path + 2);
else
joinpath(path, argv0_path);
}
else { else {
getcwd(path, MAXPATHLEN); getcwd(path, MAXPATHLEN);
joinpath(path, argv0_path); if (p[0] == '.' && p[1] == SEP)
p += 2;
joinpath(path, p);
} }
} }
/* absolutize() requires that path be allocated at least MAXPATHLEN+1 bytes. */
static void
absolutize(char *path)
{
char buffer[MAXPATHLEN + 1];
if (path[0] == SEP)
return;
copy_absolute(buffer, path);
strcpy(path, buffer);
}
/* search_for_prefix requires that argv0_path be no more than MAXPATHLEN /* search_for_prefix requires that argv0_path be no more than MAXPATHLEN
bytes long. bytes long.
*/ */
@ -271,7 +276,7 @@ search_for_prefix(char *argv0_path, char *home)
} }
/* Search from argv0_path, until root is found */ /* Search from argv0_path, until root is found */
init_path_from_argv0(prefix, argv0_path); copy_absolute(prefix, argv0_path);
do { do {
n = strlen(prefix); n = strlen(prefix);
joinpath(prefix, lib_python); joinpath(prefix, lib_python);
@ -324,7 +329,7 @@ search_for_exec_prefix(char *argv0_path, char *home)
} }
/* Search from argv0_path, until root is found */ /* Search from argv0_path, until root is found */
init_path_from_argv0(exec_prefix, argv0_path); copy_absolute(exec_prefix, argv0_path);
do { do {
n = strlen(exec_prefix); n = strlen(exec_prefix);
joinpath(exec_prefix, lib_python); joinpath(exec_prefix, lib_python);
@ -387,13 +392,13 @@ calculate_path(void)
joinpath(argv0_path, lib_python); joinpath(argv0_path, lib_python);
joinpath(argv0_path, LANDMARK); joinpath(argv0_path, LANDMARK);
if (!ismodule(argv0_path)) { if (!ismodule(argv0_path)) {
/* We are in the build directory so use the name of the /* We are in the build directory so use the name of the
executable - we know that the absolute path is passed */ executable - we know that the absolute path is passed */
strncpy(progpath, prog, MAXPATHLEN); strncpy(progpath, prog, MAXPATHLEN);
} }
else { else {
/* Use the location of the library as the progpath */ /* Use the location of the library as the progpath */
strncpy(progpath, buf, MAXPATHLEN); strncpy(progpath, buf, MAXPATHLEN);
} }
} }
else { else {
@ -401,28 +406,26 @@ calculate_path(void)
(even though NSNameOfModule() probably does the same thing.) */ (even though NSNameOfModule() probably does the same thing.) */
#endif #endif
/* If there is no slash in the argv0 path, then we have to /* 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 * assume python is on the user's $PATH, since there's no
* other way to find a directory to start the search from. If * other way to find a directory to start the search from. If
* $PATH isn't exported, you lose. * $PATH isn't exported, you lose.
*/ */
if (strchr(prog, SEP)) if (strchr(prog, SEP))
strncpy(progpath, prog, MAXPATHLEN); strncpy(progpath, prog, MAXPATHLEN);
else if (path) { else if (path) {
int bufspace = MAXPATHLEN;
while (1) { while (1) {
char *delim = strchr(path, DELIM); char *delim = strchr(path, DELIM);
if (delim) { if (delim) {
size_t len = delim - path; size_t len = delim - path;
if (len > bufspace) if (len > MAXPATHLEN)
len = bufspace; len = MAXPATHLEN;
strncpy(progpath, path, len); strncpy(progpath, path, len);
*(progpath + len) = '\0'; *(progpath + len) = '\0';
bufspace -= len;
} }
else else
strncpy(progpath, path, bufspace); strncpy(progpath, path, MAXPATHLEN);
joinpath(progpath, prog); joinpath(progpath, prog);
if (isxfile(progpath)) if (isxfile(progpath))
@ -434,9 +437,11 @@ calculate_path(void)
} }
path = delim + 1; path = delim + 1;
} }
} }
else else
progpath[0] = '\0'; progpath[0] = '\0';
if (progpath[0] != SEP)
absolutize(progpath);
#ifdef WITH_NEXT_FRAMEWORK #ifdef WITH_NEXT_FRAMEWORK
} }
#endif #endif
@ -451,8 +456,8 @@ calculate_path(void)
/* It's not null terminated! */ /* It's not null terminated! */
tmpbuffer[linklen] = '\0'; tmpbuffer[linklen] = '\0';
if (tmpbuffer[0] == SEP) if (tmpbuffer[0] == SEP)
/* tmpbuffer should never be longer than MAXPATHLEN, /* tmpbuffer should never be longer than MAXPATHLEN,
but extra check does not hurt */ but extra check does not hurt */
strncpy(argv0_path, tmpbuffer, MAXPATHLEN); strncpy(argv0_path, tmpbuffer, MAXPATHLEN);
else { else {
/* Interpret relative to progpath */ /* Interpret relative to progpath */
@ -472,7 +477,7 @@ calculate_path(void)
if (!(pfound = search_for_prefix(argv0_path, home))) { if (!(pfound = search_for_prefix(argv0_path, home))) {
if (!Py_FrozenFlag) if (!Py_FrozenFlag)
fprintf(stderr, fprintf(stderr,
"Could not find platform independent libraries <prefix>\n"); "Could not find platform independent libraries <prefix>\n");
strncpy(prefix, PREFIX, MAXPATHLEN); strncpy(prefix, PREFIX, MAXPATHLEN);
joinpath(prefix, lib_python); joinpath(prefix, lib_python);
} }
@ -482,7 +487,7 @@ calculate_path(void)
if (!(efound = search_for_exec_prefix(argv0_path, home))) { if (!(efound = search_for_exec_prefix(argv0_path, home))) {
if (!Py_FrozenFlag) if (!Py_FrozenFlag)
fprintf(stderr, fprintf(stderr,
"Could not find platform dependent libraries <exec_prefix>\n"); "Could not find platform dependent libraries <exec_prefix>\n");
strncpy(exec_prefix, EXEC_PREFIX, MAXPATHLEN); strncpy(exec_prefix, EXEC_PREFIX, MAXPATHLEN);
joinpath(exec_prefix, "lib/lib-dynload"); joinpath(exec_prefix, "lib/lib-dynload");
} }