diff options
Diffstat (limited to 'source/blender/blenlib/intern/path_util.c')
-rw-r--r-- | source/blender/blenlib/intern/path_util.c | 92 |
1 files changed, 48 insertions, 44 deletions
diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index eae50b309b1..e44e3e92cdb 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -861,7 +861,7 @@ static int test_path(char *targetpath, const char *path_base, const char *path_s { char tmppath[FILE_MAX]; - if(path_sep) BLI_join_dirfile(tmppath, path_base, path_sep); + if(path_sep) BLI_join_dirfile(tmppath, sizeof(tmppath), path_base, path_sep); else BLI_strncpy(tmppath, path_base, sizeof(tmppath)); BLI_make_file_string("/", targetpath, tmppath, folder_name); @@ -905,9 +905,9 @@ static int get_path_local(char *targetpath, const char *folder_name, const char #endif if (subfolder_name) { - BLI_join_dirfile(relfolder, folder_name, subfolder_name); + BLI_join_dirfile(relfolder, sizeof(relfolder), folder_name, subfolder_name); } else { - BLI_strncpy(relfolder, folder_name, FILE_MAX); + BLI_strncpy(relfolder, folder_name, sizeof(relfolder)); } /* use argv[0] (bprogname) to get the path to the executable */ @@ -973,15 +973,18 @@ static int get_path_system(char *targetpath, const char *folder_name, const char BLI_split_dirfile(bprogname, bprogdir, NULL); if (subfolder_name) { - BLI_join_dirfile(relfolder, folder_name, subfolder_name); + BLI_join_dirfile(relfolder, sizeof(relfolder), folder_name, subfolder_name); } else { - BLI_strncpy(relfolder, folder_name, FILE_MAX); + BLI_strncpy(relfolder, folder_name, sizeof(relfolder)); } /* try CWD/release/folder_name */ - if(test_path(targetpath, BLI_getwdN(cwd, sizeof(cwd)), "release", relfolder)) - return 1; - + if(BLI_getwdN(cwd, sizeof(cwd))) { + if(test_path(targetpath, cwd, "release", relfolder)) { + return 1; + } + } + /* try EXECUTABLE_DIR/release/folder_name */ if(test_path(targetpath, bprogdir, "release", relfolder)) return 1; @@ -1420,12 +1423,12 @@ void BLI_split_dirfile(const char *string, char *dir, char *file) } /* simple appending of filename to dir, does not check for valid path! */ -void BLI_join_dirfile(char *string, const char *dir, const char *file) +void BLI_join_dirfile(char *string, const int maxlen, const char *dir, const char *file) { int sl_dir; if(string != dir) /* compare pointers */ - BLI_strncpy(string, dir, FILE_MAX); + BLI_strncpy(string, dir, maxlen); if (!file) return; @@ -1433,7 +1436,7 @@ void BLI_join_dirfile(char *string, const char *dir, const char *file) sl_dir= BLI_add_slash(string); if (sl_dir <FILE_MAX) { - BLI_strncpy(string + sl_dir, file, FILE_MAX-sl_dir); + BLI_strncpy(string + sl_dir, file, maxlen - sl_dir); } } @@ -1508,21 +1511,21 @@ int BKE_rebase_path(char *abs, int abs_size, char *rel, int rel_size, const char /* if image is _in_ current .blend file directory */ if (!strcmp(dir, blend_dir)) { - BLI_join_dirfile(dest_path, dest_dir, base); + BLI_join_dirfile(dest_path, sizeof(dest_path), dest_dir, base); } /* "below" */ else { /* rel = image_path_dir - blend_dir */ BLI_strncpy(rel_dir, dir + len, sizeof(rel_dir)); - BLI_join_dirfile(dest_path, dest_dir, rel_dir); - BLI_join_dirfile(dest_path, dest_path, base); + BLI_join_dirfile(dest_path, sizeof(dest_path), dest_dir, rel_dir); + BLI_join_dirfile(dest_path, sizeof(dest_path), dest_path, base); } } /* image is out of current directory */ else { - BLI_join_dirfile(dest_path, dest_dir, base); + BLI_join_dirfile(dest_path, sizeof(dest_path), dest_dir, base); } if (abs) @@ -1646,17 +1649,15 @@ static int add_win32_extension(char *name) } /* filename must be FILE_MAX length minimum */ -void BLI_where_am_i(char *fullname, const char *name) +void BLI_where_am_i(char *fullname, const int maxlen, const char *name) { char filename[FILE_MAXDIR+FILE_MAXFILE]; char *path = NULL, *temp; - + #ifdef _WIN32 const char *separator = ";"; - char slash = '\\'; #else const char *separator = ":"; - char slash = '/'; #endif @@ -1664,36 +1665,37 @@ void BLI_where_am_i(char *fullname, const char *name) /* linux uses binreloc since argv[0] is not relyable, call br_init( NULL ) first */ path = br_find_exe( NULL ); if (path) { - BLI_strncpy(fullname, path, FILE_MAXDIR+FILE_MAXFILE); + BLI_strncpy(fullname, path, maxlen); free(path); return; } #endif #ifdef _WIN32 - if(GetModuleFileName(0, fullname, FILE_MAXDIR+FILE_MAXFILE)) { - GetShortPathName(fullname, fullname, FILE_MAXDIR+FILE_MAXFILE); + if(GetModuleFileName(0, fullname, maxlen)) { + GetShortPathName(fullname, fullname, maxlen); return; } #endif /* unix and non linux */ - if (name && fullname && strlen(name)) { - strcpy(fullname, name); + if (name && name[0]) { + BLI_strncpy(fullname, name, maxlen); if (name[0] == '.') { - // relative path, prepend cwd - BLI_getwdN(fullname, FILE_MAX); - + char wdir[FILE_MAX]= ""; + BLI_getwdN(wdir, sizeof(wdir)); /* backup cwd to restore after */ + // not needed but avoids annoying /./ in name - if(name && name[0]=='.' && name[1]==slash) - BLI_join_dirfile(fullname, fullname, name+2); + if(name[1]==SEP) + BLI_join_dirfile(fullname, maxlen, wdir, name+2); else - BLI_join_dirfile(fullname, fullname, name); - - add_win32_extension(fullname); - } else if (BLI_last_slash(name)) { + BLI_join_dirfile(fullname, maxlen, wdir, name); + + add_win32_extension(fullname); /* XXX, doesnt respect length */ + } + else if (BLI_last_slash(name)) { // full path - strcpy(fullname, name); + BLI_strncpy(fullname, name, maxlen); add_win32_extension(fullname); } else { // search for binary in $PATH @@ -1708,9 +1710,9 @@ void BLI_where_am_i(char *fullname, const char *name) } else { strncpy(filename, path, sizeof(filename)); } - BLI_join_dirfile(fullname, fullname, name); + BLI_join_dirfile(fullname, maxlen, fullname, name); if (add_win32_extension(filename)) { - strcpy(fullname, filename); + BLI_strncpy(fullname, filename, maxlen); break; } } while (temp); @@ -1728,7 +1730,7 @@ void BLI_where_am_i(char *fullname, const char *name) // spaces and double-quotes. There's another solution to this // with spawnv(P_WAIT, bprogname, argv) instead of system() but // that's even uglier - GetShortPathName(fullname, fullname, FILE_MAXDIR+FILE_MAXFILE); + GetShortPathName(fullname, fullname, maxlen); #if defined(DEBUG) printf("Shortname = '%s'\n", fullname); #endif @@ -1736,12 +1738,12 @@ void BLI_where_am_i(char *fullname, const char *name) } } -void BLI_where_is_temp(char *fullname, int usertemp) +void BLI_where_is_temp(char *fullname, int maxlen, int usertemp) { fullname[0] = '\0'; if (usertemp && BLI_is_dir(U.tempdir)) { - strcpy(fullname, U.tempdir); + BLI_strncpy(fullname, U.tempdir, maxlen); } @@ -1749,7 +1751,7 @@ void BLI_where_is_temp(char *fullname, int usertemp) if (fullname[0] == '\0') { char *tmp = getenv("TEMP"); /* Windows */ if (tmp && BLI_is_dir(tmp)) { - strcpy(fullname, tmp); + BLI_strncpy((fullname, tmp, maxlen); } } #else @@ -1757,25 +1759,27 @@ void BLI_where_is_temp(char *fullname, int usertemp) if (fullname[0] == '\0') { char *tmp = getenv("TMP"); if (tmp && BLI_is_dir(tmp)) { - strcpy(fullname, tmp); + BLI_strncpy(fullname, tmp, maxlen); } } if (fullname[0] == '\0') { char *tmp = getenv("TMPDIR"); if (tmp && BLI_is_dir(tmp)) { - strcpy(fullname, tmp); + BLI_strncpy(fullname, tmp, maxlen); } } #endif if (fullname[0] == '\0') { - strcpy(fullname, "/tmp/"); + BLI_strncpy(fullname, "/tmp/", maxlen); } else { /* add a trailing slash if needed */ BLI_add_slash(fullname); #ifdef WIN32 - strcpy(U.tempdir, fullname); /* also set user pref to show %TEMP%. /tmp/ is just plain confusing for Windows users. */ + if(U.tempdir != fullname) { + BLI_strncpy(U.tempdir, fullname, maxlen); /* also set user pref to show %TEMP%. /tmp/ is just plain confusing for Windows users. */ + } #endif } } |