diff options
author | Campbell Barton <ideasman42@gmail.com> | 2015-10-08 07:05:58 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-10-08 07:05:58 +0300 |
commit | a5e631171bdcdb12f508528f391d3916014e23e8 (patch) | |
tree | 79cc6bb9d4d0c1425ac53d12496ebcba96c5ec21 /source/blender/blenlib | |
parent | e47177e301d104a8b7110e4490bf587c86835bad (diff) |
BLI_path api, minor changes to CWD handling
- BLI_current_working_dir's return value must be checked, since it may fail.
- BLI_current_working_dir now behaves like getcwd, where a too-small target will return failure.
- avoid buffer overrun with BLI_path_cwd, by taking a maxlen arg.
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_fileops.h | 2 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_path_util.h | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/path_util.c | 25 | ||||
-rw-r--r-- | source/blender/blenlib/intern/storage.c | 10 |
4 files changed, 18 insertions, 21 deletions
diff --git a/source/blender/blenlib/BLI_fileops.h b/source/blender/blenlib/BLI_fileops.h index 33dae45ac26..53ebc81fe22 100644 --- a/source/blender/blenlib/BLI_fileops.h +++ b/source/blender/blenlib/BLI_fileops.h @@ -89,7 +89,7 @@ bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); bool BLI_dir_create_recursive(const char *dir) ATTR_NONNULL(); double BLI_dir_free_space(const char *dir) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); -char *BLI_current_working_dir(char *dir, const size_t maxlen) ATTR_NONNULL(); +char *BLI_current_working_dir(char *dir, const size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(); /* Filelist */ diff --git a/source/blender/blenlib/BLI_path_util.h b/source/blender/blenlib/BLI_path_util.h index a344c9d2bc1..1be086cd96c 100644 --- a/source/blender/blenlib/BLI_path_util.h +++ b/source/blender/blenlib/BLI_path_util.h @@ -136,7 +136,7 @@ bool BLI_path_frame_range(char *path, int sta, int end, int digits) ATTR_NONNULL bool BLI_path_frame_get(char *path, int *r_frame, int *numdigits) ATTR_NONNULL(); void BLI_path_frame_strip(char *path, bool setsharp, char *ext) ATTR_NONNULL(); bool BLI_path_frame_check_chars(const char *path) ATTR_NONNULL(); -bool BLI_path_cwd(char *path) ATTR_NONNULL(); +bool BLI_path_cwd(char *path, const size_t maxlen) ATTR_NONNULL(); void BLI_path_rel(char *file, const char *relfile) ATTR_NONNULL(); bool BLI_path_is_rel(const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT; diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index 658c55cd75f..473ec1c67fa 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -1202,7 +1202,7 @@ bool BLI_path_abs(char *path, const char *basepath) * \note Should only be done with command line paths. * this is _not_ something blenders internal paths support like the "//" prefix */ -bool BLI_path_cwd(char *path) +bool BLI_path_cwd(char *path, const size_t maxlen) { bool wasrelative = true; const int filelen = strlen(path); @@ -1216,24 +1216,15 @@ bool BLI_path_cwd(char *path) #endif if (wasrelative) { - char cwd[FILE_MAX] = ""; - BLI_current_working_dir(cwd, sizeof(cwd)); /* in case the full path to the blend isn't used */ - - if (cwd[0] == '\0') { - printf("Could not get the current working directory - $PWD for an unknown reason.\n"); - } - else { - /* uses the blend path relative to cwd important for loading relative linked files. - * - * cwd should contain c:\ etc on win32 so the relbase can be NULL - * relbase being NULL also prevents // being misunderstood as relative to the current - * blend file which isn't a feature we want to use in this case since were dealing - * with a path from the command line, rather than from inside Blender */ - + char cwd[FILE_MAX]; + /* in case the full path to the blend isn't used */ + if (BLI_current_working_dir(cwd, sizeof(cwd))) { char origpath[FILE_MAX]; BLI_strncpy(origpath, path, FILE_MAX); - - BLI_make_file_string(NULL, path, cwd, origpath); + BLI_join_dirfile(path, maxlen, cwd, origpath); + } + else { + printf("Could not get the current working directory - $PWD for an unknown reason.\n"); } } diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c index 7fdf6ec8101..04d5e35355a 100644 --- a/source/blender/blenlib/intern/storage.c +++ b/source/blender/blenlib/intern/storage.c @@ -91,8 +91,14 @@ char *BLI_current_working_dir(char *dir, const size_t maxncpy) { const char *pwd = getenv("PWD"); if (pwd) { - BLI_strncpy(dir, pwd, maxncpy); - return dir; + size_t srclen = BLI_strnlen(pwd, maxncpy); + if (srclen != maxncpy) { + memcpy(dir, pwd, srclen); + return dir; + } + else { + return NULL; + } } return getcwd(dir, maxncpy); |