/* return phase path */ char * get_phase_dir (phases_t index) { #if defined(__linux__) || defined(__APPLE__) char *name = phase_info[index].name; if (external_gcc != TRUE) { /* Construct the path to the internal gcc binaries. */ if (strcmp(name, "gcc") == 0 || strcmp(name, "g++") == 0) { char *root_dir = directory_path(get_executable_dir()); char *bin_dir = concat_path(root_dir, INTERNAL_GCC_BIN); char *gcc_path = concat_path(bin_dir, name); if (is_executable(gcc_path)) { free(gcc_path); return bin_dir; } free(gcc_path); free(bin_dir); } } if (phase_info[index].find_dir_by_path) { char cmd[PATH_BUF_LEN]; char result[PATH_BUF_LEN]; sprintf(cmd, "dirname \"`which %s`\"", phase_info[index].name); if(read_cmd_out(cmd, result) == NULL) return phase_info[index].dir; return string_copy(result); } #endif return phase_info[index].dir; }
char *find_path_exec(char *exec, t_gen *envp) { char *path; char **split; int i; i = -1; if ((path = get_var_env("PATH=", envp->env)) == NULL) return (NULL); split = ft_strsplit(path, ':'); while (split[++i]) { path = concat_path(split[i], exec); if ((access(path, F_OK) != -1) && ((envp->ret = check_access(path)) == 0)) break ; else { envp->ret = check_access(path); ft_strdel(&path); if (envp->ret == ENPERM) break ; } } free_split(&split); return (path); }
bool append_clustering_result(char* outputFolder, int k, double score) { extern int nodesCount; bool success = TRUE; char* resultsPath; FILE * fp; concat_path(outputFolder, "results", &resultsPath); fp = fopen(resultsPath, "a"); if (fp == NULL) { success = FALSE; goto TERMINATE; } if (k > nodesCount) { /* infeasible clustering */ if (fprintf(fp, "Clustering with k=%d: infeasible\n", k) < 0) { success = FALSE; goto TERMINATE; } } else { if (fprintf(fp, "Clustering with k=%d: %1.3f\n", k, score) < 0) { success = FALSE; goto TERMINATE; } } TERMINATE: fclose(fp); free(resultsPath); return success; }
VMML_TEMPLATE_STRING void* VMML_TEMPLATE_CLASSNAME:: t3_allocate_rd_mmap( const std::string& dir_, const std::string& filename_ ) { //void * mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset); std::string path = concat_path( dir_, filename_ ); _fd = open( path.c_str(), O_RDONLY, 0 ); if ( _fd == -1 ) { { close(_fd); std::cout << "no file open for memory mapping" << std::endl; } } off_t offset = 0; _data = (void*)mmap( 0, _file_size, PROT_READ, MAP_FILE | MAP_SHARED, _fd, offset ); if( _data == MAP_FAILED) { std::cout << "mmap failed" << std::endl; return 0; } return _data; }
static void* dir_read(mount_t *mnt, const char *filename, int *size) { char buf[MAX_PATH]; /* Make fullpath */ int err = concat_path(buf, mnt->path, filename); if (err) { return NULL; } /* Open file */ FILE *fp = fopen(buf, "rb"); if (!fp) { return NULL; } /* Get size */ fseek(fp, 0, SEEK_END); *size = ftell(fp); fseek(fp, 0, SEEK_SET); /* Load data */ void *p = dmt_malloc(*size); if (!p) { return NULL; } fread(p, 1, *size, fp); /* Close file and return data */ fclose(fp); return p; }
char explorer_on_enter(t_tekpaint *tekpaint, t_explorer *explorer, int *i) { char *path; char *file_name; DIR *dirp; if ((file_name = get_focused_file_name(explorer, *i))) { path = concat_path(explorer->current_path, file_name); if (NULL == (dirp = opendir(path))) { closedir(dirp); if (explorer_on_enter_file(file_name, path, explorer)) return (1); } else { closedir(dirp); explorer->current_path = path; *i = 0; read_files(explorer); display_files(tekpaint, explorer, *i); } } else my_putstr("Ce fichier est incorrect !\n"); return (0); }
bool write_upper_bound_results(char* outputFolder, int upperBound, double weightIn, double weightOut, double* scores, int* diameters, int* clusterIds) { extern node* nodes; extern int nodesCount; extern edge* edges; extern int edgesCount; bool success = TRUE; char* resultsPath; FILE * fp; int i; concat_path(outputFolder, "results", &resultsPath); fp = fopen(resultsPath, "a"); if (fp == NULL) { success = FALSE; goto TERMINATE; } success = (fprintf(fp, "\nThe clustered network for k=%d:\n", upperBound) > 0); /* nodes list */ success = success && (fprintf(fp, "%d vertices:\n", nodesCount) > 0); for (i=0; (i<nodesCount) && success; i++) { success = success && (fprintf(fp, "%d: %s %d\n", i+1, nodes[i].name, nodes[i].clusterID + 1)); } /* edges list */ success = success && (fprintf(fp, "%d edges:\n", edgesCount)); for (i=0; (i<edgesCount) && success; i++) { success = success && (fprintf(fp, "%d: %s-%s %1.3f\n", i+1, nodes[edges[i].nodeFrom].name, nodes[edges[i].nodeTo].name, edges[i].weight)); } /* statistics output */ success = (fprintf(fp, "\nClustering statistics for %d:\n", upperBound) > 0); success = success && (fprintf(fp, "Average weight of an edge within clusters: %1.3f\n", weightIn) > 0); success = success && (fprintf(fp, "Average weight of an edge between clusters: %1.3f\n", weightOut) > 0); for (i=0; (i<upperBound) && success; i++) { fprintf(fp, "Cluster %d: score-%1.3f diameter-", i + 1, scores[clusterIds[i]]); if (diameters[clusterIds[i]] == INF_DIAMETER) { fprintf(fp, "inf\n"); } else { fprintf(fp, "%d\n", diameters[clusterIds[i]]); } } if (!success) { goto TERMINATE; } TERMINATE: fclose(fp); free(resultsPath); return success; }
bool read_data(char* inputFolder) { bool success; char* nodesPath = NULL; char* edgesPath = NULL; success = (concat_path(inputFolder, "nodes", &nodesPath) && concat_path(inputFolder, "edges", &edgesPath)); if (success) { success = success && read_nodes(nodesPath); success = success && read_edges(edgesPath); } free(nodesPath); free(edgesPath); return success; }
/*============================================= * concat_path_alloc -- add file & directory together into newly alloc'd string & return * dir: [IN] directory (may be NULL) * file: [IN] file (may be NULL) * See concat_path *===========================================*/ STRING concat_path_alloc (CNSTRING dir, CNSTRING file) { INT len = (dir ? strlen(dir) : 0) + (file ? strlen(file) : 0) +2; STRING buffer = malloc(len); INT myutf8=0; /* buffer is big enough, so won't matter */ return concat_path(dir, file, myutf8, buffer, len); }
VMML_TEMPLATE_STRING void* VMML_TEMPLATE_CLASSNAME:: t3_allocate_rd_mmap( const std::string& dir_, const std::string& filename_ ) { //void * mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset); std::string path = concat_path( dir_, filename_ ); #ifdef _WIN32 _fd = CreateFile( path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if( _fd == INVALID_HANDLE_VALUE ) { CloseHandle( _fd ); // FIXME shouldn't we return here? } off_t offset = 0; _fd_mmapped = ( void* )CreateFileMapping( _fd, 0, PAGE_READONLY, 0, 0, NULL ); // FIXME check. The fifth should be either 0 or _file_size if (_fd_mmapped == NULL ) { std::cout << "CreateFileMapping failed" << std::endl; return 0; } _data = MapViewOfFile( _fd_mmapped, FILE_MAP_READ, 0, 0, 0 ); if( _data == NULL ) { std::cout << "MapViewOfFile failed" << std::endl; return 0; } return _data; #else _fd = open( path.c_str(), O_RDONLY, 0 ); if ( _fd == -1 ) { { close(_fd); std::cout << "no file open for memory mapping" << std::endl; // FIXME shouldn't we return here? } } off_t offset = 0; _data = (void*)mmap( 0, _file_size, PROT_READ, MAP_FILE | MAP_SHARED, _fd, offset ); if( _data == MAP_FAILED) { std::cout << "mmap failed" << std::endl; return 0; } return _data; #endif }
char *home_path(const char *rel_path) { static char *home = NULL; if (!home) { home = getenv("HOME"); if (!home) home = "/"; } return concat_path(home, rel_path); }
static int concat_and_get_file_type(const char *dir, const char *filename) { char buf[MAX_PATH]; /* Make fullpath */ int err = concat_path(buf, dir, filename); if (err) { return err; } /* Stat */ return get_file_type(buf); }
// argv[1] - The current working directory // argv[2] - The name of the directory to create. int main(int argc, char **argv) { char *path = concat_path(argv[1], argv[2]); if (access(path, F_OK) == ERR) { mkdir(path, 0777); } else { fprintf(stderr, "Error: Cannot create directory \'%s\'.\n", argv[1]); exit(EXIT_FAILURE); } return 0; }
std::string getDataPath(std::string filename) { static std::string dataPath; if (dataPath.size()) return concat_path(dataPath, filename); const char *env_path = getenv("CRPROPA_DATA_PATH"); if (env_path) { if (is_directory(env_path)) { dataPath = env_path; KISS_LOG_INFO << "getDataPath: use environment variable, " << dataPath << std::endl; return concat_path(dataPath, filename); } } #ifdef CRPROPA_INSTALL_PREFIX { std::string _path = CRPROPA_INSTALL_PREFIX "/share/crpropa"; if (is_directory(_path)) { dataPath = _path; KISS_LOG_INFO << "getDataPath: use install prefix, " << dataPath << std::endl; return concat_path(dataPath, filename); } } #endif { std::string _path = executable_path() + "../data"; if (is_directory(_path)) { dataPath = _path; KISS_LOG_INFO << "getDataPath: use executable path, " << dataPath << std::endl; return concat_path(dataPath, filename); } } dataPath = "data"; KISS_LOG_INFO << "getDataPath: use default, " << dataPath << std::endl; return concat_path(dataPath, filename); }
/*================================================== * open_or_create_database -- open database, prompt for * creating new one if it doesn't exist * if fails, displays error (show_open_error) and returns * FALSE * alteration: [IN] flags for locking, forcing open... * dbused: [I/O] actual database path (may be relative) * If this routine creates new database, it will alter dbused * Created: 2001/04/29, Perry Rapp *================================================*/ BOOLEAN open_or_create_database (INT alteration, STRING *dbused) { INT lldberrnum=0; /* Open Database */ if (open_database(alteration, *dbused, &lldberrnum)) return TRUE; /* filter out real errors */ if (lldberrnum != BTERR_NODB && lldberrnum != BTERR_NOKEY) { show_open_error(lldberrnum); return FALSE; } if (readonly || immutable || alteration) { llwprintf(_("Cannot create new database with -r, -i, -l, or -f flags.")); return FALSE; } /* error was only that db doesn't exist, so lets try making a new one If no database directory specified, add prefix llnewdbdir */ if (is_unadorned_directory(*dbused)) { STRING dbpath = getlloptstr("LLDATABASES", "."); CNSTRING newdbdir = get_first_path_entry(dbpath); STRING temp = *dbused; if (newdbdir) { char tempth[MAXPATHLEN]; newdbdir = strdup(newdbdir); concat_path(newdbdir, *dbused, uu8, tempth, sizeof(tempth)); *dbused = strsave(tempth); stdfree(temp); stdfree((STRING)newdbdir); } } /* Is user willing to make a new db ? */ if (!ask_yes_or_no_msg(_(qSnodbse), _(qScrdbse))) return FALSE; /* try to make a new db */ if (create_database(*dbused, &lldberrnum)) return TRUE; show_open_error(lldberrnum); return FALSE; }
bool init_output_folder(char* outputFolder) { bool success = TRUE; FILE * fp; char* resultsPath; concat_path(outputFolder, "results", &resultsPath); /* open the 'results' file with the 'w' flag so that its content is reset */ fp = fopen(resultsPath, "w"); if (fp == NULL) { success = FALSE; goto TERMINATE; } TERMINATE: fclose(fp); free(resultsPath); return success; }
VMML_TEMPLATE_STRING void* VMML_TEMPLATE_CLASSNAME:: t3_allocate_wr_mmap( const std::string& dir_, const std::string& filename_, const C& tx_converter_ ) { //void * mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset); std::string path = concat_path( dir_, filename_ ); _fd = open( path.c_str(), O_RDWR, 0 ); if ( _fd == -1 ) { if ( ! _tensor ) { _tensor = new T(); } _tensor->zero(); tx_converter_.write_to_raw( *_tensor, dir_, filename_ ); _fd = open( path.c_str(), O_RDWR, 0 ); if ( _fd == -1 ) { close(_fd); std::cout << "no file open for memory mapping" << std::endl; } } off_t offset = 0; _data = (void*)mmap( 0, _file_size, PROT_WRITE, MAP_FILE | MAP_SHARED, _fd, offset ); if( _data == MAP_FAILED) { std::cout << "mmap failed" << std::endl; return 0; } return _data; }
static int make_dirs(const char *path) { char str[MAX_PATH]; char *p = str; int err = concat_path(str, path, ""); if (err) { return err; } if (p[0] == '/') p++; if (p[0] && p[1] == ':' && p[2] == '\\') p += 3; while (*p) { if (is_separator(*p)) { *p = '\0'; if (get_file_type(str) != FILESYSTEM_TDIR) { if (mkdir(str, S_IRWXU) == -1) { return FILESYSTEM_EMKDIRFAIL; } } *p = '/'; } p++; } return FILESYSTEM_ESUCCESS; }
VMML_TEMPLATE_STRING void* VMML_TEMPLATE_CLASSNAME:: t3_allocate_wr_mmap( const std::string& dir_, const std::string& filename_, const C& tx_converter_ ) { //void * mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset); std::string path = concat_path( dir_, filename_ ); #ifdef _WIN32 _fd = CreateFile( path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if( _fd == INVALID_HANDLE_VALUE ) { // Call GetLastError for more information if ( ! _tensor ) { _tensor = new T(); } _tensor->zero(); tx_converter_.write_to_raw( *_tensor, dir_, filename_ ); _fd = CreateFile( path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); if( _fd == INVALID_HANDLE_VALUE ) { CloseHandle( _fd ); std::cout << "no file open for memory mapping" << std::endl; // FIXME shouldn't we return here? } } off_t offset = 0; _fd_mmapped = ( void* )CreateFileMapping( _fd, 0, PAGE_READWRITE, 0, 0, NULL ); // FIXME check. The fifth should be either 0 or _file_size if (_fd_mmapped == NULL ) { std::cout << "CreateFileMapping failed" << std::endl; return 0; } _data = MapViewOfFile( _fd_mmapped, FILE_MAP_WRITE, 0, 0, 0 ); if( _data == NULL ) { std::cout << "MapViewOfFile failed" << std::endl; return 0; } return _data; #else _fd = open( path.c_str(), O_RDWR, 0 ); if ( _fd == -1 ) { if ( ! _tensor ) { _tensor = new T(); } _tensor->zero(); tx_converter_.write_to_raw( *_tensor, dir_, filename_ ); _fd = open( path.c_str(), O_RDWR, 0 ); if ( _fd == -1 ) { close(_fd); std::cout << "no file open for memory mapping" << std::endl; // FIXME shouldn't we return here? } } off_t offset = 0; _data = (void*)mmap( 0, _file_size, PROT_WRITE, MAP_FILE | MAP_SHARED, _fd, offset ); if( _data == MAP_FAILED) { std::cout << "mmap failed" << std::endl; return 0; } return _data; #endif }
static void test_concat_path (void) { char buffer[MAXPATHLEN]; STRING testpath; testpath = concat_path("hey", "jude", buffer, sizeof(buffer)); testpath = concat_path("hey", "/jude", buffer, sizeof(buffer)); testpath = concat_path("hey/", "jude", buffer, sizeof(buffer)); testpath = concat_path("hey/", "/jude", buffer, sizeof(buffer)); testpath = concat_path("hey", "jude", buffer, sizeof(buffer)); testpath = concat_path("hey", "\\jude", buffer, sizeof(buffer)); testpath = concat_path("hey/", "jude", buffer, sizeof(buffer)); testpath = concat_path("hey\\", "\\jude", buffer, sizeof(buffer)); testpath = concat_path(NULL, "\\jude", buffer, sizeof(buffer)); testpath = concat_path(NULL, "jude", buffer, sizeof(buffer)); testpath = concat_path("hey", NULL, buffer, sizeof(buffer)); testpath = concat_path("hey/", NULL, buffer, sizeof(buffer)); testpath = concat_path(NULL, NULL, buffer, sizeof(buffer)); testpath = concat_path("/", NULL, buffer, sizeof(buffer)); }
int main(int argc, char **argv) { program_name = argv[0]; char *test_dir = NULL; bool base_failures = false; bool retest_failures = false; bool show_output = false; char opt; while ((opt = getopt(argc, argv, "bfhot:")) != -1) { switch (opt) { case 'b': base_failures = true; break; case 'f': retest_failures = true; break; case 'o': show_output = true; break; case 'h': usage(); case 't': free(test_dir); test_dir = xstrdup(optarg); default: bad_usage(NULL); } } if (optind == argc) bad_usage("missing BUILD_DIR argument"); char *build_dir = argv[optind]; if (optind + 1 < argc) bad_usage("too many arguments"); if (base_failures && retest_failures) bad_usage("only one of -b or -f can be given"); if (!test_dir) test_dir = home_path(test_dir_path); if (chdir(build_dir)) perror_and_abort(); char *exec_args[9]; size_t n = 0; char *test_driver = concat_path(test_dir, "jstests.py"); exec_args[n++] = test_driver; exec_args[n++] = test_driver; exec_args[n++] = "--timeout=20"; if (retest_failures) { exec_args[n++] = xasprintf("--file=%s/%s", build_dir, "failures.txt"); } else if (base_failures){ exec_args[n++] = xasprintf("--failure-file=%s/%s", build_dir, "base_failures.txt"); } else { exec_args[n++] = xasprintf("--failure-file=%s/%s", build_dir, "failures.txt"); exec_args[n++] = xasprintf("--exclude-file=%s/%s", build_dir, "base_failures.txt"); } if (show_output) { exec_args[n++] = "-s"; exec_args[n++] = "-o"; } exec_args[n++] = xasprintf("%s/%s", build_dir, "js"); exec_args[n++] = NULL; assert(n <= sizeof(exec_args) / sizeof(exec_args[0])); execvp("python", exec_args); perror_and_abort(); }