Guido van Rossum | 582646a | 1996-05-28 22:30:17 +0000 | [diff] [blame^] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
| 25 | /* Return the initial module search path. */ |
| 26 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 27 | #include "Python.h" |
| 28 | #include "osdefs.h" |
| 29 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 30 | |
| 31 | #ifndef PYTHONPATH |
| 32 | #define PYTHONPATH ".:/usr/local/lib/python" |
| 33 | #endif |
| 34 | |
| 35 | |
Guido van Rossum | 582646a | 1996-05-28 22:30:17 +0000 | [diff] [blame^] | 36 | /* This is called once from pythonrun to initialize sys.path. The |
| 37 | environment variable PYTHONPATH is fetched and the default path |
| 38 | appended. The default path may be passed to the preprocessor; if |
| 39 | not, a hardcoded default is used, which only makes (some) sense on |
| 40 | Unix. */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 41 | |
| 42 | char * |
Guido van Rossum | 582646a | 1996-05-28 22:30:17 +0000 | [diff] [blame^] | 43 | Py_GetPath() |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 44 | { |
| 45 | char *path = getenv("PYTHONPATH"); |
| 46 | char *defpath = PYTHONPATH; |
| 47 | static char *buf = NULL; |
| 48 | char *p; |
| 49 | int n; |
| 50 | |
| 51 | if (path == NULL) |
| 52 | path = ""; |
| 53 | n = strlen(path) + strlen(defpath) + 2; |
| 54 | if (buf != NULL) { |
| 55 | free(buf); |
| 56 | buf = NULL; |
| 57 | } |
| 58 | buf = malloc(n); |
| 59 | if (buf == NULL) |
| 60 | Py_FatalError("not enough memory to copy module search path"); |
| 61 | strcpy(buf, path); |
| 62 | p = buf + strlen(buf); |
| 63 | if (p != buf) |
| 64 | *p++ = DELIM; |
| 65 | strcpy(p, defpath); |
| 66 | return buf; |
| 67 | } |