Back to index

python3.2  3.2.2
Defines | Functions | Variables
getpathp.c File Reference
#include "Python.h"
#include "osdefs.h"
#include <os2.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

Go to the source code of this file.

Defines

#define INCL_DOS
#define LANDMARK   "lib\\os.py"

Functions

static int is_sep (char ch)
static void reduce (char *dir)
static int exists (char *filename)
static int ismodule (char *filename)
static void join (char *buffer, char *stuff)
static int gotlandmark (char *landmark)
static int search_for_prefix (char *argv0_path, char *landmark)
static void get_progpath (void)
static void calculate_path (void)
char * Py_GetPath (void)
char * Py_GetPrefix (void)
char * Py_GetExecPrefix (void)
char * Py_GetProgramFullPath (void)

Variables

static char prefix [MAXPATHLEN+1]
static char progpath [MAXPATHLEN+1]
static char * module_search_path = NULL

Define Documentation

#define INCL_DOS

Definition at line 54 of file getpathp.c.

#define LANDMARK   "lib\\os.py"

Definition at line 78 of file getpathp.c.


Function Documentation

static void calculate_path ( void  ) [static]

Definition at line 259 of file getpathp.c.

{
    char argv0_path[MAXPATHLEN+1];
    char *buf;
    size_t bufsz;
    char *pythonhome = Py_GetPythonHome();
    char *envpath = getenv("PYTHONPATH");
    char zip_path[MAXPATHLEN+1];
    size_t len;

    get_progpath();
    /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
    strcpy(argv0_path, progpath);
    reduce(argv0_path);
    if (pythonhome == NULL || *pythonhome == '\0') {
        if (search_for_prefix(argv0_path, LANDMARK))
            pythonhome = prefix;
        else
            pythonhome = NULL;
    }
    else
        strncpy(prefix, pythonhome, MAXPATHLEN);

    if (envpath && *envpath == '\0')
        envpath = NULL;

    /* Calculate zip archive path */
    strncpy(zip_path, progpath, MAXPATHLEN);
    zip_path[MAXPATHLEN] = '\0';
    len = strlen(zip_path);
    if (len > 4) {
        zip_path[len-3] = 'z';  /* change ending to "zip" */
        zip_path[len-2] = 'i';
        zip_path[len-1] = 'p';
    }
    else {
        zip_path[0] = 0;
    }

    /* We need to construct a path from the following parts.
     * (1) the PYTHONPATH environment variable, if set;
     * (2) the zip archive file path;
     * (3) the PYTHONPATH config macro, with the leading "."
     *     of each component replaced with pythonhome, if set;
     * (4) the directory containing the executable (argv0_path).
     * The length calculation calculates #3 first.
     */

    /* Calculate size of return buffer */
    if (pythonhome != NULL) {
        char *p;
        bufsz = 1;
        for (p = PYTHONPATH; *p; p++) {
            if (*p == DELIM)
                bufsz++; /* number of DELIM plus one */
        }
        bufsz *= strlen(pythonhome);
    }
    else
        bufsz = 0;
    bufsz += strlen(PYTHONPATH) + 1;
    bufsz += strlen(argv0_path) + 1;
    bufsz += strlen(zip_path) + 1;
    if (envpath != NULL)
        bufsz += strlen(envpath) + 1;

    module_search_path = buf = malloc(bufsz);
    if (buf == NULL) {
        /* We can't exit, so print a warning and limp along */
        fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
        if (envpath) {
            fprintf(stderr, "Using environment $PYTHONPATH.\n");
            module_search_path = envpath;
        }
        else {
            fprintf(stderr, "Using default static path.\n");
            module_search_path = PYTHONPATH;
        }
        return;
    }

    if (envpath) {
        strcpy(buf, envpath);
        buf = strchr(buf, '\0');
        *buf++ = DELIM;
    }
    if (zip_path[0]) {
        strcpy(buf, zip_path);
        buf = strchr(buf, '\0');
        *buf++ = DELIM;
    }

    if (pythonhome == NULL) {
        strcpy(buf, PYTHONPATH);
        buf = strchr(buf, '\0');
    }
    else {
        char *p = PYTHONPATH;
        char *q;
        size_t n;
        for (;;) {
            q = strchr(p, DELIM);
            if (q == NULL)
                n = strlen(p);
            else
                n = q-p;
            if (p[0] == '.' && is_sep(p[1])) {
                strcpy(buf, pythonhome);
                buf = strchr(buf, '\0');
                p++;
                n--;
            }
            strncpy(buf, p, n);
            buf += n;
            if (q == NULL)
                break;
            *buf++ = DELIM;
            p = q+1;
        }
    }
    if (argv0_path) {
        *buf++ = DELIM;
        strcpy(buf, argv0_path);
        buf = strchr(buf, '\0');
    }
    *buf = '\0';
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int exists ( char *  filename) [static]

Definition at line 110 of file getpathp.c.

{
    struct stat buf;
    return stat(filename, &buf) == 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void get_progpath ( void  ) [static]

Definition at line 199 of file getpathp.c.

{
    extern char *Py_GetProgramName(void);
    char *path = getenv("PATH");
    char *prog = Py_GetProgramName();

    PPIB pib;
    if ((DosGetInfoBlocks(NULL, &pib) == 0) &&
        (DosQueryModuleName(pib->pib_hmte, sizeof(progpath), progpath) == 0))
        return;

    if (prog == NULL || *prog == '\0')
        prog = "python";

    /* 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.
     */
#ifdef ALTSEP
    if (strchr(prog, SEP) || strchr(prog, ALTSEP))
#else
    if (strchr(prog, SEP))
#endif
        strncpy(progpath, prog, MAXPATHLEN);
    else if (path) {
        while (1) {
            char *delim = strchr(path, DELIM);

            if (delim) {
                size_t len = delim - path;
                /* ensure we can't overwrite buffer */
#if !defined(PYCC_GCC)
                len = min(MAXPATHLEN,len);
#else
                len = MAXPATHLEN < len ? MAXPATHLEN : len;
#endif
                strncpy(progpath, path, len);
                *(progpath + len) = '\0';
            }
            else
                strncpy(progpath, path, MAXPATHLEN);

            /* join() is safe for MAXPATHLEN+1 size buffer */
            join(progpath, prog);
            if (exists(progpath))
                break;

            if (!delim) {
                progpath[0] = '\0';
                break;
            }
            path = delim + 1;
        }
    }
    else
        progpath[0] = '\0';
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int gotlandmark ( char *  landmark) [static]

Definition at line 170 of file getpathp.c.

{
    int n, ok;

    n = strlen(prefix);
    join(prefix, landmark);
    ok = ismodule(prefix);
    prefix[n] = '\0';
    return ok;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int is_sep ( char  ch) [static]

Definition at line 88 of file getpathp.c.

{
#ifdef ALTSEP
    return ch == SEP || ch == ALTSEP;
#else
    return ch == SEP;
#endif
}

Here is the caller graph for this function:

static int ismodule ( char *  filename) [static]

Definition at line 121 of file getpathp.c.

{
    if (exists(filename))
        return 1;

    /* Check for the compiled version of prefix. */
    if (strlen(filename) < MAXPATHLEN) {
        strcat(filename, Py_OptimizeFlag ? "o" : "c");
        if (exists(filename))
            return 1;
    }
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void join ( char *  buffer,
char *  stuff 
) [static]

Definition at line 146 of file getpathp.c.

{
    size_t n, k;
    if (is_sep(stuff[0]))
        n = 0;
    else {
        n = strlen(buffer);
        if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
            buffer[n++] = SEP;
    }
    if (n > MAXPATHLEN)
        Py_FatalError("buffer overflow in getpathp.c's joinpath()");
    k = strlen(stuff);
    if (n + k > MAXPATHLEN)
        k = MAXPATHLEN - n;
    strncpy(buffer+n, stuff, k);
    buffer[n+k] = '\0';
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* Py_GetExecPrefix ( void  )

Definition at line 407 of file getpathp.c.

{
    return Py_GetPrefix();
}

Here is the call graph for this function:

char* Py_GetPath ( void  )

Definition at line 391 of file getpathp.c.

Here is the call graph for this function:

char* Py_GetPrefix ( void  )

Definition at line 399 of file getpathp.c.

Here is the call graph for this function:

Definition at line 413 of file getpathp.c.

Here is the call graph for this function:

static void reduce ( char *  dir) [static]

Definition at line 101 of file getpathp.c.

{
    size_t i = strlen(dir);
    while (i > 0 && !is_sep(dir[i]))
        --i;
    dir[i] = '\0';
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int search_for_prefix ( char *  argv0_path,
char *  landmark 
) [static]

Definition at line 185 of file getpathp.c.

{
    /* Search from argv0_path, until landmark is found */
    strcpy(prefix, argv0_path);
    do {
        if (gotlandmark(landmark))
            return 1;
        reduce(prefix);
    } while (prefix[0]);
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

char* module_search_path = NULL [static]

Definition at line 84 of file getpathp.c.

char prefix[MAXPATHLEN+1] [static]

Definition at line 82 of file getpathp.c.

char progpath[MAXPATHLEN+1] [static]

Definition at line 83 of file getpathp.c.