char* path_prepend( char* tail, const char* base ) { if( !tail ) return path_clean( string_clone( base ), path_is_absolute( base ) ); tail = string_prepend( tail, "/" ); tail = string_prepend( tail, base ); tail = path_clean( tail, path_is_absolute( tail ) ); return tail; }
char* path_append( char* base, const char* tail ) { if( !base ) return path_clean( string_clone( tail ), path_is_absolute( tail ) ); base = string_append( base, "/" ); base = string_append( base, tail ); base = path_clean( base, path_is_absolute( base ) ); return base; }
void ps_default_search_args(cmd_ln_t *config) { #ifdef MODELDIR /* Set default acoustic and language models. */ const char *hmmdir = cmd_ln_str_r(config, "-hmm"); if (hmmdir == NULL && hmmdir_exists(MODELDIR "/en-us/en-us")) { hmmdir = MODELDIR "/en-us/en-us"; cmd_ln_set_str_r(config, "-hmm", hmmdir); } const char *lmfile = cmd_ln_str_r(config, "-lm"); if (lmfile == NULL && !cmd_ln_str_r(config, "-fsg") && !cmd_ln_str_r(config, "-jsgf") && !cmd_ln_str_r(config, "-lmctl") && !cmd_ln_str_r(config, "-kws") && !cmd_ln_str_r(config, "-keyphrase") && file_exists(MODELDIR "/en-us/en-us.lm.dmp")) { lmfile = MODELDIR "/en-us/en-us.lm.dmp"; cmd_ln_set_str_r(config, "-lm", lmfile); } const char *dictfile = cmd_ln_str_r(config, "-dict"); if (dictfile == NULL && file_exists(MODELDIR "/en-us/cmudict-en-us.dict")) { dictfile = MODELDIR "/en-us/cmudict-en-us.dict"; cmd_ln_set_str_r(config, "-dict", dictfile); } /* Expand acoustic and language model filenames relative to installation * path. */ if (hmmdir && !path_is_absolute(hmmdir) && !hmmdir_exists(hmmdir)) { char *tmphmm = string_join(MODELDIR "/hmm/", hmmdir, NULL); if (hmmdir_exists(tmphmm)) { cmd_ln_set_str_r(config, "-hmm", tmphmm); } else { E_ERROR("Failed to find mdef file inside the model folder " "specified with -hmm `%s'\n", hmmdir); } ckd_free(tmphmm); } if (lmfile && !path_is_absolute(lmfile) && !file_exists(lmfile)) { char *tmplm = string_join(MODELDIR "/lm/", lmfile, NULL); cmd_ln_set_str_r(config, "-lm", tmplm); ckd_free(tmplm); } if (dictfile && !path_is_absolute(dictfile) && !file_exists(dictfile)) { char *tmpdict = string_join(MODELDIR "/lm/", dictfile, NULL); cmd_ln_set_str_r(config, "-dict", tmpdict); ckd_free(tmpdict); } #endif }
int bin2hex_process_files( char const* const* input, char const* const* output, int columns ) { int result = BIN2HEX_RESULT_OK; unsigned int ifile, files_size; for( ifile = 0, files_size = array_size( input ); ( result == BIN2HEX_RESULT_OK ) && ( ifile < files_size ); ++ifile ) { char* input_filename = 0; char* output_filename = 0; stream_t* input_file = 0; stream_t* output_file = 0; input_filename = path_clean( string_clone( input[ifile] ), path_is_absolute( input[ifile] ) ); error_context_push( "parsing file", input_filename ); output_filename = path_clean( string_clone( output[ifile] ), path_is_absolute( output[ifile] ) ); log_infof( 0, "bin2hex %s -> %s", input_filename, output_filename ); input_file = stream_open( input_filename, STREAM_IN | STREAM_BINARY ); if( !input_file ) { log_warnf( 0, WARNING_BAD_DATA, "Unable to open input file: %s", input_filename ); result = BIN2HEX_RESULT_MISSING_INPUT_FILE; } else { output_file = stream_open( output_filename, STREAM_OUT ); if( !output_file ) { log_warnf( 0, WARNING_BAD_DATA, "Unable to open output file: %s", output_filename ); result = BIN2HEX_RESULT_UNABLE_TO_OPEN_OUTPUT_FILE; } } if( input_file && output_file ) result = bin2hex_process_file( input_file, output_file, columns ); stream_deallocate( input_file ); stream_deallocate( output_file ); string_deallocate( output_filename ); error_context_pop(); string_deallocate( input_filename ); } if( ( result == BIN2HEX_RESULT_OK ) && ( files_size > 0 ) ) log_info( 0, "All files generated" ); return result; }
static int append_access_mounts(MountEntry **p, char **strv, MountMode mode, bool forcibly_require_prefix) { char **i; assert(p); /* Adds a list of user-supplied READWRITE/READONLY/INACCESSIBLE entries */ STRV_FOREACH(i, strv) { bool ignore = false, needs_prefix = false; const char *e = *i; /* Look for any prefixes */ if (startswith(e, "-")) { e++; ignore = true; } if (startswith(e, "+")) { e++; needs_prefix = true; } if (!path_is_absolute(e)) return -EINVAL; *((*p)++) = (MountEntry) { .path_const = e, .mode = mode, .ignore = ignore, .has_prefix = !needs_prefix && !forcibly_require_prefix, }; } return 0; }
void Doom3MapCompiler::dmapCmd(const cmd::ArgumentList& args) { if (args.size() != 1) { rWarning() << "Usage: dmap <mapFile>" << std::endl; return; } std::string mapFile = args[0].getString(); if (!boost::algorithm::iends_with(mapFile, ".map")) { mapFile.append(".map"); } std::string mapPath = mapFile; // Find the map file if (!path_is_absolute(mapPath.c_str())) { mapPath = GlobalFileSystem().findFile(mapFile); if (mapPath.empty()) { // Try again with maps/ prepended mapFile = "maps/" + mapFile; mapPath = GlobalFileSystem().findFile(mapFile); } mapPath += mapFile; } // Start the sequence runDmap(mapPath); }
/* Parse a given config.ini file and extract the list of SDK search paths * from it. Returns the number of valid paths stored in 'searchPaths', or -1 * in case of problem. * * Relative search paths in the config.ini will be stored as full pathnames * relative to 'sdkRootPath'. * * 'searchPaths' must be an array of char* pointers of at most 'maxSearchPaths' * entries. */ static int _getSearchPaths( IniFile* configIni, const char* sdkRootPath, int maxSearchPaths, char** searchPaths ) { char temp[PATH_MAX], *p = temp, *end= p+sizeof temp; int nn, count = 0; for (nn = 0; nn < maxSearchPaths; nn++) { char* path; p = bufprint(temp, end, "%s%d", SEARCH_PREFIX, nn+1 ); if (p >= end) continue; path = iniFile_getString(configIni, temp, NULL); if (path != NULL) { DD(" found image search path: %s", path); if (!path_is_absolute(path)) { p = bufprint(temp, end, "%s/%s", sdkRootPath, path); AFREE(path); path = ASTRDUP(temp); } searchPaths[count++] = path; } } return count; }
static int add_root_mount(void) { _cleanup_free_ char *what = NULL; const char *opts; if (isempty(arg_root_what)) { log_debug("Could not find a root= entry on the kernel command line."); return 0; } what = fstab_node_to_udev_node(arg_root_what); if (!path_is_absolute(what)) { log_debug("Skipping entry what=%s where=/sysroot type=%s", what, strna(arg_root_fstype)); return 0; } if (!arg_root_options) opts = arg_root_rw > 0 ? "rw" : "ro"; else if (arg_root_rw >= 0 || !fstab_test_option(arg_root_options, "ro\0" "rw\0")) opts = strjoina(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro"); else opts = arg_root_options; log_debug("Found entry what=%s where=/sysroot type=%s", what, strna(arg_root_fstype)); return add_mount(what, "/sysroot", arg_root_fstype, opts, 1, false, false, false, SPECIAL_INITRD_ROOT_FS_TARGET, "/proc/cmdline"); }
int path_make_absolute_cwd(const char *p, char **ret) { char *c; assert(p); assert(ret); /* Similar to path_make_absolute(), but prefixes with the * current working directory. */ if (path_is_absolute(p)) c = strdup(p); else { _cleanup_free_ char *cwd = NULL; cwd = get_current_dir_name(); if (!cwd) return negative_errno(); c = strjoin(cwd, "/", p, NULL); } if (!c) return -ENOMEM; *ret = c; return 0; }
char *path_make_absolute_cwd(const char *p) { char *cwd, *r; size_t plen; size_t cwdlen; if (path_is_absolute(p)) return strdup(p); cwd = get_current_dir_name(); if (cwd == NULL) return NULL; plen = strlen(p); cwdlen = strlen(cwd); /* cwd + '/' + p + '\0' */ r = realloc(cwd, cwdlen + 1 + plen + 1); if (r == NULL) { free(cwd); return NULL; } r[cwdlen] = '/'; memcpy(&r[cwdlen + 1], p, plen + 1); return r; }
static int condition_test_needs_update(Condition *c) { const char *p; struct stat usr, other; assert(c); assert(c->parameter); assert(c->type == CONDITION_NEEDS_UPDATE); /* If the file system is read-only we shouldn't suggest an update */ if (path_is_read_only_fs(c->parameter) > 0) return false; /* Any other failure means we should allow the condition to be true, * so that we rather invoke too many update tools then too * few. */ if (!path_is_absolute(c->parameter)) return true; p = strjoina(c->parameter, "/.updated"); if (lstat(p, &other) < 0) return true; if (lstat("/usr/", &usr) < 0) return true; return usr.st_mtim.tv_sec > other.st_mtim.tv_sec || (usr.st_mtim.tv_sec == other.st_mtim.tv_sec && usr.st_mtim.tv_nsec > other.st_mtim.tv_nsec); }
int cg_fix_path(const char *path, char **result) { char *t, *c, *p; int r; assert(path); assert(result); /* First check if it already is a filesystem path */ if (path_is_absolute(path) && path_startswith(path, "/sys/fs/cgroup") && access(path, F_OK) >= 0) { if (!(t = strdup(path))) return -ENOMEM; *result = t; return 0; } /* Otherwise treat it as cg spec */ if ((r = cg_split_spec(path, &c, &p)) < 0) return r; r = cg_get_path(c ? c : SYSTEMD_CGROUP_CONTROLLER, p ? p : "/", NULL, result); free(c); free(p); return r; }
/* Function: al_make_path_absolute */ bool al_make_path_absolute(ALLEGRO_PATH *path) { ALLEGRO_PATH *cwd_path; int i; ASSERT(path); if (path_is_absolute(path)) { return true; } cwd_path = al_get_current_directory(); if (!cwd_path) return false; al_set_path_drive(path, al_get_path_drive(cwd_path)); for (i = al_get_path_num_components(cwd_path) - 1; i >= 0; i--) { al_insert_path_component(path, 0, al_get_path_component(cwd_path, i)); } al_destroy_path(cwd_path); return true; }
int get_home_dir(char **_h) { struct passwd *p; const char *e; char *h; uid_t u; assert(_h); /* Take the user specified one */ e = secure_getenv("HOME"); if (e && path_is_absolute(e)) { h = strdup(e); if (!h) return -ENOMEM; *_h = h; return 0; } /* Hardcode home directory for root to avoid NSS */ u = getuid(); if (u == 0) { h = strdup("/root"); if (!h) return -ENOMEM; *_h = h; return 0; } /* Check the database... */ errno = 0; p = getpwuid(u); if (!p) return errno > 0 ? -errno : -ESRCH; if (!path_is_absolute(p->pw_dir)) return -EINVAL; h = strdup(p->pw_dir); if (!h) return -ENOMEM; *_h = h; return 0; }
char* path_subdirectory_name( const char* path, const char* root ) { char* subpath; char* testpath; char* testroot; char* pathofpath; unsigned int pathprotocol, rootprotocol; char* cpath = string_clone( path ); char* croot = string_clone( root ); cpath = path_clean( cpath, path_is_absolute( cpath ) ); croot = path_clean( croot, path_is_absolute( croot ) ); pathofpath = path_directory_name( cpath ); testpath = pathofpath; pathprotocol = string_find_string( testpath, "://", 0 ); if( pathprotocol != STRING_NPOS ) testpath += pathprotocol + 2; // add two to treat as absolute path testroot = croot; rootprotocol = string_find_string( testroot, "://", 0 ); if( rootprotocol != STRING_NPOS ) testroot += rootprotocol + 2; if( ( rootprotocol != STRING_NPOS ) && ( ( pathprotocol == STRING_NPOS ) || ( pathprotocol != rootprotocol ) || !string_equal_substr( cpath, croot, rootprotocol ) ) ) subpath = string_allocate( 0 ); else if( !string_equal_substr( testpath, testroot, string_length( testroot ) ) ) subpath = string_allocate( 0 ); else { char* filename = path_file_name( cpath ); subpath = string_substr( testpath, string_length( testroot ), STRING_NPOS ); subpath = path_clean( path_append( subpath, filename ), false ); string_deallocate( filename ); } string_deallocate( pathofpath ); string_deallocate( cpath ); string_deallocate( croot ); return subpath; }
static int add_usr_mount(void) { _cleanup_free_ char *what = NULL; const char *opts; if (!arg_usr_what && !arg_usr_fstype && !arg_usr_options) return 0; if (arg_root_what && !arg_usr_what) { arg_usr_what = strdup(arg_root_what); if (!arg_usr_what) return log_oom(); } if (arg_root_fstype && !arg_usr_fstype) { arg_usr_fstype = strdup(arg_root_fstype); if (!arg_usr_fstype) return log_oom(); } if (arg_root_options && !arg_usr_options) { arg_usr_options = strdup(arg_root_options); if (!arg_usr_options) return log_oom(); } if (!arg_usr_what) return 0; what = fstab_node_to_udev_node(arg_usr_what); if (!path_is_absolute(what)) { log_debug("Skipping entry what=%s where=/sysroot/usr type=%s", what, strna(arg_usr_fstype)); return -1; } if (!arg_usr_options) opts = arg_root_rw > 0 ? "rw" : "ro"; else if (!fstab_test_option(arg_usr_options, "ro\0" "rw\0")) opts = strjoina(arg_usr_options, ",", arg_root_rw > 0 ? "rw" : "ro"); else opts = arg_usr_options; log_debug("Found entry what=%s where=/sysroot/usr type=%s", what, strna(arg_usr_fstype)); return add_mount(what, "/sysroot/usr", arg_usr_fstype, opts, 1, false, false, false, SPECIAL_INITRD_ROOT_FS_TARGET, "/proc/cmdline"); }
char* path_get_absolute( const char* path ) { if (path_is_absolute(path)) { return ASTRDUP(path); } #ifdef _WIN32 { char* result; int pathLen = strlen(path); int currentLen = GetCurrentDirectory(0, NULL); if (currentLen <= 0) { /* Could not get size of working directory. something is * really fishy here, return a simple copy */ return ASTRDUP(path); } result = malloc(currentLen + pathLen + 2); GetCurrentDirectory(currentLen+1, result); if (currentLen == 0 || result[currentLen-1] != '\\') { result[currentLen++] = '\\'; } memcpy(result + currentLen, path, pathLen+1); return result; } #else { int pathLen = strlen(path); char currentDir[PATH_MAX]; int currentLen; char* result; if (getcwd(currentDir, sizeof(currentDir)) == NULL) { /* Could not get the current working directory. something is really * fishy here, so don't do anything and return a copy */ return ASTRDUP(path); } /* Make a new path with <current-path>/<path> */ currentLen = strlen(currentDir); result = malloc(currentLen + pathLen + 2); memcpy(result, currentDir, currentLen); if (currentLen == 0 || result[currentLen-1] != '/') { result[currentLen++] = '/'; } memcpy(result + currentLen, path, pathLen+1); return result; } #endif }
void button_clicked_entry_browse_directory( GtkWidget* widget, GtkEntry* entry ){ const char* text = gtk_entry_get_text( entry ); char *dir = dir_dialog( gtk_widget_get_toplevel( widget ), "Choose Directory", path_is_absolute( text ) ? text : "" ); if ( dir != 0 ) { gchar* converted = g_filename_to_utf8( dir, -1, 0, 0, 0 ); gtk_entry_set_text( entry, converted ); g_free( dir ); g_free( converted ); } }
char *path_make_absolute(const char *p, const char *prefix) { assert(p); /* Makes every item in the list an absolute path by prepending * the prefix, if specified and necessary */ if (path_is_absolute(p) || !prefix) return strdup(p); return strjoin(prefix, "/", p, NULL); }
static int add_matches(sd_journal *j, char **args) { char **i; int r; assert(j); STRV_FOREACH(i, args) { if (streq(*i, "+")) r = sd_journal_add_disjunction(j); else if (path_is_absolute(*i)) { char *p; const char *path; struct stat st; p = canonicalize_file_name(*i); path = p ? p : *i; if (stat(path, &st) < 0) { free(p); log_error("Couldn't stat file: %m"); return -errno; } if (S_ISREG(st.st_mode) && (0111 & st.st_mode)) { char *t; t = strappend("_EXE=", path); if (!t) { free(p); log_error("Out of memory"); return -ENOMEM; } r = sd_journal_add_match(j, t, 0); free(t); } else { free(p); log_error("File is not a regular file or is not executable: %s", *i); return -EINVAL; } free(p); } else r = sd_journal_add_match(j, *i, 0); if (r < 0) { log_error("Failed to add match '%s': %s", *i, strerror(-r)); return r; } } return 0; }
/** * fill_pathname_resolve_relative: * @out_path : output path * @in_refpath : input reference path * @in_path : input path * @size : size of @out_path * * Joins basedir of @in_refpath together with @in_path. * If @in_path is an absolute path, out_path = in_path. * E.g.: in_refpath = "/foo/bar/baz.a", in_path = "foobar.cg", * out_path = "/foo/bar/foobar.cg". **/ void fill_pathname_resolve_relative(char *out_path, const char *in_refpath, const char *in_path, size_t size) { if (path_is_absolute(in_path)) { retro_assert(strlcpy(out_path, in_path, size) < size); return; } fill_pathname_basedir(out_path, in_refpath, size); retro_assert(strlcat(out_path, in_path, size) < size); }
void fill_pathname_resolve_relative(char *out_path, const char *in_refpath, const char *in_path, size_t size) { if (path_is_absolute(in_path)) rarch_assert(strlcpy(out_path, in_path, size) < size); else { rarch_assert(strlcpy(out_path, in_refpath, size) < size); path_basedir(out_path); rarch_assert(strlcat(out_path, in_path, size) < size); } }
static ALLEGRO_USTR *apply_cwd(const char *path) { ALLEGRO_USTR *us; if (path_is_absolute(path)) { return al_ustr_new(path); } us = al_ustr_new(fs_apk_cwd); al_ustr_append_cstr(us, path); return us; }
int label_mkdir( const char *path, mode_t mode) { /* Creates a directory and labels it according to the SELinux policy */ #ifdef HAVE_SELINUX int r; security_context_t fcon = NULL; if (use_selinux() && label_hnd) { if (path_is_absolute(path)) r = selabel_lookup_raw(label_hnd, &fcon, path, mode); else { char *newpath = NULL; if (!(newpath = path_make_absolute_cwd(path))) return -ENOMEM; r = selabel_lookup_raw(label_hnd, &fcon, newpath, mode); free(newpath); } if (r == 0) r = setfscreatecon(fcon); if (r < 0 && errno != ENOENT) { log_error("Failed to set security context %s for %s: %m", fcon, path); r = -errno; if (security_getenforce() == 1) goto finish; } } if ((r = mkdir(path, mode)) < 0) r = -errno; finish: if (use_selinux() && label_hnd) { setfscreatecon(NULL); freecon(fcon); } return r; #else return mkdir(path, mode); #endif }
char* path_base_file_name_with_path( const char* path ) { unsigned int start, end; char* base; if( !path ) return string_allocate( 0 ); start = string_find_last_of( path, "/\\", STRING_NPOS ); end = string_rfind( path, '.', STRING_NPOS ); //For "dot" files, i.e files with names like "/path/to/.file", return the dot name ".file" if( !end || ( end == ( start + 1 ) ) || ( ( start != STRING_NPOS ) && ( end < start ) ) ) end = STRING_NPOS; base = string_substr( path, 0, ( end != STRING_NPOS ) ? end : STRING_NPOS ); base = path_clean( base, path_is_absolute( base ) ); return base; }
/* TODO: Put in shared source file */ static char* _getFullFilePath( const char* rootPath, const char* fileName ) { if (path_is_absolute(fileName)) { return ASTRDUP(fileName); } else { char temp[PATH_MAX], *p=temp, *end=p+sizeof(temp); p = bufprint(temp, end, "%s/%s", rootPath, fileName); if (p >= end) { return NULL; } return ASTRDUP(temp); } }
char* path_merge( const char* first, const char* second ) { char* merged; if( !first ) merged = string_clone( second ); else if( !second ) merged = string_clone( first ); else { merged = string_format( "%s/", first ); merged = string_append( merged, second ); } merged = path_clean( merged, path_is_absolute( first ) ); return merged; }
std::string PrefabSelector::getPrefabFolder() { std::string customPath = _customPath->getEntryWidget()->GetValue().ToStdString(); // Only search in custom paths if it is absolute if (_useCustomPath->GetValue() && !customPath.empty() && path_is_absolute(customPath.c_str())) { return customPath; } else { // No custom path return PREFAB_FOLDER; } }
int cg_join_spec(const char *controller, const char *path, char **spec) { assert(controller); assert(path); if (!path_is_absolute(path) || controller[0] == 0 || strchr(controller, ':') || strchr(controller, '/')) return -EINVAL; if (asprintf(spec, "%s:%s", controller, path) < 0) return -ENOMEM; return 0; }
char *path_make_absolute_cwd(const char *p) { _cleanup_free_ char *cwd = NULL; assert(p); /* Similar to path_make_absolute(), but prefixes with the * current working directory. */ if (path_is_absolute(p)) return strdup(p); cwd = get_current_dir_name(); if (!cwd) return NULL; return strjoin(cwd, "/", p, NULL); }