static char * find_executable_in_path(const char *name, const char *exclude_name, char *path) { path = x_strdup(path); // Search the path looking for the first compiler of the right name that // isn't us. char *saveptr = NULL; for (char *tok = strtok_r(path, PATH_DELIM, &saveptr); tok; tok = strtok_r(NULL, PATH_DELIM, &saveptr)) { #ifdef _WIN32 char namebuf[MAX_PATH]; int ret = SearchPath(tok, name, NULL, sizeof(namebuf), namebuf, NULL); if (!ret) { char *exename = format("%s.exe", name); ret = SearchPath(tok, exename, NULL, sizeof(namebuf), namebuf, NULL); free(exename); } (void) exclude_name; if (ret) { free(path); return x_strdup(namebuf); } #else struct stat st1, st2; char *fname = format("%s/%s", tok, name); // Look for a normal executable file. if (access(fname, X_OK) == 0 && lstat(fname, &st1) == 0 && stat(fname, &st2) == 0 && S_ISREG(st2.st_mode)) { if (S_ISLNK(st1.st_mode)) { char *buf = x_realpath(fname); if (buf) { char *p = basename(buf); if (str_eq(p, exclude_name)) { // It's a link to "ccache"! free(p); free(buf); continue; } free(buf); free(p); } } // Found it! free(path); return fname; } free(fname); #endif } free(path); return NULL; }
/* find an executable by name in $PATH. Exclude any that are links to exclude_name */ char *find_executable(const char *name, const char *exclude_name) { char *path; char *tok; struct stat st1, st2; if (*name == '/') { return x_strdup(name); } path = getenv("F90CACHE_PATH"); if (!path) { path = getenv("PATH"); } if (!path) { fc_log("no PATH variable!?\n"); return NULL; } path = x_strdup(path); /* search the path looking for the first compiler of the right name that isn't us */ for (tok=strtok(path,":"); tok; tok = strtok(NULL, ":")) { char *fname; x_asprintf(&fname, "%s/%s", tok, name); /* look for a normal executable file */ if (access(fname, X_OK) == 0 && lstat(fname, &st1) == 0 && stat(fname, &st2) == 0 && S_ISREG(st2.st_mode)) { /* if its a symlink then ensure it doesn't point at something called exclude_name */ if (S_ISLNK(st1.st_mode)) { char *buf = x_realpath(fname); if (buf) { char *p = str_basename(buf); if (strcmp(p, exclude_name) == 0) { /* its a link to "f90cache" ! */ free(p); free(buf); continue; } free(buf); free(p); } } /* found it! */ free(path); return fname; } free(fname); } return NULL; }
static int check_working_directory(CodeRunInstance *instance, const char *working_directory, char **p_fullpath_working_directory) { char *p; if(NULL == (p = x_realpath(working_directory))) { return 1; } *p_fullpath_working_directory = p; return 0; }
gchar * format_url (gchar *item, GFileTest test) { gchar *url, rpath[PATH_MAX], *p; p = strchr (item, ':'); if (!(p && p[1] == '/' && p[2] == '/')) { /* OK, so this is NOT an valid URL */ if (!x_realpath (item, rpath)) { return NULL; } if (!g_file_test (rpath, test)) { return NULL; } url = g_strdup_printf ("file://%s", rpath); } else { url = g_strdup (item); } return x_path2url (url); }
static int prepare_log_files(CodeRunInstance *instance, const char *datafilename_stdin, const char *logfilename_stdout, const char *logfilename_stderr, char **p_fullpath_datafile_stdin, char **p_fullpath_logfile_stdout, char **p_fullpath_logfile_stderr) { int fd; char *p; if(NULL != datafilename_stdin) { if(-1 == (fd = open(datafilename_stdin, O_RDONLY))) { RECORD_ERR("failed on attempting to open STDIN file", __FILE__, __LINE__); return 1; } if(-1 == close(fd)) { RECORD_ERR("failed on attempting to close STDIN file", __FILE__, __LINE__); return 2; } if(NULL == (p = x_realpath(datafilename_stdin))) { RECORD_ERR("failed on getting path of STDIN file", __FILE__, __LINE__); return 3; } *p_fullpath_datafile_stdin = p; } else { *p_fullpath_datafile_stdin = NULL; } if(NULL != logfilename_stdout) { if(-1 == (fd = open(logfilename_stdout, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP))) { RECORD_ERR("failed on attempting to open STDOUT file", __FILE__, __LINE__); return 11; } if(-1 == close(fd)) { RECORD_ERR("failed on attempting to close STDOUT file", __FILE__, __LINE__); return 12; } if(NULL == (p = x_realpath(logfilename_stdout))) { RECORD_ERR("failed on getting path of STDOUT file", __FILE__, __LINE__); return 13; } *p_fullpath_logfile_stdout = p; } else { *p_fullpath_logfile_stdout = NULL; } if(NULL != logfilename_stderr) { if( (NULL == logfilename_stdout) || (0 != strcmp(logfilename_stdout, logfilename_stderr)) ) { if(-1 == (fd = open(logfilename_stderr, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP))) { RECORD_ERR("failed on attempting to open STDERR file", __FILE__, __LINE__); return 21; } if(-1 == close(fd)) { RECORD_ERR("failed on attempting to close STDERR file", __FILE__, __LINE__); return 22; } } if(NULL == (p = x_realpath(logfilename_stderr))) { RECORD_ERR("failed on getting path of STDERR file", __FILE__, __LINE__); return 23; } *p_fullpath_logfile_stderr = p; } else { *p_fullpath_logfile_stderr = NULL; } return 0; }