home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Modules / getpath.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-25  |  20.2 KB  |  747 lines

  1.  
  2. /* Return the initial module search path. */
  3.  
  4. #include "Python.h"
  5. #include "osdefs.h"
  6. #include "protos.h"
  7.  
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <string.h>
  11.  
  12. #ifdef _AMIGA
  13. #include <proto/dos.h>
  14. #endif
  15.  
  16. #if HAVE_UNISTD_H
  17. #include <unistd.h>
  18. #endif /* HAVE_UNISTD_H */
  19.  
  20. #ifdef WITH_NEXT_FRAMEWORK
  21. #include <mach-o/dyld.h>
  22. #endif
  23.  
  24. static int isxfile ( char *filename );    // FWD
  25.  
  26.  
  27. /* Search in some common locations for the associated Python libraries.
  28.  *
  29.  * Two directories must be found, the platform independent directory
  30.  * (prefix), containing the common .py and .pyc files, and the platform
  31.  * dependent directory (exec_prefix), containing the shared library
  32.  * modules.  Note that prefix and exec_prefix can be the same directory,
  33.  * but for some installations, they are different.
  34.  *
  35.  * Py_GetPath() carries out separate searches for prefix and exec_prefix.
  36.  * Each search tries a number of different locations until a ``landmark''
  37.  * file or directory is found.  If no prefix or exec_prefix is found, a
  38.  * warning message is issued and the preprocessor defined PREFIX and
  39.  * EXEC_PREFIX are used (even though they will not work); python carries on
  40.  * as best as is possible, but most imports will fail.
  41.  *
  42.  * Before any searches are done, the location of the executable is
  43.  * determined.  If argv[0] has one or more slashs in it, it is used
  44.  * unchanged.  Otherwise, it must have been invoked from the shell's path,
  45.  * so we search $PATH for the named executable and use that.  If the
  46.  * executable was not found on $PATH (or there was no $PATH environment
  47.  * variable), the original argv[0] string is used.
  48.  *
  49.  * Next, the executable location is examined to see if it is a symbolic
  50.  * link.  If so, the link is chased (correctly interpreting a relative
  51.  * pathname if one is found) and the directory of the link target is used.
  52.  *
  53.  * Finally, argv0_path is set to the directory containing the executable
  54.  * (i.e. the last component is stripped).
  55.  *
  56.  * With argv0_path in hand, we perform a number of steps.  The same steps
  57.  * are performed for prefix and for exec_prefix, but with a different
  58.  * landmark.
  59.  *
  60.  * Step 1. Are we running python out of the build directory?  This is
  61.  * checked by looking for a different kind of landmark relative to
  62.  * argv0_path.  For prefix, the landmark's path is derived from the VPATH
  63.  * preprocessor variable (taking into account that its value is almost, but
  64.  * not quite, what we need).  For exec_prefix, the landmark is
  65.  * Modules/Setup.  If the landmark is found, we're done.
  66.  *
  67.  * For the remaining steps, the prefix landmark will always be
  68.  * lib/python$VERSION/os.py and the exec_prefix will always be
  69.  * lib/python$VERSION/lib-dynload, where $VERSION is Python's version
  70.  * number as supplied by the Makefile.  Note that this means that no more
  71.  * build directory checking is performed; if the first step did not find
  72.  * the landmarks, the assumption is that python is running from an
  73.  * installed setup.
  74.  *
  75.  * Step 2. See if the $PYTHONHOME environment variable points to the
  76.  * installed location of the Python libraries.  If $PYTHONHOME is set, then
  77.  * it points to prefix and exec_prefix.  $PYTHONHOME can be a single
  78.  * directory, which is used for both, or the prefix and exec_prefix
  79.  * directories separated by a colon.
  80.  *
  81.  * Step 3. Try to find prefix and exec_prefix relative to argv0_path,
  82.  * backtracking up the path until it is exhausted.  This is the most common
  83.  * step to succeed.  Note that if prefix and exec_prefix are different,
  84.  * exec_prefix is more likely to be found; however if exec_prefix is a
  85.  * subdirectory of prefix, both will be found.
  86.  *
  87.  * Step 4. Search the directories pointed to by the preprocessor variables
  88.  * PREFIX and EXEC_PREFIX.  These are supplied by the Makefile but can be
  89.  * passed in as options to the configure script.
  90.  *
  91.  * That's it!
  92.  *
  93.  * Well, almost.  Once we have determined prefix and exec_prefix, the
  94.  * preprocessor variable PYTHONPATH is used to construct a path.  Each
  95.  * relative path on PYTHONPATH is prefixed with prefix.  Then the directory
  96.  * containing the shared library modules is appended.  The environment
  97.  * variable $PYTHONPATH is inserted in front of it all.  Finally, the
  98.  * prefix and exec_prefix globals are tweaked so they reflect the values
  99.  * expected by other code, by stripping the "lib/python$VERSION/..." stuff
  100.  * off.  If either points to the build directory, the globals are reset to
  101.  * the corresponding preprocessor variables (so sys.prefix will reflect the
  102.  * installation location, even though sys.path points into the build
  103.  * directory).  This seems to make more sense given that currently the only
  104.  * known use of sys.prefix and sys.exec_prefix is for the ILU installation
  105.  * process to find the installed Python tree.
  106.  */
  107.  
  108. #ifndef VERSION
  109. #define VERSION "2.0"
  110. #endif
  111.  
  112. #ifndef VPATH
  113. #define VPATH "."
  114. #endif
  115.  
  116. #ifndef PREFIX
  117. #define PREFIX "/usr/local"
  118. #endif
  119.  
  120. #ifndef EXEC_PREFIX
  121. #define EXEC_PREFIX PREFIX
  122. #endif
  123.  
  124. #ifndef PYTHONPATH
  125. #ifdef _AMIGA
  126. #define PYTHONPATH PREFIX "lib/python" VERSION ";" EXEC_PREFIX "lib/python" VERSION "/lib-dynload"
  127. #else /* !_AMIGA */
  128. /* I know this isn't K&R C, but the Makefile specifies it anyway */
  129. #define PYTHONPATH PREFIX "/lib/python" VERSION ":" \
  130.           EXEC_PREFIX "/lib/python" VERSION "/lib-dynload"
  131. #endif
  132. #endif
  133.  
  134. #ifndef LANDMARK
  135. #define LANDMARK "os.py"
  136. #endif
  137.  
  138. static char prefix[MAXPATHLEN+1];
  139. static char exec_prefix[MAXPATHLEN+1];
  140. static char progpath[MAXPATHLEN+1];
  141. static char *module_search_path = NULL;
  142. static char lib_python[] = "lib/python" VERSION;
  143.  
  144.  
  145. #ifdef _AMIGA
  146. /* determine full program path */
  147. extern void Py_GetArgcArgv Py_PROTO((int *argc, char ***argv));    /* in main.c */
  148.  
  149. static const char *fullprogpath(void)
  150. {
  151.     static char path[MAXPATHLEN*2];
  152.     static char prog[MAXPATHLEN];
  153.  
  154.     BPTR dir;
  155.  
  156.     extern BOOL from_WB;    /* in main.c */
  157.  
  158.     // If the program name contains ':' or '/' it's not in the user's path
  159.     // and probably set by using Py_SetProgramName. In that case, just
  160.     // use this. If it exists!
  161.     strcpy(path,Py_GetProgramName());
  162.     if(strchr(path,':') || strchr(path,'/'))
  163.     {
  164.         if(!isxfile(path))
  165.         {
  166.             // Error; the specified file does not exist or is no exe
  167.             path[0]='\0';
  168.         }
  169.         return path;
  170.     }
  171.  
  172.     // Construct the full path of our executable program.
  173.     if(from_WB)
  174.     {
  175.         /* We're launced from WB, GetProgramName() won't work */
  176.         /* Use WB's argv[0] as the executable path */
  177.         int argc;
  178.         char **argv;
  179.         Py_GetArgcArgv(&argc, &argv);
  180.         if(argc>0)
  181.             strcpy(path,argv[0]);
  182.         else
  183.             strcpy(path,"!error!");
  184.     }
  185.     else
  186.     {
  187.         /* Launced from CLI, use GetProgramName */
  188.  
  189.         /* However, first check if the specified name exists */
  190.         if(!isxfile(path))
  191.         {
  192.             path[0]='\0';
  193.             return path;
  194.         }
  195.  
  196.         path[0]=0;
  197.         if(dir=GetProgramDir())
  198.         {
  199.             (void)NameFromLock(dir,path,MAXPATHLEN);
  200.             if(!GetProgramName(prog,MAXPATHLEN))    // this is a dos.library function!
  201.                 strcpy(prog,"!error!");
  202.             if(!AddPart(path,prog,MAXPATHLEN*2))
  203.                 strcpy(path,"!error!");
  204.         }    
  205.     }
  206.     return path;
  207. }
  208.  
  209. static void reduce( char *dir)
  210. {
  211.     int i = strlen(dir);
  212.     if(i>0 && dir[i-1]==':') dir[--i]=0;
  213.     while (i > 0 && dir[i] != SEP && dir[i] != ':') --i;
  214.     if(dir[i]!=':') dir[i] = '\0';
  215.     else dir[i+1]='\0';
  216. }
  217.  
  218. #else /*! AMIGA */
  219.  
  220. static void
  221. reduce(char *dir)
  222. {
  223.     size_t i = strlen(dir);
  224.     while (i > 0 && dir[i] != SEP)
  225.         --i;
  226.     dir[i] = '\0';
  227. }
  228.  
  229. #endif /* _AMIGA */
  230.  
  231. #ifndef S_ISREG
  232. #define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
  233. #endif
  234.  
  235. #ifndef S_ISDIR
  236. #define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
  237. #endif
  238.  
  239. static int
  240. isfile(char *filename)        /* Is file, not directory */
  241. {
  242.     struct stat buf;
  243.     if (stat(filename, &buf) != 0)
  244.         return 0;
  245.     if (!S_ISREG(buf.st_mode))
  246.         return 0;
  247.     return 1;
  248. }
  249.  
  250.  
  251. static int
  252. ismodule(char *filename)    /* Is module -- check for .pyc/.pyo too */
  253. {
  254.     if (isfile(filename))
  255.         return 1;
  256.  
  257.     /* Check for the compiled version of prefix. */
  258.     if (strlen(filename) < MAXPATHLEN) {
  259.         strcat(filename, Py_OptimizeFlag ? "o" : "c");
  260.         if (isfile(filename))
  261.             return 1;
  262.     }
  263.     return 0;
  264. }
  265.  
  266.  
  267. static int
  268. isxfile(char *filename)        /* Is executable file */
  269. {
  270.     struct stat buf;
  271.     if (stat(filename, &buf) != 0)
  272.         return 0;
  273.     if (!S_ISREG(buf.st_mode))
  274.         return 0;
  275.     if ((buf.st_mode & 0111) == 0)
  276.         return 0;
  277.     return 1;
  278. }
  279.  
  280.  
  281. static int
  282. isdir(char *filename)            /* Is directory */
  283. {
  284.     struct stat buf;
  285.     if (stat(filename, &buf) != 0)
  286.         return 0;
  287.     if (!S_ISDIR(buf.st_mode))
  288.         return 0;
  289.     return 1;
  290. }
  291.  
  292.  
  293. #ifdef _AMIGA
  294. #define joinpath(buffer,stuff) AddPart(buffer,stuff,MAXPATHLEN)
  295. #else
  296. /* joinpath requires that any buffer argument passed to it has at
  297.    least MAXPATHLEN + 1 bytes allocated.  If this requirement is met,
  298.    it guarantees that it will never overflow the buffer.  If stuff
  299.    is too long, buffer will contain a truncated copy of stuff.
  300. */
  301. static void
  302. joinpath(char *buffer, char *stuff)
  303. {
  304.     size_t n, k;
  305.     if (stuff[0] == SEP)
  306.         n = 0;
  307.     else {
  308.         n = strlen(buffer);
  309.         if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN)
  310.             buffer[n++] = SEP;
  311.     }
  312.     k = strlen(stuff);
  313.     if (n + k > MAXPATHLEN)
  314.         k = MAXPATHLEN - n;
  315.     strncpy(buffer+n, stuff, k);
  316.     buffer[n+k] = '\0';
  317. }
  318. #endif
  319.  
  320.  
  321. /* init_path_from_argv0 requirs that path be allocated at least
  322.    MAXPATHLEN + 1 bytes and that argv0_path be no more than MAXPATHLEN
  323.    bytes. 
  324. */
  325. static void
  326. init_path_from_argv0(char *path, char *argv0_path)
  327. {
  328.     if (argv0_path[0] == '/')
  329.     strcpy(path, argv0_path);
  330.     else if (argv0_path[0] == '.') {
  331.     getcwd(path, MAXPATHLEN);
  332.     if (argv0_path[1] == '/') 
  333.         joinpath(path, argv0_path + 2);
  334.     else
  335.         joinpath(path, argv0_path);
  336.     }
  337.     else {
  338.     getcwd(path, MAXPATHLEN);
  339.     joinpath(path, argv0_path);
  340.     }
  341. }
  342.  
  343. /* search_for_prefix requires that argv0_path be no more than MAXPATHLEN 
  344.    bytes long.
  345. */
  346. static int
  347. search_for_prefix(char *argv0_path, char *home)
  348. {
  349.     size_t n;
  350.     char *vpath;
  351.  
  352.     /* If PYTHONHOME is set, we believe it unconditionally */
  353.     if (home) {
  354.         char *delim;
  355.         strncpy(prefix, home, MAXPATHLEN);
  356.         delim = strchr(prefix, DELIM);
  357.         if (delim)
  358.             *delim = '\0';
  359.         joinpath(prefix, lib_python);
  360.         joinpath(prefix, LANDMARK);
  361.         return 1;
  362.     }
  363.  
  364.     /* Check to see if argv[0] is in the build directory */
  365.     strcpy(prefix, argv0_path);
  366.     joinpath(prefix, "Modules/Setup");
  367.     if (isfile(prefix)) {
  368.         /* Check VPATH to see if argv0_path is in the build directory.
  369.          * Complication: the VPATH passed in is relative to the
  370.          * Modules build directory and points to the Modules source
  371.          * directory; we need it relative to the build tree and
  372.          * pointing to the source tree.  Solution: chop off a leading
  373.          * ".." (but only if it's there -- it could be an absolute
  374.          * path) and chop off the final component (assuming it's
  375.          * "Modules").
  376.          */
  377.         vpath = VPATH;
  378.         if (vpath[0] == '.' && vpath[1] == '.' && vpath[2] == '/')
  379.             vpath += 3;
  380.         strcpy(prefix, argv0_path);
  381.         joinpath(prefix, vpath);
  382.         reduce(prefix);
  383.         joinpath(prefix, "Lib");
  384.         joinpath(prefix, LANDMARK);
  385.         if (ismodule(prefix))
  386.             return -1;
  387.     }
  388.  
  389.     /* Search from argv0_path, until root is found */
  390.     init_path_from_argv0(prefix, argv0_path);
  391.     do {
  392.         n = strlen(prefix);
  393.         joinpath(prefix, lib_python);
  394.         joinpath(prefix, LANDMARK);
  395.         if (ismodule(prefix))
  396.             return 1;
  397.         prefix[n] = '\0';
  398.         reduce(prefix);
  399.     } while (prefix[0]);
  400.  
  401.     /* Look at configure's PREFIX */
  402.     strncpy(prefix, PREFIX, MAXPATHLEN);
  403.     joinpath(prefix, lib_python);
  404.     joinpath(prefix, LANDMARK);
  405.     if (ismodule(prefix))
  406.         return 1;
  407.  
  408.     /* Fail */
  409.     return 0;
  410. }
  411.  
  412.  
  413. /* search_for_exec_prefix requires that argv0_path be no more than
  414.    MAXPATHLEN bytes long.  
  415. */
  416. static int
  417. search_for_exec_prefix(char *argv0_path, char *home)
  418. {
  419.     size_t n;
  420.  
  421.     /* If PYTHONHOME is set, we believe it unconditionally */
  422.     if (home) {
  423.         char *delim;
  424.         delim = strchr(home, DELIM);
  425.         if (delim)
  426.             strncpy(exec_prefix, delim+1, MAXPATHLEN);
  427.         else
  428.             strncpy(exec_prefix, home, MAXPATHLEN);
  429.         joinpath(exec_prefix, lib_python);
  430.         joinpath(exec_prefix, "lib-dynload");
  431.         return 1;
  432.     }
  433.  
  434.     /* Check to see if argv[0] is in the build directory */
  435.     strcpy(exec_prefix, argv0_path);
  436.     joinpath(exec_prefix, "Modules/Setup");
  437.     if (isfile(exec_prefix)) {
  438.         reduce(exec_prefix);
  439.         return -1;
  440.     }
  441.  
  442.     /* Search from argv0_path, until root is found */
  443.     init_path_from_argv0(exec_prefix, argv0_path);
  444.     do {
  445.         n = strlen(exec_prefix);
  446.         joinpath(exec_prefix, lib_python);
  447.         joinpath(exec_prefix, "lib-dynload");
  448.         if (isdir(exec_prefix))
  449.             return 1;
  450.         exec_prefix[n] = '\0';
  451.         reduce(exec_prefix);
  452.     } while (exec_prefix[0]);
  453.  
  454.     /* Look at configure's EXEC_PREFIX */
  455.     strncpy(exec_prefix, EXEC_PREFIX, MAXPATHLEN);
  456.     joinpath(exec_prefix, lib_python);
  457.     joinpath(exec_prefix, "lib-dynload");
  458.     if (isdir(exec_prefix))
  459.         return 1;
  460.  
  461.     /* Fail */
  462.     return 0;
  463. }
  464.  
  465.  
  466. static void
  467. calculate_path(void)
  468. {
  469.     extern char *Py_GetProgramName(void);
  470.  
  471.     static char delimiter[2] = {DELIM, '\0'};
  472.     static char separator[2] = {SEP, '\0'};
  473.     char *pythonpath = PYTHONPATH;
  474.     char *rtpypath = getenv("PYTHONPATH");
  475.     char *home = Py_GetPythonHome();
  476. #ifndef _AMIGA
  477.     char *path = getenv("PATH");
  478.     char *prog = Py_GetProgramName();
  479. #endif
  480.     char argv0_path[MAXPATHLEN+1];
  481.     int pfound, efound; /* 1 if found; -1 if found build directory */
  482.     char *buf;
  483.     size_t bufsz;
  484.     size_t prefixsz;
  485.     char *defpath = pythonpath;
  486. #ifdef WITH_NEXT_FRAMEWORK
  487.     NSModule pythonModule;
  488. #endif
  489.     
  490. #ifdef WITH_NEXT_FRAMEWORK
  491.     /* XXX Need to check this code for buffer overflows */
  492.     pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize"));
  493.     /* Use dylib functions to find out where the framework was loaded from */
  494.     buf = NSLibraryNameForModule(pythonModule);
  495.     if (buf != NULL) {
  496.         /* We're in a framework. */
  497.         strcpy(progpath, buf);
  498.  
  499.         /* Frameworks have support for versioning */
  500.         strcpy(lib_python, "lib");
  501.     }
  502.     else {
  503.         /* If we're not in a framework, fall back to the old way
  504.            (even though NSNameOfModule() probably does the same thing.) */
  505. #endif
  506.     
  507. #ifdef _AMIGA
  508.     strcpy(progpath,fullprogpath());
  509. #else /* !_AMIGA */
  510.     /* If there is no slash in the argv0 path, then we have to
  511.      * assume python is on the user's $PATH, since there's no
  512.      * other way to find a directory to start the search from.  If
  513.      * $PATH isn't exported, you lose.
  514.      */
  515.     if (strchr(prog, SEP))
  516.             strncpy(progpath, prog, MAXPATHLEN);
  517.     else if (path) {
  518.         int bufspace = MAXPATHLEN;
  519.             while (1) {
  520.                 char *delim = strchr(path, DELIM);
  521.  
  522.                 if (delim) {
  523.                     size_t len = delim - path;
  524.             if (len > bufspace)
  525.             len = bufspace;
  526.                     strncpy(progpath, path, len);
  527.                     *(progpath + len) = '\0';
  528.             bufspace -= len;
  529.                 }
  530.                 else
  531.                     strncpy(progpath, path, bufspace);
  532.  
  533.                 joinpath(progpath, prog);
  534.                 if (isxfile(progpath))
  535.                     break;
  536.  
  537.                 if (!delim) {
  538.                     progpath[0] = '\0';
  539.                     break;
  540.                 }
  541.                 path = delim + 1;
  542.             }
  543.     }
  544.     else
  545.             progpath[0] = '\0';
  546. #ifdef WITH_NEXT_FRAMEWORK
  547.     }
  548. #endif
  549. #endif /* !_AMIGA */
  550.  
  551.     strncpy(argv0_path, progpath, MAXPATHLEN);
  552.     
  553. #if HAVE_READLINK
  554.     {
  555.         char tmpbuffer[MAXPATHLEN+1];
  556.         int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN);
  557.         while (linklen != -1) {
  558.             /* It's not null terminated! */
  559.             tmpbuffer[linklen] = '\0';
  560. #ifdef _AMIGA
  561.             if (NULL==strchr(tmpbuffer,':'))
  562. #else
  563.             if (tmpbuffer[0] == SEP)
  564. #endif
  565.         /* tmpbuffer should never be longer than MAXPATHLEN,
  566.            but extra check does not hurt */
  567.                 strncpy(argv0_path, tmpbuffer, MAXPATHLEN);
  568.             else {
  569.                 /* Interpret relative to progpath */
  570.                 reduce(argv0_path);
  571.                 joinpath(argv0_path, tmpbuffer);
  572.             }
  573.             linklen = readlink(argv0_path, tmpbuffer, MAXPATHLEN);
  574.         }
  575.     }
  576. #endif /* HAVE_READLINK */
  577.  
  578.     reduce(argv0_path);
  579.     /* At this point, argv0_path is guaranteed to be less than
  580.        MAXPATHLEN bytes long.
  581.     */
  582.  
  583.     if (!(pfound = search_for_prefix(argv0_path, home))) {
  584.         if (!Py_FrozenFlag)
  585.             fprintf(stderr,
  586.                     "Could not find platform independent libraries <prefix>\n");
  587.         strncpy(prefix, PREFIX, MAXPATHLEN);
  588.         joinpath(prefix, lib_python);
  589.     }
  590.     else
  591.         reduce(prefix);
  592.     
  593.     if (!(efound = search_for_exec_prefix(argv0_path, home))) {
  594.         if (!Py_FrozenFlag)
  595.             fprintf(stderr,
  596.                     "Could not find platform dependent libraries <exec_prefix>\n");
  597.         strncpy(exec_prefix, EXEC_PREFIX, MAXPATHLEN);
  598.         joinpath(exec_prefix, "lib/lib-dynload");
  599.     }
  600.     /* If we found EXEC_PREFIX do *not* reduce it!  (Yet.) */
  601.  
  602.     if ((!pfound || !efound) && !Py_FrozenFlag)
  603.         fprintf(stderr,
  604.                 "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
  605.  
  606.     /* Calculate size of return buffer.
  607.      */
  608.     bufsz = 0;
  609.  
  610.     if (rtpypath)
  611.         bufsz += strlen(rtpypath) + 1;
  612.  
  613.     prefixsz = strlen(prefix) + 1;
  614.  
  615.     while (1) {
  616.         char *delim = strchr(defpath, DELIM);
  617.  
  618. #ifdef _AMIGA
  619.         if (NULL==strchr(defpath,':'))
  620. #else
  621.         if (defpath[0] != SEP)
  622. #endif
  623.             /* Paths are relative to prefix */
  624.             bufsz += prefixsz;
  625.  
  626.         if (delim)
  627.             bufsz += delim - defpath + 1;
  628.         else {
  629.             bufsz += strlen(defpath) + 1;
  630.             break;
  631.         }
  632.         defpath = delim + 1;
  633.     }
  634.  
  635.     bufsz += strlen(exec_prefix) + 1;
  636.  
  637.     /* This is the only malloc call in this file */
  638.     buf = PyMem_Malloc(bufsz);
  639.  
  640.     if (buf == NULL) {
  641.         /* We can't exit, so print a warning and limp along */
  642.         fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n");
  643.         fprintf(stderr, "Using default static PYTHONPATH.\n");
  644.         module_search_path = PYTHONPATH;
  645.     }
  646.     else {
  647.         /* Run-time value of $PYTHONPATH goes first */
  648.         if (rtpypath) {
  649.             strcpy(buf, rtpypath);
  650.             strcat(buf, delimiter);
  651.         }
  652.         else
  653.             buf[0] = '\0';
  654.  
  655.         /* Next goes merge of compile-time $PYTHONPATH with
  656.          * dynamically located prefix.
  657.          */
  658.         defpath = pythonpath;
  659.         while (1) {
  660.             char *delim = strchr(defpath, DELIM);
  661.  
  662. #ifdef _AMIGA
  663.             if (NULL==strchr(defpath,':')) {
  664. #else
  665.             if (defpath[0] != SEP) {
  666. #endif
  667.                 strcat(buf, prefix);
  668.                 strcat(buf, separator);
  669.             }
  670.  
  671.             if (delim) {
  672.                 size_t len = delim - defpath + 1;
  673.                 size_t end = strlen(buf) + len;
  674.                 strncat(buf, defpath, len);
  675.                 *(buf + end) = '\0';
  676.             }
  677.             else {
  678.                 strcat(buf, defpath);
  679.                 break;
  680.             }
  681.             defpath = delim + 1;
  682.         }
  683.         strcat(buf, delimiter);
  684.  
  685.         /* Finally, on goes the directory for dynamic-load modules */
  686.         strcat(buf, exec_prefix);
  687.  
  688.         /* And publish the results */
  689.         module_search_path = buf;
  690.     }
  691.  
  692.     /* Reduce prefix and exec_prefix to their essence,
  693.      * e.g. /usr/local/lib/python1.5 is reduced to /usr/local.
  694.      * If we're loading relative to the build directory,
  695.      * return the compiled-in defaults instead.
  696.      */
  697.     if (pfound > 0) {
  698.         reduce(prefix);
  699.         reduce(prefix);
  700.     }
  701.     else
  702.         strncpy(prefix, PREFIX, MAXPATHLEN);
  703.  
  704.     if (efound > 0) {
  705.         reduce(exec_prefix);
  706.         reduce(exec_prefix);
  707.         reduce(exec_prefix);
  708.     }
  709.     else
  710.         strncpy(exec_prefix, EXEC_PREFIX, MAXPATHLEN);
  711. }
  712.  
  713.  
  714. /* External interface */
  715.  
  716. char *
  717. Py_GetPath(void)
  718. {
  719.     if (!module_search_path)
  720.         calculate_path();
  721.     return module_search_path;
  722. }
  723.  
  724. char *
  725. Py_GetPrefix(void)
  726. {
  727.     if (!module_search_path)
  728.         calculate_path();
  729.     return prefix;
  730. }
  731.  
  732. char *
  733. Py_GetExecPrefix(void)
  734. {
  735.     if (!module_search_path)
  736.         calculate_path();
  737.     return exec_prefix;
  738. }
  739.  
  740. char *
  741. Py_GetProgramFullPath(void)
  742. {
  743.     if (!module_search_path)
  744.         calculate_path();
  745.     return progpath;
  746. }
  747.