scripts/wrapper.c
changeset 2381 0ca0f85a4b2a
parent 2380 40fe96dff39e
child 2382 cbd07f3dd6e3
     1.1 --- a/scripts/wrapper.c	Wed Apr 06 21:34:22 2011 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,136 +0,0 @@
     1.4 -#include <limits.h>
     1.5 -#include <stdlib.h>
     1.6 -#include <stdio.h>
     1.7 -#include <string.h>
     1.8 -#include <sys/types.h>
     1.9 -#include <sys/stat.h>
    1.10 -#include <unistd.h>
    1.11 -#include <errno.h>
    1.12 -
    1.13 -#ifdef	__APPLE__
    1.14 -#define LDLP "DYLD_LIBRARY_PATH"
    1.15 -#else
    1.16 -#define LDLP "LD_LIBRARY_PATH"
    1.17 -#endif
    1.18 -
    1.19 -/* Needed for execve */
    1.20 -extern char **environ;
    1.21 -
    1.22 -int main( int argc,
    1.23 -          char** argv )
    1.24 -{
    1.25 -  char *fullname;   /* 'fullname' is used to store the absolute path to the
    1.26 -                       tool being executed; it serves as a base to compute
    1.27 -                       the realname of that tool, and the directory holding
    1.28 -                       our runtime libraries */
    1.29 -  char *realname;   /* 'realname' is the real name of the tool, that is what
    1.30 -                       the wrapper is currently impersonating */
    1.31 -  char *basedir;    /* 'libdir' contains our runtime libraries */
    1.32 -
    1.33 -  char *lastslash;  /* Temporary variables now */
    1.34 -  char *ldlibpath;
    1.35 -  size_t len;
    1.36 -  int execve_ret;
    1.37 -
    1.38 -  /* Avoid the warning-treated-as-error: "error: unused parameter 'argc'" */
    1.39 -  len = argc;
    1.40 -
    1.41 -  /* In case we have a relative or absolute pathname (ie. contains a slash),
    1.42 -   * then realpath wll work. But if the tool was found in the PATH, realpath
    1.43 -   * won't work, and we'll have to search ourselves.
    1.44 -   * This if{}else{} block allocates memory for fullname. */
    1.45 -  if( strchr( argv[0], '/' ) ) {
    1.46 -    fullname = (char*) malloc( PATH_MAX * sizeof(char) );
    1.47 -    if( ! realpath( argv[0], fullname ) ) {
    1.48 -      perror( "tool wrapper" );
    1.49 -      exit( 1 );
    1.50 -    }
    1.51 -  } else {
    1.52 -    char *path;
    1.53 -    char *mypath;
    1.54 -    char *colon;
    1.55 -    char *testname;
    1.56 -    struct stat st;
    1.57 -
    1.58 -    fullname = NULL;
    1.59 -    colon = mypath = path = strdup( getenv( "PATH" ) );
    1.60 -    while( colon ) {
    1.61 -      colon = strchr( mypath, ':' );
    1.62 -      if( colon ) {
    1.63 -        *colon = '\0';
    1.64 -      }
    1.65 -      testname = strdup( mypath );
    1.66 -      testname = (char*) realloc( testname,   strlen( testname )
    1.67 -                                            + strlen( argv[0] )
    1.68 -                                            + 2 * sizeof(char) );
    1.69 -      memset( testname + strlen( testname ),
    1.70 -              0,
    1.71 -              strlen( argv[0] ) + 2 * sizeof(char) );
    1.72 -      strcat( testname, "/" );
    1.73 -      strcat( testname, argv[0] );
    1.74 -      if( stat( testname, &st ) == 0 ) {
    1.75 -        /* OK, exists. Is it a regular file, or a
    1.76 -         * symlink, which the current user may execute? */
    1.77 -        if( S_ISREG( st.st_mode ) && ! access( testname, X_OK | R_OK ) ) {
    1.78 -          fullname = strdup( testname );
    1.79 -          break;
    1.80 -        }
    1.81 -      }
    1.82 -      free( testname );
    1.83 -      mypath = colon + 1;
    1.84 -    }
    1.85 -    free( path );
    1.86 -    if( ! fullname ) {
    1.87 -      fprintf( stderr, "tool wrapper: %s: command not found\n", argv[0] );
    1.88 -      exit( 1 );
    1.89 -    }
    1.90 -  }
    1.91 -
    1.92 -  /* Duplicate my own name to add the 'dot' to tool name */
    1.93 -  realname = strdup( fullname );
    1.94 -  realname = (char*) realloc( realname, strlen( realname) + 2 * sizeof(char) );
    1.95 -  realname[ strlen( realname ) + 1 ] = '\0';
    1.96 -
    1.97 -  /* Add the dot after the last '/' */
    1.98 -  lastslash = strrchr( realname, '/' );
    1.99 -  memmove( lastslash + 1, lastslash, strlen( lastslash ) );
   1.100 -  *( lastslash + 1 ) = '.';
   1.101 -
   1.102 -  /* Compute the basedir of the tool */
   1.103 -  basedir = strdup( fullname );
   1.104 -  lastslash = strrchr( basedir, '/' );
   1.105 -  *lastslash = '\0';
   1.106 -  lastslash = strrchr( basedir, '/' );
   1.107 -  *lastslash = '\0';
   1.108 -
   1.109 -  /* Append '/lib' */
   1.110 -  len = strlen( basedir );
   1.111 -  basedir = (char*) realloc( basedir, len + 5 );
   1.112 -  *( basedir + len ) = '\0';
   1.113 -  strcat( basedir, "/lib" );
   1.114 -
   1.115 -  /* Now add the directory with our runtime libraries to the
   1.116 -     front of the library search path, LD_LIBRARY_PATH */
   1.117 -  ldlibpath = getenv(LDLP);
   1.118 -  if( ldlibpath ) {
   1.119 -    basedir = (char*) realloc( basedir,   strlen( basedir )
   1.120 -                                        + strlen( ldlibpath )
   1.121 -                                        + 2 * sizeof(char) );
   1.122 -    strcat( basedir, ":" );
   1.123 -    strcat( basedir, ldlibpath );
   1.124 -  }
   1.125 -
   1.126 -  if( setenv( LDLP, basedir, 1 ) ) {
   1.127 -    errno = ENOMEM;
   1.128 -    perror( "tool wrapper" );
   1.129 -    exit( 1 );
   1.130 -  }
   1.131 -
   1.132 -  /* Execute the real tool, now */
   1.133 -  execve_ret = execve( realname, argv, environ );
   1.134 -
   1.135 -  /* In case something went wrong above, print a
   1.136 -     diagnostic message, and exit with error code 1 */
   1.137 -  perror( "tool wrapper" );
   1.138 -  return 1;
   1.139 -}