Ejemplo n.º 1
0
/* uses the global executable var */
int main(int argc, char *argv[])
{
  struct stat fileinfo;
  char *path = getenv("PATH");
  int len, found = 0;
  char *pathcounter, save;
  char *myname = argv[0];
  char myname_augmented[MAXPATHLEN];
#ifndef WIN_NT
  int link_len;
#endif

  set_user_home();

#ifndef WIN_NT
#ifndef SIMPLESCALAR
  /* Unix */
  /* if we can read symlink, then it is a symlink */
  if ( (link_len = readlink(myname, myname_augmented, MAXPATHLEN)) > 0 ) {
    /* we can't assume that the value of the link is null-terminated */
    if ( *(myname_augmented+link_len) != '\0' )
      *(myname_augmented+link_len+1) = '\0';
  } else
    strcpy(myname_augmented, myname);
#endif
#else
  /* Windows doesn't seem to have readlink() */
  strcpy(myname_augmented, myname);
  /* if executable doesn't end with .exe, then add it */
  if ( *(myname_augmented + strlen(myname) - 4) != '.'
       || tolower(*(myname_augmented + strlen(myname) - 3)) != 'e'
       || tolower(*(myname_augmented + strlen(myname) - 2)) != 'x'
       || tolower(*(myname_augmented + strlen(myname) - 1)) != 'e' )
    snprintf(myname_augmented, MAXPATHLEN, "%s.exe", myname);
#endif

#ifdef WIN_NT
  /* CygWin32 uses absolute paths like this:
     //<drive letter>/dir1/dir2/...
     actually /cygdrive/<drive letter>/....
     If we find such a path, we transform it to a windows-like pathname.
     This assumes that XSB has been compiled using the native Windows
     API, and is being run from CygWin32 bash (like from the test
     scripts). */
  transform_cygwin_pathname(myname_augmented);
#endif

  if (is_absolute_filename(myname_augmented))
    strcpy(executable_path_gl, myname_augmented);
  else {
    snprintf(executable_path_gl, MAXPATHLEN, "%s%c%s", current_dir_gl, SLASH, myname_augmented);
  }

  /* found executable by prepending cwd. */
  if ((!stat(executable_path_gl, &fileinfo)) && (S_ISREG(fileinfo.st_mode))) {
    printf("%s",get_flora_install_dir());
    return FALSE;
  }

  /* Otherwise, search PATH environment var.
     This code is a modified "which" shell builtin */
  pathcounter = path;
  while (*pathcounter != '\0' && found == 0) {
    len = 0;
    while (*pathcounter != PATH_SEPARATOR && *pathcounter != '\0') {
      len++;
      pathcounter++;
    }

    /* save the separator ':' (or ';' on NT and replace it with \0) */
    save = *pathcounter;
    *pathcounter = '\0';

    /* Now `len' holds the length of the PATH component 
       we are currently looking at.
       `pathcounter' points to the end of this component. */
    snprintf(executable_path_gl, MAXPATHLEN, "%s%c%s", pathcounter - len, SLASH, myname_augmented);

    /* restore the separator and addvance the pathcounter */
    *pathcounter = save;
    if (*pathcounter) pathcounter++;

#ifdef WIN_NT
    found = (0 == access(executable_path_gl, 02));	/* readable */
#else
    found = (0 == access(executable_path_gl, 01));	/* executable */
#endif

    if (found) printf("%s",get_flora_install_dir());
  }

  return FALSE;
}
Ejemplo n.º 2
0
/* uses the global executable var */
DllExport char *xsb_executable_full_path(char *myname)
{
  struct stat fileinfo;
  char *path = getenv("PATH");
  int len, found = 0;
  char *pathcounter, save;
  static char myname_augmented[MAXPATHLEN];
  char *dummy; /* to squash warnings */
#ifndef WIN_NT
  int link_len;
#endif

#ifndef WIN_NT
#ifndef SIMPLESCALAR
  /* Unix */
  /* if we can read symlink, then it is a symlink */
  if ( (link_len = readlink(myname, myname_augmented, MAXPATHLEN)) > 0 ) {
    /* we can't assume that the value of the link is null-terminated */
    if ( *(myname_augmented+link_len) != '\0' )
      *(myname_augmented+link_len+1) = '\0';
  } else
    strcpy(myname_augmented, myname);
#endif
#else
  /* Windows doesn't seem to have readlink() */
  strcpy(myname_augmented, myname);
  /* if executable doesn't end with .exe, then add it */
  if ( *(myname_augmented + strlen(myname) - 4) != '.'
       || tolower(*(myname_augmented + strlen(myname) - 3)) != 'e'
       || tolower(*(myname_augmented + strlen(myname) - 2)) != 'x'
       || tolower(*(myname_augmented + strlen(myname) - 1)) != 'e' )
    snprintf(myname_augmented, MAXPATHLEN, "%s.exe", myname);
#endif

#ifdef WIN_NT
  /* CygWin32 uses absolute paths like this:
     //<drive letter>/dir1/dir2/...
     actually /cygdrive/<drive letter>/....
     If we find such a path, we transform it to a windows-like pathname.
     This assumes that XSB has been compiled using the native Windows
     API, and is being run from CygWin32 bash (like from the test
     scripts). */
  transform_cygwin_pathname(myname_augmented);
#endif

  if (is_absolute_filename(myname_augmented))
    strcpy(executable_path_gl, myname_augmented);
  else {
    dummy = getcwd(current_dir_gl, MAXPATHLEN-1);
    snprintf(executable_path_gl, MAXPATHLEN, "%s%c%s", current_dir_gl, SLASH, myname_augmented);
  }

  /* found executable by prepending cwd. Make sure we haven't found a directory named xsb */
  if ((!stat(executable_path_gl, &fileinfo)) && (S_ISREG(fileinfo.st_mode))) return executable_path_gl;
                                          //  or (!S_ISDIR(fileinfo.st_mode))

  /* Otherwise, search PATH environment var.
     This code is a modified "which" shell builtin */
  pathcounter = path;
  while (*pathcounter != '\0' && found == 0) {
    len = 0;
    while (*pathcounter != PATH_SEPARATOR && *pathcounter != '\0') {
      len++;
      pathcounter++;
    }

    /* save the separator ':' (or ';' on NT and replace it with \0) */
    save = *pathcounter;
    *pathcounter = '\0';

    /* Now `len' holds the length of the PATH component 
       we are currently looking at.
       `pathcounter' points to the end of this component. */
    snprintf(executable_path_gl, MAXPATHLEN, "%s%c%s", pathcounter - len, SLASH, myname_augmented);

    /* restore the separator and addvance the pathcounter */
    *pathcounter = save;
    if (*pathcounter) pathcounter++;

#ifdef WIN_NT
    found = (0 == access(executable_path_gl, 02));	/* readable */
#else
    found = (0 == access(executable_path_gl, 01));	/* executable */
#endif
    if (found) return executable_path_gl;
  }

  /* XSB executable isn't found after searching PATH */
  if (xsb_mode != C_CALLING_XSB) {
    fprintf(stderr,
	  "*************************************************************\n");
    fprintf(stderr, 
	  "PANIC!!! Cannot determine the full name of the XSB executable!\n");
    fprintf(stderr, 
	  "Please report this problem using the XSB bug tracking system accessible from\n");
    fprintf(stderr, "\t http://sourceforge.net/projects/xsb\n");
    fprintf(stderr,
	    "*************************************************************\n");
    exit(1);
  }
  else { /* Dont want to exit when calling from C */
    xsb_initialization_exit("Cannot determine the full name of the XSB executable!\n"
		       "Please report this problem using the XSB bug tracking system accessible from\n"
		       "\t http://sourceforge.net/projects/xsb')\n");
  }
  /* This return is needed just to pacify the compiler */
  return FALSE;
}