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
1 changed files with 58 additions and 53 deletions

View File

@ -213,28 +213,33 @@ joinpath(char *buffer, char *stuff)
buffer[n+k] = '\0';
}
/* init_path_from_argv0 requires that path be allocated at least
MAXPATHLEN + 1 bytes and that argv0_path be no more than MAXPATHLEN
bytes.
*/
/* copy_absolute requires that path be allocated at least
MAXPATHLEN + 1 bytes and that p be no more than MAXPATHLEN bytes. */
static void
init_path_from_argv0(char *path, char *argv0_path)
copy_absolute(char *path, char *p)
{
if (argv0_path[0] == '/')
strcpy(path, argv0_path);
else if (argv0_path[0] == '.') {
getcwd(path, MAXPATHLEN);
if (argv0_path[1] == '/')
joinpath(path, argv0_path + 2);
else
joinpath(path, argv0_path);
}
if (p[0] == SEP)
strcpy(path, p);
else {
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
bytes long.
*/
@ -271,7 +276,7 @@ search_for_prefix(char *argv0_path, char *home)
}
/* Search from argv0_path, until root is found */
init_path_from_argv0(prefix, argv0_path);
copy_absolute(prefix, argv0_path);
do {
n = strlen(prefix);
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 */
init_path_from_argv0(exec_prefix, argv0_path);
copy_absolute(exec_prefix, argv0_path);
do {
n = strlen(exec_prefix);
joinpath(exec_prefix, lib_python);
@ -409,20 +414,18 @@ calculate_path(void)
if (strchr(prog, SEP))
strncpy(progpath, prog, MAXPATHLEN);
else if (path) {
int bufspace = MAXPATHLEN;
while (1) {
char *delim = strchr(path, DELIM);
if (delim) {
size_t len = delim - path;
if (len > bufspace)
len = bufspace;
if (len > MAXPATHLEN)
len = MAXPATHLEN;
strncpy(progpath, path, len);
*(progpath + len) = '\0';
bufspace -= len;
}
else
strncpy(progpath, path, bufspace);
strncpy(progpath, path, MAXPATHLEN);
joinpath(progpath, prog);
if (isxfile(progpath))
@ -437,6 +440,8 @@ calculate_path(void)
}
else
progpath[0] = '\0';
if (progpath[0] != SEP)
absolutize(progpath);
#ifdef WITH_NEXT_FRAMEWORK
}
#endif