11 #define LDLP "DYLD_LIBRARY_PATH"
13 #define LDLP "LD_LIBRARY_PATH"
16 /* Needed for execve */
17 extern char **environ;
22 char *fullname; /* 'fullname' is used to store the absolute path to the
23 tool being executed; it serves as a base to compute
24 the realname of that tool, and the directory holding
25 our runtime libraries */
26 char *realname; /* 'realname' is the real name of the tool, that is what
27 the wrapper is currently impersonating */
28 char *basedir; /* 'libdir' contains our runtime libraries */
30 char *lastslash; /* Temporary variables now */
35 /* Avoid the warning-treated-as-error: "error: unused parameter 'argc'" */
38 /* In case we have a relative or absolute pathname (ie. contains a slash),
39 * then realpath wll work. But if the tool was found in the PATH, realpath
40 * won't work, and we'll have to search ourselves.
41 * This if{}else{} block allocates memory for fullname. */
42 if( strchr( argv[0], '/' ) ) {
43 fullname = (char*) malloc( PATH_MAX * sizeof(char) );
44 if( ! realpath( argv[0], fullname ) ) {
45 perror( "tool wrapper" );
56 colon = mypath = path = strdup( getenv( "PATH" ) );
58 colon = strchr( mypath, ':' );
62 testname = strdup( mypath );
63 testname = (char*) realloc( testname, strlen( testname )
66 memset( testname + strlen( testname ),
68 strlen( argv[0] ) + 2 * sizeof(char) );
69 strcat( testname, "/" );
70 strcat( testname, argv[0] );
71 if( stat( testname, &st ) == 0 ) {
72 /* OK, exists. Is it a regular file, or a
73 * symlink, which the current user may execute? */
74 if( S_ISREG( st.st_mode ) && ! access( testname, X_OK | R_OK ) ) {
75 fullname = strdup( testname );
84 fprintf( stderr, "tool wrapper: %s: command not found\n", argv[0] );
89 /* Duplicate my own name to add the 'dot' to tool name */
90 realname = strdup( fullname );
91 realname = (char*) realloc( realname, strlen( realname) + 2 * sizeof(char) );
92 realname[ strlen( realname ) + 1 ] = '\0';
94 /* Add the dot after the last '/' */
95 lastslash = strrchr( realname, '/' );
96 memmove( lastslash + 1, lastslash, strlen( lastslash ) );
97 *( lastslash + 1 ) = '.';
99 /* Compute the basedir of the tool */
100 basedir = strdup( fullname );
101 lastslash = strrchr( basedir, '/' );
103 lastslash = strrchr( basedir, '/' );
107 len = strlen( basedir );
108 basedir = (char*) realloc( basedir, len + 5 );
109 *( basedir + len ) = '\0';
110 strcat( basedir, "/lib" );
112 /* Now add the directory with our runtime libraries to the
113 front of the library search path, LD_LIBRARY_PATH */
114 ldlibpath = getenv(LDLP);
116 basedir = (char*) realloc( basedir, strlen( basedir )
117 + strlen( ldlibpath )
118 + 2 * sizeof(char) );
119 strcat( basedir, ":" );
120 strcat( basedir, ldlibpath );
123 if( setenv( LDLP, basedir, 1 ) ) {
125 perror( "tool wrapper" );
129 /* Execute the real tool, now */
130 execve_ret = execve( realname, argv, environ );
132 /* In case something went wrong above, print a
133 diagnostic message, and exit with error code 1 */
134 perror( "tool wrapper" );