summaryrefslogtreecommitdiffstats
path: root/Python/sysmodule.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1997-04-25 15:38:31 (GMT)
committerGuido van Rossum <guido@python.org>1997-04-25 15:38:31 (GMT)
commitc474deaaf63deb633bf9bc951c8cc1cd5d107eda (patch)
tree11e4d355d5d8d5a03a14a5df5349546943e1166a /Python/sysmodule.c
parent4246edda7c055359443ec44a0b862310042d3a1d (diff)
downloadcpython-c474deaaf63deb633bf9bc951c8cc1cd5d107eda.zip
cpython-c474deaaf63deb633bf9bc951c8cc1cd5d107eda.tar.gz
cpython-c474deaaf63deb633bf9bc951c8cc1cd5d107eda.tar.bz2
Expand one level of symbolic link in sys.argv[0] before inserting its
dirname in sys.path. This means that you can create a symbolic link foo in /usr/local/bin pointing to /usr/yourname/src/foo/foo.py, and then invoking foo will insert /usr/yourname/src/foo in sys.path, not /usr/local/bin. This makes it easier to have multifile programs (before, the program would have to do an os.readlink(sys.argv[0]) itself and insert the resulting directory in sys.path -- Grail does this). Note that the expansion is only used for sys.path; sys.argv[0] is still the original, unadorned filename (/usr/local/bin/foo in the example).
Diffstat (limited to 'Python/sysmodule.c')
-rw-r--r--Python/sysmodule.c49
1 files changed, 41 insertions, 8 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index c1eaeed..69cb7aa 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -54,6 +54,10 @@ Data members:
object *sys_trace, *sys_profile;
int sys_checkinterval = 10;
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
static object *sysdict;
#ifdef MS_COREDLL
@@ -387,35 +391,64 @@ setpythonargv(argc, argv)
if (sysset("argv", av) != 0)
fatal("can't assign sys.argv");
if (path != NULL) {
+ char *argv0 = argv[0];
char *p = NULL;
int n = 0;
object *a;
+#ifdef HAVE_READLINK
+ char link[MAXPATHLEN+1];
+ char argv0copy[2*MAXPATHLEN+1];
+ int nr = 0;
+ if (argc > 0 && argv0 != NULL)
+ nr = readlink(argv0, link, MAXPATHLEN);
+ if (nr > 0) {
+ /* It's a symlink */
+ link[nr] = '\0';
+ if (link[0] == SEP)
+ argv0 = link; /* Link to absolute path */
+ else if (strchr(link, SEP) == NULL)
+ ; /* Link without path */
+ else {
+ /* Must join(dirname(argv0), link) */
+ char *q = strrchr(argv0, SEP);
+ if (q == NULL)
+ argv0 = link; /* argv0 without path */
+ else {
+ /* Must make a copy */
+ strcpy(argv0copy, argv0);
+ q = strrchr(argv0copy, SEP);
+ strcpy(q+1, link);
+ argv0 = argv0copy;
+ }
+ }
+ }
+#endif /* HAVE_READLINK */
#if SEP == '\\' /* Special case for MS filename syntax */
- if (argc > 0 && argv[0] != NULL) {
+ if (argc > 0 && argv0 != NULL) {
char *q;
- p = strrchr(argv[0], SEP);
+ p = strrchr(argv0, SEP);
/* Test for alternate separator */
- q = strrchr(p ? p : argv[0], '/');
+ q = strrchr(p ? p : argv0, '/');
if (q != NULL)
p = q;
if (p != NULL) {
- n = p + 1 - argv[0];
+ n = p + 1 - argv0;
if (n > 1 && p[-1] != ':')
n--; /* Drop trailing separator */
}
}
#else /* All other filename syntaxes */
- if (argc > 0 && argv[0] != NULL)
- p = strrchr(argv[0], SEP);
+ if (argc > 0 && argv0 != NULL)
+ p = strrchr(argv0, SEP);
if (p != NULL) {
- n = p + 1 - argv[0];
+ n = p + 1 - argv0;
#if SEP == '/' /* Special case for Unix filename syntax */
if (n > 1)
n--; /* Drop trailing separator */
#endif /* Unix */
}
#endif /* All others */
- a = newsizedstringobject(argv[0], n);
+ a = newsizedstringobject(argv0, n);
if (a == NULL)
fatal("no mem for sys.path insertion");
if (inslistitem(path, 0, a) < 0)
value='bug_io_32_11'>bug_io_32_11 Tcl is a high-level, general-purpose, interpreted, dynamic programming language. It was designed with the goal of being very simple but powerful.
summaryrefslogtreecommitdiffstats
path: root/unix/ldAix
blob: d61ab2ac5b9e2d47fb6ee458cf05febd425e4c05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/sh
#
# ldAix ldCmd ldArg ldArg ...
#
# This shell script provides a wrapper for ld under AIX in order to
# create the .exp file required for linking.  Its arguments consist
# of the name and arguments that would normally be provided to the
# ld command.  This script extracts the names of the object files
# from the argument list, creates a .exp file describing all of the
# symbols exported by those files, and then invokes "ldCmd" to
# perform the real link.
#
# RCS: @(#) $Id: ldAix,v 1.2 1998/09/14 18:40:16 stanton Exp $

# Extract from the arguments the names of all of the object files.

args=$*
ofiles=""
for i do
    x=`echo $i | grep '[^.].o$'`
    if test "$x" != ""; then
	ofiles="$ofiles $i"
    fi
done

# Create the export file from all of the object files, using nm followed
# by sed editing.  Here are some tricky aspects of this:
#
# 1. Nm produces different output under AIX 4.1 than under AIX 3.2.5;
#    the following statements handle both versions.
# 2. Use the -g switch to nm instead of -e under 4.1 (this shows just
#    externals, not statics;  -g isn't available under 3.2.5, though).
# 3. Eliminate lines that end in ":": these are the names of object
#    files (relevant in 4.1 only).
# 4. Eliminate entries with the "U" key letter;  these are undefined
#    symbols (relevant in 4.1 only).
# 5. Eliminate lines that contain the string "0|extern" preceded by space;
#    in 3.2.5, these are undefined symbols (address 0).
# 6. Eliminate lines containing the "unamex" symbol.  In 3.2.5, these
#    are also undefined symbols.
# 7. If a line starts with ".", delete the leading ".", since this will
#    just cause confusion later.
# 8. Eliminate everything after the first field in a line, so that we're
#    left with just the symbol name.

nmopts="-g -C"
osver=`uname -v`
if test $osver -eq 3; then
  nmopts="-e"
fi
rm -f lib.exp
echo "#! " >lib.exp
/usr/ccs/bin/nm $nmopts -h $ofiles | sed -e '/:$/d' -e '/ U /d' -e '/[ 	]0|extern/d' -e '/unamex/d' -e 's/^\.//' -e 's/[ 	|].*//' | sort | uniq >>lib.exp

# Extract the name of the object file that we're linking.  If it's a .a
# file, then link all the objects together into a single file "shr.o"
# and then put that into the archive.  Otherwise link the object files
# directly into the .a file.

outputFile=`echo $args | sed -e 's/.*-o \([^ ]*\).*/\1/'`
noDotA=`echo $outputFile | sed -e '/\.a$/d'`
echo "noDotA=\"$noDotA\""
if test "$noDotA" = "" ; then
    linkArgs=`echo $args | sed -e 's/-o .*\.a /-o shr.o /'`
    echo $linkArgs
    eval $linkArgs
    echo ar cr $outputFile shr.o
    ar cr $outputFile shr.o
    rm -f shr.o
else
    eval $args
fi