11 /* Needed for execve */
12 extern char **environ;
17 char *fullname; /* 'fullname' is used to store the absolute path to the
18 tool being executed; it serves as a base to compute
19 the realname of that tool, and the directory holding
20 our runtime libraries */
21 char *realname; /* 'realname' is the real name of the tool, that is what
22 the wrapper is currently impersonating */
23 char *basedir; /* 'libdir' contains our runtime libraries */
25 char *lastslash; /* Temporary variables now */
30 /* Avoid the warning-treated-as-error: "error: unused parameter 'argc'" */
33 /* In case we have a relative or absolute pathname (ie. contains a slash),
34 * then realpath wll work. But if the tool was found in the PATH, realpath
35 * won't work, and we'll have to search ourselves.
36 * This if{}else{} block allocates memory for fullname. */
37 if( strchr( argv[0], '/' ) ) {
38 fullname = (char*) malloc( PATH_MAX * sizeof(char) );
39 if( ! realpath( argv[0], fullname ) ) {
40 perror( "tool wrapper" );
51 colon = mypath = path = strdup( getenv( "PATH" ) );
53 colon = strchr( mypath, ':' );
57 testname = strdup( mypath );
58 testname = (char*) realloc( testname, strlen( testname )
61 memset( testname + strlen( testname ),
63 strlen( argv[0] ) + 2 * sizeof(char) );
64 strcat( testname, "/" );
65 strcat( testname, argv[0] );
66 if( stat( testname, &st ) == 0 ) {
67 /* OK, exists. Is it a regular file, or a
68 * symlink, which the current user may execute? */
69 if( S_ISREG( st.st_mode ) && ! access( testname, X_OK || R_OK ) ) {
70 fullname = strdup( testname );
79 fprintf( stderr, "tool wrapper: %s: command not found\n", argv[0] );
84 /* Duplicate my own name to add the 'dot' to tool name */
85 realname = strdup( fullname );
86 realname = (char*) realloc( realname, strlen( realname) + 2 * sizeof(char) );
87 realname[ strlen( realname ) + 1 ] = '\0';
89 /* Add the dot after the last '/' */
90 lastslash = strrchr( realname, '/' );
91 memmove( lastslash + 1, lastslash, strlen( lastslash ) );
92 *( lastslash + 1 ) = '.';
94 /* Compute the basedir of the tool */
95 basedir = strdup( fullname );
96 lastslash = strrchr( basedir, '/' );
98 lastslash = strrchr( basedir, '/' );
102 len = strlen( basedir );
103 basedir = (char*) realloc( basedir, len + 5 );
104 *( basedir + len ) = '\0';
105 strcat( basedir, "/lib" );
107 /* Now add the directory with our runtime libraries to the
108 front of the library search path, LD_LIBRARY_PATH */
109 ldlibpath = getenv( "LD_LIBRARY_PATH" );
111 basedir = (char*) realloc( basedir, strlen( basedir )
112 + strlen( ldlibpath )
113 + 2 * sizeof(char) );
114 strcat( basedir, ":" );
115 strcat( basedir, ldlibpath );
118 if( setenv( "LD_LIBRARY_PATH", basedir, 1 ) ) {
120 perror( "tool wrapper" );
124 /* Execute the real tool, now */
125 execve_ret = execve( realname, argv, environ );
127 /* In case something went wrong above, print a
128 diagnostic message, and exit with error code 1 */
129 perror( "tool wrapper" );