Merge the C wrapper.
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 /* In case we have a relative or absolute pathname (ie. contains a slash),
31 * then realpath wll work. But if the tool was found in the PATH, realpath
32 * won't work, and we'll have to search ourselves.
33 * This if{}else{} block allocates memory for fullname. */
34 if( strchr( argv[0], '/' ) ) {
35 fullname = (char*) malloc( PATH_MAX * sizeof(char) );
36 if( ! realpath( argv[0], fullname ) ) {
37 perror( "tool wrapper" );
48 colon = mypath = path = strdup( getenv( "PATH" ) );
50 colon = strchr( mypath, ':' );
54 testname = strdup( mypath );
55 testname = (char*) realloc( testname, strlen( testname )
58 memset( testname + strlen( testname ),
60 strlen( argv[0] ) + 2 * sizeof(char) );
61 strcat( testname, "/" );
62 strcat( testname, argv[0] );
63 if( stat( testname, &st ) == 0 ) {
64 /* OK, exists. Is it a regular file, or a
65 * symlink, which the current user may execute? */
66 if( S_ISREG( st.st_mode ) && ! access( testname, X_OK || R_OK ) ) {
67 fullname = strdup( testname );
76 fprintf( stderr, "tool wrapper: %s: command not found\n", argv[0] );
81 /* Duplicate my own name to add the 'dot' to tool name */
82 realname = strdup( fullname );
83 realname = (char*) realloc( realname, strlen( realname) + 2 * sizeof(char) );
84 realname[ strlen( realname ) + 1 ] = '\0';
86 /* Add the dot after the last '/' */
87 lastslash = strrchr( realname, '/' );
88 memmove( lastslash + 1, lastslash, strlen( lastslash ) );
89 *( lastslash + 1 ) = '.';
91 /* Compute the basedir of the tool */
92 basedir = strdup( fullname );
93 lastslash = strrchr( basedir, '/' );
95 lastslash = strrchr( basedir, '/' );
99 len = strlen( basedir );
100 basedir = (char*) realloc( basedir, len + 5 );
101 *( basedir + len ) = '\0';
102 strcat( basedir, "/lib" );
104 /* Now add the directory with our runtime libraries to the
105 front of the library search path, LD_LIBRARY_PATH */
106 ldlibpath = getenv( "LD_LIBRARY_PATH" );
108 basedir = (char*) realloc( basedir, strlen( basedir )
109 + strlen( ldlibpath )
110 + 2 * sizeof(char) );
111 strcat( basedir, ":" );
112 strcat( basedir, ldlibpath );
115 if( setenv( "LD_LIBRARY_PATH", basedir, 1 ) ) {
117 perror( "tool wrapper" );
121 /* Execute the real tool, now */
122 execve_ret = execve( realname, argv, environ );
124 /* In case something went wrong above, print a
125 diagnostic message, and exit with error code 1 */
126 perror( "tool wrapper" );