Fix T54152: --env-system-scripts fails on win32
authorRay Molenkamp <github@lazydodo.com>
Wed, 5 Sep 2018 04:31:10 +0000 (14:31 +1000)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 5 Sep 2018 04:32:19 +0000 (14:32 +1000)
source/blender/blenkernel/intern/appdir.c
source/blender/blenlib/BLI_path_util.h
source/blender/blenlib/intern/path_util.c
source/blender/blenlib/intern/storage.c
source/blender/blentranslation/intern/blt_lang.c
source/blender/editors/physics/physics_fluid.c
source/blender/editors/space_file/fsmenu.c
source/blender/imbuf/intern/colormanagement.c
source/blender/imbuf/intern/thumbs.c
source/blender/modifiers/intern/MOD_fluidsim_util.c

index 317994d4ed91aca8a4ca23e057a89fc52065d58d..8d6c34222d717a06ec9c97e1fc9b228a3f24a2dd 100644 (file)
@@ -69,12 +69,12 @@ static char btempdir_session[FILE_MAX] = "";  /* volatile temporary directory */
 const char *BKE_appdir_folder_default(void)
 {
 #ifndef WIN32
-       const char * const xdg_documents_dir = getenv("XDG_DOCUMENTS_DIR");
+       const char * const xdg_documents_dir = BLI_getenv("XDG_DOCUMENTS_DIR");
 
        if (xdg_documents_dir)
                return xdg_documents_dir;
 
-       return getenv("HOME");
+       return BLI_getenv("HOME");
 #else /* Windows */
        static char documentfolder[MAXPATHLEN];
        HRESULT hResult;
@@ -159,7 +159,7 @@ static bool test_path(
  */
 static bool test_env_path(char *path, const char *envvar)
 {
-       const char *env = envvar ? getenv(envvar) : NULL;
+       const char *env = envvar ? BLI_getenv(envvar) : NULL;
        if (!env) return false;
 
        if (BLI_is_dir(env)) {
@@ -236,6 +236,39 @@ static bool is_portable_install(void)
        return get_path_local(path, sizeof(path), "config", NULL, ver);
 }
 
+/**
+ * Returns the path of a folder from environment variables
+ *
+ * \param targetpath: String to return path.
+ * \param subfolder_name: optional name of subfolder within folder.
+ * \param envvar: name of environment variable to check folder_name.
+ * \return true if it was able to construct such a path.
+ */
+static bool get_path_environment(
+        char *targetpath,
+        size_t targetpath_len,
+        const char *subfolder_name,
+        const char *envvar)
+{
+       char user_path[FILE_MAX];
+
+       if (test_env_path(user_path, envvar)) {
+               if (subfolder_name) {
+                       return test_path(
+                               targetpath,
+                               targetpath_len,
+                               user_path,
+                               NULL,
+                               subfolder_name);
+               }
+               else {
+                       BLI_strncpy(targetpath, user_path, FILE_MAX);
+                       return true;
+               }
+       }
+       return false;
+}
+
 /**
  * Returns the path of a folder within the user-files area.
  *
@@ -243,13 +276,12 @@ static bool is_portable_install(void)
  * \param targetpath  String to return path
  * \param folder_name  default name of folder within user area
  * \param subfolder_name  optional name of subfolder within folder
- * \param envvar  name of environment variable which, if defined, overrides folder_name
- * \param ver  Blender version, used to construct a subdirectory name
+  * \param ver  Blender version, used to construct a subdirectory name
  * \return true if it was able to construct such a path.
  */
 static bool get_path_user(
         char *targetpath, size_t targetpath_len, const char *folder_name, const char *subfolder_name,
-        const char *envvar, const int ver)
+        const int ver)
 {
        char user_path[FILE_MAX];
        const char *user_base_path;
@@ -260,16 +292,6 @@ static bool get_path_user(
        }
        user_path[0] = '\0';
 
-       if (test_env_path(user_path, envvar)) {
-               if (subfolder_name) {
-                       return test_path(targetpath, targetpath_len, user_path, NULL, subfolder_name);
-               }
-               else {
-                       BLI_strncpy(targetpath, user_path, FILE_MAX);
-                       return true;
-               }
-       }
-
        user_base_path = (const char *)GHOST_getUserDir(ver, blender_version_decimal(ver));
        if (user_base_path)
                BLI_strncpy(user_path, user_base_path, FILE_MAX);
@@ -295,13 +317,12 @@ static bool get_path_user(
  * \param targetpath  String to return path
  * \param folder_name  default name of folder within installation area
  * \param subfolder_name  optional name of subfolder within folder
- * \param envvar  name of environment variable which, if defined, overrides folder_name
  * \param ver  Blender version, used to construct a subdirectory name
  * \return  true if it was able to construct such a path.
  */
 static bool get_path_system(
         char *targetpath, size_t targetpath_len, const char *folder_name, const char *subfolder_name,
-        const char *envvar, const int ver)
+        const int ver)
 {
        char system_path[FILE_MAX];
        const char *system_base_path;
@@ -320,17 +341,6 @@ static bool get_path_system(
        }
 
        system_path[0] = '\0';
-
-       if (test_env_path(system_path, envvar)) {
-               if (subfolder_name) {
-                       return test_path(targetpath, targetpath_len, system_path, NULL, subfolder_name);
-               }
-               else {
-                       BLI_strncpy(targetpath, system_path, FILE_MAX);
-                       return true;
-               }
-       }
-
        system_base_path = (const char *)GHOST_getSystemDir(ver, blender_version_decimal(ver));
        if (system_base_path)
                BLI_strncpy(system_path, system_base_path, FILE_MAX);
@@ -367,40 +377,49 @@ const char *BKE_appdir_folder_id_ex(
 
        switch (folder_id) {
                case BLENDER_DATAFILES:     /* general case */
-                       if (get_path_user(path, path_len, "datafiles", subfolder, "BLENDER_USER_DATAFILES", ver)) break;
+                       if (get_path_environment(path, path_len, subfolder, "BLENDER_USER_DATAFILES")) break;
+                       if (get_path_user(path, path_len, "datafiles", subfolder, ver)) break;
+                       if (get_path_environment(path, path_len, subfolder, "BLENDER_SYSTEM_DATAFILES")) break;
                        if (get_path_local(path, path_len, "datafiles", subfolder, ver)) break;
-                       if (get_path_system(path, path_len, "datafiles", subfolder, "BLENDER_SYSTEM_DATAFILES", ver)) break;
+                       if (get_path_system(path, path_len, "datafiles", subfolder, ver)) break;
                        return NULL;
 
                case BLENDER_USER_DATAFILES:
-                       if (get_path_user(path, path_len, "datafiles", subfolder, "BLENDER_USER_DATAFILES", ver)) break;
+                       if (get_path_environment(path, path_len, subfolder, "BLENDER_USER_DATAFILES")) break;
+                       if (get_path_user(path, path_len, "datafiles", subfolder, ver)) break;
                        return NULL;
 
                case BLENDER_SYSTEM_DATAFILES:
+                       if (get_path_environment(path, path_len, subfolder, "BLENDER_SYSTEM_DATAFILES")) break;
+                       if (get_path_system(path, path_len, "datafiles", subfolder, ver)) break;
                        if (get_path_local(path, path_len, "datafiles", subfolder, ver)) break;
-                       if (get_path_system(path, path_len, "datafiles", subfolder, "BLENDER_SYSTEM_DATAFILES", ver)) break;
                        return NULL;
 
                case BLENDER_USER_AUTOSAVE:
-                       if (get_path_user(path, path_len, "autosave", subfolder, "BLENDER_USER_DATAFILES", ver)) break;
+                       if (get_path_environment(path, path_len, subfolder, "BLENDER_USER_DATAFILES")) break;
+                       if (get_path_user(path, path_len, "autosave", subfolder, ver)) break;
                        return NULL;
 
                case BLENDER_USER_CONFIG:
-                       if (get_path_user(path, path_len, "config", subfolder, "BLENDER_USER_CONFIG", ver)) break;
+                       if (get_path_environment(path, path_len, subfolder, "BLENDER_USER_CONFIG")) break;
+                       if (get_path_user(path, path_len, "config", subfolder, ver)) break;
                        return NULL;
 
                case BLENDER_USER_SCRIPTS:
-                       if (get_path_user(path, path_len, "scripts", subfolder, "BLENDER_USER_SCRIPTS", ver)) break;
+                       if (get_path_environment(path, path_len, subfolder, "BLENDER_USER_SCRIPTS")) break;
+                       if (get_path_user(path, path_len, "scripts", subfolder, ver)) break;
                        return NULL;
 
                case BLENDER_SYSTEM_SCRIPTS:
+                       if (get_path_environment(path, path_len, subfolder, "BLENDER_SYSTEM_SCRIPTS")) break;
+                       if (get_path_system(path, path_len, "scripts", subfolder, ver)) break;
                        if (get_path_local(path, path_len, "scripts", subfolder, ver)) break;
-                       if (get_path_system(path, path_len, "scripts", subfolder, "BLENDER_SYSTEM_SCRIPTS", ver)) break;
                        return NULL;
 
                case BLENDER_SYSTEM_PYTHON:
+                       if (get_path_environment(path, path_len, subfolder, "BLENDER_SYSTEM_PYTHON")) break;
+                       if (get_path_system(path, path_len, "python", subfolder, ver)) break;
                        if (get_path_local(path, path_len, "python", subfolder, ver)) break;
-                       if (get_path_system(path, path_len, "python", subfolder, "BLENDER_SYSTEM_PYTHON", ver)) break;
                        return NULL;
 
                default:
@@ -428,16 +447,20 @@ const char *BKE_appdir_folder_id_user_notest(const int folder_id, const char *su
 
        switch (folder_id) {
                case BLENDER_USER_DATAFILES:
-                       get_path_user(path, sizeof(path), "datafiles", subfolder, "BLENDER_USER_DATAFILES", ver);
+                       if (get_path_environment(path, sizeof(path), subfolder, "BLENDER_USER_DATAFILES")) break;
+                       get_path_user(path, sizeof(path), "datafiles", subfolder, ver);
                        break;
                case BLENDER_USER_CONFIG:
-                       get_path_user(path, sizeof(path), "config", subfolder, "BLENDER_USER_CONFIG", ver);
+                       if (get_path_environment(path, sizeof(path), subfolder, "BLENDER_USER_CONFIG")) break;
+                       get_path_user(path, sizeof(path), "config", subfolder, ver);
                        break;
                case BLENDER_USER_AUTOSAVE:
-                       get_path_user(path, sizeof(path), "autosave", subfolder, "BLENDER_USER_AUTOSAVE", ver);
+                       if (get_path_environment(path, sizeof(path), subfolder, "BLENDER_USER_AUTOSAVE")) break;
+                       get_path_user(path, sizeof(path), "autosave", subfolder, ver);
                        break;
                case BLENDER_USER_SCRIPTS:
-                       get_path_user(path, sizeof(path), "scripts", subfolder, "BLENDER_USER_SCRIPTS", ver);
+                       if (get_path_environment(path, sizeof(path), subfolder, "BLENDER_USER_SCRIPTS")) break;
+                       get_path_user(path, sizeof(path), "scripts", subfolder, ver);
                        break;
                default:
                        BLI_assert(0);
@@ -481,13 +504,13 @@ const char *BKE_appdir_folder_id_version(const int folder_id, const int ver, con
        bool ok;
        switch (folder_id) {
                case BLENDER_RESOURCE_PATH_USER:
-                       ok = get_path_user(path, sizeof(path), NULL, NULL, NULL, ver);
+                       ok = get_path_user(path, sizeof(path), NULL, NULL, ver);
                        break;
                case BLENDER_RESOURCE_PATH_LOCAL:
                        ok = get_path_local(path, sizeof(path), NULL, NULL, ver);
                        break;
                case BLENDER_RESOURCE_PATH_SYSTEM:
-                       ok = get_path_system(path, sizeof(path), NULL, NULL, NULL, ver);
+                       ok = get_path_system(path, sizeof(path), NULL, NULL, ver);
                        break;
                default:
                        path[0] = '\0'; /* in case do_check is false */
@@ -743,7 +766,7 @@ static void where_is_temp(char *fullname, char *basename, const size_t maxlen, c
 
 #ifdef WIN32
        if (fullname[0] == '\0') {
-               const char *tmp = getenv("TEMP"); /* Windows */
+               const char *tmp = BLI_getenv("TEMP"); /* Windows */
                if (tmp && BLI_is_dir(tmp)) {
                        BLI_strncpy(fullname, tmp, maxlen);
                }
@@ -751,14 +774,14 @@ static void where_is_temp(char *fullname, char *basename, const size_t maxlen, c
 #else
        /* Other OS's - Try TMP and TMPDIR */
        if (fullname[0] == '\0') {
-               const char *tmp = getenv("TMP");
+               const char *tmp = BLI_getenv("TMP");
                if (tmp && BLI_is_dir(tmp)) {
                        BLI_strncpy(fullname, tmp, maxlen);
                }
        }
 
        if (fullname[0] == '\0') {
-               const char *tmp = getenv("TMPDIR");
+               const char *tmp = BLI_getenv("TMPDIR");
                if (tmp && BLI_is_dir(tmp)) {
                        BLI_strncpy(fullname, tmp, maxlen);
                }
index b4329bf81acd62cd39f65d46a6a390dea9387424..e434e83416b56839ad288fd864bd6dcb16e7e188 100644 (file)
@@ -41,6 +41,7 @@ struct ListBase;
 
 void BLI_setenv(const char *env, const char *val) ATTR_NONNULL(1);
 void BLI_setenv_if_new(const char *env, const char *val) ATTR_NONNULL(1);
+const char *BLI_getenv(const char *env) ATTR_NONNULL(1);
 
 void BLI_make_file_string(const char *relabase, char *string,  const char *dir, const char *file);
 void BLI_make_exist(char *dir);
index 6272f2109d2472852206ecd29c00a86728302913..b28579febaf71168fece03f62ab473cc1eb3bd09 100644 (file)
@@ -1095,7 +1095,7 @@ bool BLI_path_program_extensions_add_win32(char *name, const size_t maxlen)
        if ((type == 0) || S_ISDIR(type)) {
                /* typically 3-5, ".EXE", ".BAT"... etc */
                const int ext_max = 12;
-               const char *ext = getenv("PATHEXT");
+               const char *ext = BLI_getenv("PATHEXT");
                if (ext) {
                        const int name_len = strlen(name);
                        char *filename = alloca(name_len + ext_max);
@@ -1152,7 +1152,7 @@ bool BLI_path_program_search(
        const char separator = ':';
 #endif
 
-       path = getenv("PATH");
+       path = BLI_getenv("PATH");
        if (path) {
                char filename[FILE_MAX];
                const char *temp;
@@ -1220,10 +1220,27 @@ void BLI_setenv(const char *env, const char *val)
  */
 void BLI_setenv_if_new(const char *env, const char *val)
 {
-       if (getenv(env) == NULL)
+       if (BLI_getenv(env) == NULL)
                BLI_setenv(env, val);
 }
 
+/**
+* get an env var, result has to be used immediately
+*/
+const char* BLI_getenv(const char *env)
+{
+#ifdef _MSC_VER
+       static char buffer[32767]; /* 32767 is the total size of the environment block on windows*/
+       if (GetEnvironmentVariableA(env, buffer, sizeof(buffer)))
+               return buffer;
+       else
+               return NULL;
+#else
+       return getenv(env);
+#endif
+}
+
+
 /**
  * Strips off nonexistent (or non-accessible) subdirectories from the end of *dir, leaving the path of
  * the lowest-level directory that does exist and we can read.
index f69b35ce5a935de574f9e10181f8f84118f53b58..cfcc2bc439062536bbcf6a291a09682cc8293788 100644 (file)
@@ -85,7 +85,7 @@
  */
 char *BLI_current_working_dir(char *dir, const size_t maxncpy)
 {
-       const char *pwd = getenv("PWD");
+       const char *pwd = BLI_getenv("PWD");
        if (pwd) {
                size_t srclen = BLI_strnlen(pwd, maxncpy);
                if (srclen != maxncpy) {
index 916de3a5d20fb3ce9d5ff9811d9629f5de6c8ef8..f373ca248616dd0aa39524b63c47ac0f5b0c4a93 100644 (file)
@@ -214,7 +214,7 @@ void BLT_lang_init(void)
         *
         * Would also be good to find nicer way to check if LANG is correct.
         */
-       const char *lang = getenv("LANG");
+       const char *lang = BLI_getenv("LANG");
        if (lang != NULL) {
                char *old_locale = setlocale(LC_ALL, NULL);
                /* Make a copy so subsequenct setlocale() doesn't interfere. */
index 1cd2528b3ca510ecc3cf4db7b53e330ed6e4bba5..f960d6ed97b151f6d9d25c0360c93226987b7fe0 100644 (file)
@@ -42,6 +42,7 @@
 #include "DNA_object_fluidsim_types.h"
 
 #include "BLI_blenlib.h"
+#include "BLI_path_util.h"
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
 
@@ -868,8 +869,8 @@ static int fluidsimBake(bContext *C, ReportList *reports, Object *fsDomain, shor
 
        fb= MEM_callocN(sizeof(FluidBakeJob), "fluid bake job");
 
-       if (getenv(strEnvName)) {
-               int dlevel = atoi(getenv(strEnvName));
+       if (BLI_getenv(strEnvName)) {
+               int dlevel = atoi(BLI_getenv(strEnvName));
                elbeemSetDebugLevel(dlevel);
                BLI_snprintf(debugStrBuffer, sizeof(debugStrBuffer), "fluidsimBake::msg: Debug messages activated due to envvar '%s'\n", strEnvName);
                elbeemDebugOut(debugStrBuffer);
index baa8e78572afc2d8647cc0d49332853559142897..70df086b32da95898ad35d23c875773ac164cc3f 100644 (file)
@@ -577,7 +577,7 @@ void fsmenu_read_system(struct FSMenu *fsmenu, int read_bookmarks)
 #else
        /* unix */
        {
-               const char *home = getenv("HOME");
+               const char *home = BLI_getenv("HOME");
 
                if (read_bookmarks && home) {
                        BLI_snprintf(line, sizeof(line), "%s/", home);
index 86c0dd699303fbcabaf0fc24beefc95a68629aab..1d287025a4e751d2fff3844e65295fc801609bc3 100644 (file)
@@ -624,7 +624,7 @@ void colormanagement_init(void)
 
        OCIO_init();
 
-       ocio_env = getenv("OCIO");
+       ocio_env = BLI_getenv("OCIO");
 
        if (ocio_env && ocio_env[0] != '\0') {
                config = OCIO_configCreateFromEnv();
index bfb7a0418024c3ad012932b550ecac2114c76002..9d2b635bd7b142a73f6ba863b04f11fc6dd98837 100644 (file)
@@ -99,10 +99,10 @@ static bool get_thumb_dir(char *dir, ThumbSize size)
        s += strlen(dir);
 #else
 #if defined(USE_FREEDESKTOP)
-       const char *home_cache = getenv("XDG_CACHE_HOME");
-       const char *home = home_cache ? home_cache : getenv("HOME");
+       const char *home_cache = BLI_getenv("XDG_CACHE_HOME");
+       const char *home = home_cache ? home_cache : BLI_getenv("HOME");
 #else
-       const char *home = getenv("HOME");
+       const char *home = BLI_getenv("HOME");
 #endif
        if (!home) return 0;
        s += BLI_strncpy_rlen(s, home, FILE_MAX);
index e366116a5fb3512adb0957e72abba70be26be742..8dd4e2895037bfe75b99924ddde9090e8086a37c 100644 (file)
@@ -481,8 +481,8 @@ static DerivedMesh *fluidsim_read_cache(
                const char *strEnvName2 = "BLENDER_ELBEEMBOBJABORT"; // from blendercall.cpp
 
                if (G.background == 1) {
-                       if (getenv(strEnvName2)) {
-                               int elevel = atoi(getenv(strEnvName2));
+                       if (BLI_getenv(strEnvName2)) {
+                               int elevel = atoi(BLI_getenv(strEnvName2));
                                if (elevel > 0) {
                                        printf("Env. var %s set, fluid sim mesh '%s' not found, aborting render...\n",
                                               strEnvName2, targetFile);