void set_user_home() { char* ret = getcwd(current_dir_gl, MAXPATHLEN-1); user_home_gl = (char *) getenv("HOME"); if ( user_home_gl == NULL ) { user_home_gl = (char *) getenv("USERPROFILE"); /* often used in Windows */ if ( user_home_gl == NULL ) user_home_gl = current_dir_gl; } #ifdef WIN_NT transform_cygwin_pathname(user_home_gl); #endif }
/* 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; }
void set_config_file(void) { int retcode; struct stat fileinfo; /* The config file is in the lib directory at the same level as the xsb executable. */ xsb_config_file_gl = strip_names_from_path(executable_path_gl, 2); snprintf(xsb_config_file_gl+strlen(xsb_config_file_gl),(MAXPATHLEN-strlen(xsb_config_file_gl)), "%clib%cxsb_configuration%s", SLASH, SLASH,XSB_SRC_EXTENSION_STRING); /* Perform sanity checks: xsb_config_file must be in install_dir/config This is probably redundant */ if ( strncmp(install_dir_gl, xsb_config_file_gl, strlen(install_dir_gl)) != 0 || (strstr(xsb_config_file_gl, "config") == NULL) ) { if (xsb_mode != C_CALLING_XSB) { fprintf(stderr, "*************************************************************\n"); fprintf(stderr, "PANIC!! The file configuration%s\n", XSB_SRC_EXTENSION_STRING); fprintf(stderr, "is not where it is expected: %s%cconfig%c%s%clib\n", install_dir_gl, SLASH, SLASH, FULL_CONFIG_NAME, SLASH); fprintf(stderr, "Perhaps you moved the XSB executable %s\n", executable_path_gl); fprintf(stderr, "away from its usual place?\n"); fprintf(stderr, "*************************************************************\n"); exit(1); } else { xsb_initialization_exit("The file configuration%s\n" "is not where it is expected: %s%cconfig%c%s%clib\n" "Perhaps you moved the XSB executable %s\n", "away from its usual place?\n" XSB_SRC_EXTENSION_STRING,install_dir_gl, SLASH, SLASH, FULL_CONFIG_NAME, SLASH, executable_path_gl); } } /* Check if configuration.P exists and is readable */ retcode = stat(xsb_config_file_gl, &fileinfo); #ifdef WIN_NT if ( (retcode != 0) || !(S_IREAD & fileinfo.st_mode) ) { #else if ( (retcode != 0) || !(S_IRUSR & fileinfo.st_mode) ) { #endif if (xsb_mode != C_CALLING_XSB) { fprintf(stderr, "*************************************************************\n"); fprintf(stderr, "PANIC! XSB configuration file %s\n", xsb_config_file_gl); fprintf(stderr, "doesn't exist or is not readable by you.\n"); fprintf(stderr, "*************************************************************\n"); exit(1); } else { xsb_initialization_exit("XSB configuration file %s does not exist or is not readable by you.\n", xsb_config_file_gl); } } } #ifdef WIN_NT void transform_cygwin_pathname(char*); #endif void set_user_home() { user_home_gl = (char *) getenv("HOME"); if ( user_home_gl == NULL ) { user_home_gl = (char *) getenv("USERPROFILE"); /* often used in Windows */ if ( user_home_gl == NULL ) user_home_gl = install_dir_gl; } #ifdef WIN_NT transform_cygwin_pathname(user_home_gl); #endif }
/* 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; }