srcanamdw/codescanner/pyinstaller/source/linux/getpath.c
author noe\swadi
Thu, 18 Feb 2010 12:29:02 +0530
changeset 1 22878952f6e2
permissions -rw-r--r--
Committing the CodeScanner Core tool This component has been moved from the StaticAnaApps package. BUG : 5889 (http://developer.symbian.org/webbugs/show_bug.cgi?id=5889).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     1
/***********************************************************
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     2
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     3
The Netherlands.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     4
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     5
                        All Rights Reserved
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     6
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     7
Permission to use, copy, modify, and distribute this software and its
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     8
documentation for any purpose and without fee is hereby granted,
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
     9
provided that the above copyright notice appear in all copies and that
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    10
both that copyright notice and this permission notice appear in
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    11
supporting documentation, and that the names of Stichting Mathematisch
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    12
Centrum or CWI or Corporation for National Research Initiatives or
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    13
CNRI not be used in advertising or publicity pertaining to
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    14
distribution of the software without specific, written prior
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    15
permission.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    16
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    17
While CWI is the initial source for this software, a modified version
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    18
is made available by the Corporation for National Research Initiatives
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    19
(CNRI) at the Internet address ftp://ftp.python.org.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    20
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    21
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    22
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    23
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    24
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    25
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    26
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    27
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    28
PERFORMANCE OF THIS SOFTWARE.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    29
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    30
******************************************************************/
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    31
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    32
/* A special version for minimal installs, where
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    33
   the bootstrap path is the directory in which
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    34
   the executable lives.
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    35
   Gordon McMillan, McMillan Enterprises, Inc. */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    36
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    37
/* Return the initial module search path. */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    38
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    39
#include "Python.h"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    40
#include "osdefs.h"
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    41
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    42
#include <sys/types.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    43
#include <sys/stat.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    44
#include <string.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    45
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    46
#if HAVE_UNISTD_H
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    47
#include <unistd.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    48
#endif /* HAVE_UNISTD_H */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    49
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    50
#ifdef WITH_NEXT_FRAMEWORK
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    51
#include <mach-o/dyld.h>
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    52
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    53
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    54
static char prefix[MAXPATHLEN+1];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    55
static char *exec_prefix;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    56
static char progpath[MAXPATHLEN+1];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    57
static char *module_search_path = NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    58
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    59
static void
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    60
reduce(char *dir)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    61
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    62
	int i = strlen(dir);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    63
	while (i > 0 && dir[i] != SEP)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    64
		--i;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    65
	dir[i] = '\0';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    66
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    67
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    68
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    69
#ifndef S_ISREG
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    70
#define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    71
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    72
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    73
#ifndef S_ISDIR
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    74
#define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    75
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    76
#if 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    77
static int
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    78
isfile(char *filename)		/* Is file, not directory */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    79
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    80
	struct stat buf;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    81
	if (stat(filename, &buf) != 0)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    82
		return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    83
	if (!S_ISREG(buf.st_mode))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    84
		return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    85
	return 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    86
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    87
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    88
#if 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    89
static int
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    90
ismodule(char *filename)		/* Is module -- check for .pyc/.pyo too */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    91
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    92
	if (isfile(filename))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    93
		return 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    94
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    95
	/* Check for the compiled version of prefix. */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    96
	if (strlen(filename) < MAXPATHLEN) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    97
		strcat(filename, Py_OptimizeFlag ? "o" : "c");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    98
		if (isfile(filename))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
    99
			return 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   100
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   101
	return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   102
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   103
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   104
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   105
static int
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   106
isxfile(char *filename)		/* Is executable file */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   107
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   108
	struct stat buf;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   109
	if (stat(filename, &buf) != 0)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   110
		return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   111
	if (!S_ISREG(buf.st_mode))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   112
		return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   113
	if ((buf.st_mode & 0111) == 0)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   114
		return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   115
	return 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   116
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   117
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   118
#if 0
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   119
static int
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   120
isdir(char *filename)			/* Is directory */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   121
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   122
	struct stat buf;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   123
	if (stat(filename, &buf) != 0)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   124
		return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   125
	if (!S_ISDIR(buf.st_mode))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   126
		return 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   127
	return 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   128
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   129
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   130
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   131
static void
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   132
joinpath(char *buffer, char *stuff)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   133
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   134
	int n, k;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   135
	if (stuff[0] == SEP)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   136
		n = 0;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   137
	else {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   138
		n = strlen(buffer);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   139
		if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   140
			buffer[n++] = SEP;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   141
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   142
	k = strlen(stuff);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   143
	if (n + k > MAXPATHLEN)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   144
		k = MAXPATHLEN - n;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   145
	strncpy(buffer+n, stuff, k);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   146
	buffer[n+k] = '\0';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   147
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   148
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   149
static void
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   150
calculate_path(void)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   151
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   152
	char *prog = Py_GetProgramName();	/* use Py_SetProgramName(argv[0]) before Py_Initialize() */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   153
	char argv0_path[MAXPATHLEN+1];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   154
	char *epath;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   155
	char *path = NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   156
	char *ppath = NULL;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   157
        int  numchars;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   158
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   159
	if (strchr(prog, SEP))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   160
		strcpy(progpath, prog);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   161
	else {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   162
#if HAVE_READLINK
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   163
            sprintf(argv0_path, "/proc/%d/exe", getpid());
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   164
            numchars = readlink(argv0_path, progpath, MAXPATHLEN);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   165
            if (numchars > 0) 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   166
                progpath[numchars] = '\0';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   167
            else {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   168
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   169
		epath = getenv("PATH");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   170
                if (epath) 
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   171
                    path = malloc(strlen(epath)+3);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   172
		if (path) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   173
                    strcpy(path, ".:");
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   174
                    strcat(path, epath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   175
		    ppath = path;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   176
	    	    while (1) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   177
				char *delim = strchr(ppath, DELIM);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   178
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   179
				if (delim) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   180
					int len = delim - ppath;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   181
					strncpy(progpath, ppath, len);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   182
					*(progpath + len) = '\0';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   183
				}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   184
				else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   185
					strcpy(progpath, ppath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   186
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   187
				joinpath(progpath, prog);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   188
				if (isxfile(progpath))
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   189
					break;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   190
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   191
				if (!delim) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   192
					progpath[0] = '\0';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   193
					break;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   194
				}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   195
				ppath = delim + 1;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   196
		    }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   197
                    free(path);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   198
		}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   199
		else
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   200
			progpath[0] = '\0';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   201
#if HAVE_READLINK
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   202
            }
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   203
#endif
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   204
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   205
	/* at this point progpath includes the executable */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   206
	strcpy(argv0_path, progpath);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   207
	
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   208
#if HAVE_READLINK
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   209
	{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   210
		char tmpbuffer[MAXPATHLEN+1];
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   211
		int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   212
		while (linklen != -1) {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   213
			/* It's not null terminated! */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   214
			tmpbuffer[linklen] = '\0';
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   215
			if (tmpbuffer[0] == SEP)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   216
				strcpy(argv0_path, tmpbuffer);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   217
			else {
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   218
				/* Interpret relative to progpath */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   219
				reduce(argv0_path);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   220
				joinpath(argv0_path, tmpbuffer);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   221
			}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   222
			linklen = readlink(argv0_path, tmpbuffer, MAXPATHLEN);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   223
		}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   224
                strcpy(progpath, argv0_path);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   225
	}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   226
#endif /* HAVE_READLINK */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   227
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   228
	reduce(argv0_path);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   229
	/* now argv0_path is the directory of the executable */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   230
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   231
	strcpy(prefix, argv0_path);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   232
	exec_prefix = prefix;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   233
	module_search_path = malloc(strlen(prefix)+1);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   234
	strcpy(module_search_path, prefix);
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   235
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   236
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   237
/* External interface */
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   238
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   239
char *
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   240
Py_GetPath(void)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   241
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   242
	if (!module_search_path)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   243
		calculate_path();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   244
	return module_search_path;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   245
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   246
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   247
char *
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   248
Py_GetPrefix(void)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   249
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   250
	if (!module_search_path)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   251
		calculate_path();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   252
	return prefix;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   253
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   254
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   255
char *
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   256
Py_GetExecPrefix(void)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   257
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   258
	if (!module_search_path)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   259
		calculate_path();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   260
	return exec_prefix;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   261
}
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   262
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   263
char *
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   264
Py_GetProgramFullPath(void)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   265
{
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   266
	if (!module_search_path)
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   267
		calculate_path();
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   268
	return progpath;
22878952f6e2 Committing the CodeScanner Core tool
noe\swadi
parents:
diff changeset
   269
}