bugfix [#24462] UV Layouts saved as PNG results in two files (one is 0 KB, other...
[blender.git] / source / blender / blenlib / intern / path_util.c
index 092cca7..91558d4 100644 (file)
 
 #include "GHOST_Path-api.h"
 
+#if defined WIN32 && !defined _LIBC
+# include "BLI_fnmatch.h" /* use fnmatch included in blenlib */
+#else
+# define _GNU_SOURCE
+# include <fnmatch.h>
+#endif
 
 #ifdef WIN32
 #include <io.h>
@@ -311,10 +317,17 @@ void BLI_cleanup_file(const char *relabase, char *dir)
           dir[0]= '/';
           dir[1]= 0;
           return;
-       }       
+       }
+
+       /* support for odd paths: eg /../home/me --> /home/me
+        * this is a valid path in blender but we cant handle this the useual way below
+        * simply strip this prefix then evaluate the path as useual. pythons os.path.normpath() does this */
+       while((strncmp(dir, "/../", 4)==0)) {
+               memmove( dir, dir + 4, strlen(dir + 4) + 1 );
+       }
 
        while ( (start = strstr(dir, "/../")) ) {
-               eind = start + strlen("/../") - 1;
+               eind = start + (4 - 1) /* strlen("/../") - 1 */;
                a = start-dir-1;
                while (a>0) {
                        if (dir[a] == '/') break;
@@ -328,12 +341,12 @@ void BLI_cleanup_file(const char *relabase, char *dir)
        }
 
        while ( (start = strstr(dir,"/./")) ){
-               eind = start + strlen("/./") - 1;
+               eind = start + (3 - 1) /* strlen("/./") - 1 */;
                memmove( start, eind, strlen(eind)+1 );
        }
 
        while ( (start = strstr(dir,"//" )) ){
-               eind = start + strlen("//") - 1;
+               eind = start + (2 - 1) /* strlen("//") - 1 */;
                memmove( start, eind, strlen(eind)+1 );
        }
 
@@ -458,13 +471,9 @@ int BLI_has_parent(char *path)
 
 int BLI_parent_dir(char *path)
 {
-#ifdef WIN32
-       static char *parent_dir="..\\";
-#else
-       static char *parent_dir="../";
-#endif
+       static char parent_dir[]= {'.', '.', SEP, '\0'}; /* "../" or "..\\" */
        char tmp[FILE_MAXDIR+FILE_MAXFILE+4];
-       BLI_strncpy(tmp, path, sizeof(tmp));
+       BLI_strncpy(tmp, path, sizeof(tmp)-4);
        BLI_add_slash(tmp);
        strcat(tmp, parent_dir);
        BLI_cleanup_dir(NULL, tmp);
@@ -534,9 +543,20 @@ int BLI_path_frame(char *path, int frame, int digits)
                ensure_digits(path, digits);
 
        if (stringframe_chars(path, &ch_sta, &ch_end)) { /* warning, ch_end is the last # +1 */
-               char tmp[FILE_MAX], format[64];
+               char tmp[FILE_MAX];
+#if 0  // neat but breaks on non ascii strings.
+               char format[64];
                sprintf(format, "%%.%ds%%.%dd%%s", ch_sta, ch_end-ch_sta); /* example result: "%.12s%.5d%s" */
                sprintf(tmp, format, path, frame, path+ch_end);
+#else
+               char format[8];
+               char *p;
+               sprintf(format, "%%.%dd", ch_end-ch_sta); /* example result: "%.5d" */
+               memcpy(tmp, path, sizeof(char) * ch_sta);
+               p= tmp + ch_sta;
+               p += sprintf(p, format, frame);
+               memcpy(p, path + ch_end, strlen(path + ch_end));
+#endif
                strcpy(path, tmp);
                return 1;
        }
@@ -586,7 +606,7 @@ int BLI_path_abs(char *path, const char *basepath)
                BLI_strncpy(tmp, path, FILE_MAX);
        }
 #else
-       BLI_strncpy(tmp, path, FILE_MAX);
+       BLI_strncpy(tmp, path, sizeof(tmp));
        
        /* Check for loading a windows path on a posix system
         * in this case, there is no use in trying C:/ since it 
@@ -603,7 +623,7 @@ int BLI_path_abs(char *path, const char *basepath)
        
 #endif
 
-       BLI_strncpy(base, basepath, FILE_MAX);
+       BLI_strncpy(base, basepath, sizeof(base));
        
        BLI_cleanup_file(NULL, base);
        
@@ -626,17 +646,19 @@ int BLI_path_abs(char *path, const char *basepath)
                        BLI_strncpy(path, tmp+2, FILE_MAX);
                        
                        memcpy(tmp, base, baselen);
-                       strcpy(tmp+baselen, path);
-                       strcpy(path, tmp);
+                       BLI_strncpy(tmp+baselen, path, sizeof(tmp)-baselen);
+                       BLI_strncpy(path, tmp, FILE_MAX);
                } else {
-                       strcpy(path, tmp+2);
+                       BLI_strncpy(path, tmp+2, FILE_MAX);
                }
        } else {
-               strcpy(path, tmp);
+               BLI_strncpy(path, tmp, FILE_MAX);
        }
        
        if (path[0]!='\0') {
                if ( path[strlen(path)-1]=='/') {
+                       /* remove the '/' so we avoid BLI_cleanup_dir adding an extra \ in WIN32 */
+                       path[strlen(path)-1] = '\0';
                        BLI_cleanup_dir(NULL, path);
                } else {
                        BLI_cleanup_file(NULL, path);
@@ -732,184 +754,41 @@ void BLI_getlastdir(const char* dir, char *last, int maxlen)
        }
 }
 
-char *BLI_gethome(void) {
+/* This is now only used to really get the user's default document folder */
+/* On Windows I chose the 'Users/<MyUserName>/Documents' since it's used
+   as default location to save documents */
+char *BLI_getDefaultDocumentFolder(void) {
        #if !defined(WIN32)
                return getenv("HOME");
 
        #else /* Windows */
                char * ret;
-               static char dir[512];
-               static char appdatapath[MAXPATHLEN];
+               static char documentfolder[MAXPATHLEN];
                HRESULT hResult;
 
                /* Check for %HOME% env var */
 
                ret = getenv("HOME");
                if(ret) {
-                       sprintf(dir, "%s\\%s", ret, blender_version_decimal());
-                       if (BLI_is_dir(dir)) return dir;
+                       if (BLI_is_dir(ret)) return ret;
                }
-
-               /* else, check install dir (path containing blender.exe) */
-
-               if(BLI_getInstallationDir(dir))
-               {
-                       sprintf(dir, "%s", dir, blender_version_decimal());
-                       if (BLI_is_dir(dir)) return(dir);
-               }
-
                                
                /* add user profile support for WIN 2K / NT.
                 * This is %APPDATA%, which translates to either
                 * %USERPROFILE%\Application Data or since Vista
                 * to %USERPROFILE%\AppData\Roaming
                 */
-               hResult = SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appdatapath);
+               hResult = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, documentfolder);
                
                if (hResult == S_OK)
                {
-                       if (BLI_is_dir(appdatapath)) { /* from fop, also below... */
-                               sprintf(dir, "%s\\Blender Foundation\\Blender", appdatapath);
-                               BLI_recurdir_fileops(dir);
-                               if (BLI_is_dir(dir)) {
-                                       sprintf(dir,"%s\\%s", dir, blender_version_decimal());
-                                       if(BLI_is_dir(dir)) return(dir);
-                               }
-                       }
-                       hResult = SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, SHGFP_TYPE_CURRENT, appdatapath);
-                       if (hResult == S_OK)
-                       {
-                               if (BLI_is_dir(appdatapath)) 
-                               { /* from fop, also below... */
-                                       sprintf(dir, "%s\\Blender Foundation\\Blender", appdatapath);
-                                       BLI_recurdir_fileops(dir);
-                                       if (BLI_is_dir(dir)) {
-                                               sprintf(dir,"%s\\%s", dir, blender_version_decimal());
-                                               if(BLI_is_dir(dir)) return(dir);
-                                       }
-                               }
-                       }
+                       if (BLI_is_dir(documentfolder)) return documentfolder;
                }
                
-               return "C:\\Temp";      /* sheesh! bad, bad, bad! (aphex) */
+               return NULL;
        #endif
 }
 
-/* this function returns the path to a blender folder, if it exists
- * utility functions for BLI_gethome_folder */
-
-// #define PATH_DEBUG /* for testing paths that are checked */
-
-static int test_data_path(char *targetpath, char *path_base, char *path_sep, char *folder_name)
-{
-       char tmppath[FILE_MAXDIR];
-       
-       if(path_sep)    BLI_join_dirfile(tmppath, path_base, path_sep);
-       else                    BLI_strncpy(tmppath, path_base, sizeof(tmppath));
-       
-       BLI_make_file_string("/", targetpath, tmppath, folder_name);
-       
-       if (BLI_is_dir(targetpath)) {
-#ifdef PATH_DEBUG
-               printf("\tpath found: %s\n", targetpath);
-#endif
-               return 1;
-       }
-       else {
-#ifdef PATH_DEBUG
-               printf("\tpath missing: %s\n", targetpath);
-#endif
-               targetpath[0] = '\0';
-               return 0;
-       }
-}
-
-static int gethome_path_local(char *targetpath, char *folder_name)
-{
-       extern char bprogname[]; /* argv[0] from creator.c */
-       char bprogdir[FILE_MAXDIR];
-       char cwd[FILE_MAXDIR];
-       char *s;
-       int i;
-       
-#ifdef PATH_DEBUG
-       printf("gethome_path_local...\n");
-#endif
-       
-       /* try release/folder_name (binary relative) */
-       /* use argv[0] (bprogname) to get the path to the executable */
-       s = BLI_last_slash(bprogname);
-       i = s - bprogname + 1;
-       BLI_strncpy(bprogdir, bprogname, i);
-
-       /* try release/folder_name (BIN relative) */
-       if(test_data_path(targetpath, bprogdir, "release", folder_name))
-               return 1;
-
-       /* try release/folder_name (CWD relative) */
-       if(test_data_path(targetpath, BLI_getwdN(cwd), "release", folder_name))
-               return 1;
-
-       /* try ./.blender/folder_name */
-       if(test_data_path(targetpath, bprogdir, ".blender", folder_name))
-               return 1;
-       
-       return 0;
-}
-
-static int gethome_path_user(char *targetpath, char *folder_name)
-{
-       char *home_path= BLI_gethome();
-
-#ifdef PATH_DEBUG
-       printf("gethome_path_user...\n");
-#endif
-       
-       /* try $HOME/folder_name */
-       return test_data_path(targetpath, home_path, ".blender", folder_name);
-}
-
-static int gethome_path_system(char *targetpath, char *folder_name)
-{
-       extern char blender_path[]; /* unix prefix eg. /usr/share/blender/2.5 creator.c */
-       
-       if(!blender_path[0])
-               return 0;
-       
-#ifdef PATH_DEBUG
-       printf("gethome_path_system...\n");
-#endif
-       
-       /* try $BLENDERPATH/folder_name */
-       return test_data_path(targetpath, blender_path, NULL, folder_name);
-}
-
-char *BLI_gethome_folder(char *folder_name, int flag)
-{
-       static char fulldir[FILE_MAXDIR] = "";
-       
-       /* first check if this is a redistributable bundle */
-       if(flag & BLI_GETHOME_LOCAL) {
-               if (gethome_path_local(fulldir, folder_name))
-                       return fulldir;
-       }
-
-       /* then check if the OS has blender data files installed in a global location */
-       if(flag & BLI_GETHOME_SYSTEM) {
-               if (gethome_path_system(fulldir, folder_name))
-                       return fulldir;
-       }
-       
-       /* now check the users home dir for data files */
-       if(flag & BLI_GETHOME_USER) {
-               if (gethome_path_user(fulldir, folder_name))
-                       return fulldir;
-       }
-       
-       return NULL;
-}
-
-
 /* NEW stuff, to be cleaned up when fully migrated */
 /* ************************************************************* */
 /* ************************************************************* */
@@ -961,57 +840,48 @@ static int test_env_path(char *path, char *envvar)
        }
 }
 
-static int get_path_local(char *targetpath, char *folder_name)
+static int get_path_local(char *targetpath, char *folder_name, char *subfolder_name)
 {
        extern char bprogname[]; /* argv[0] from creator.c */
        char bprogdir[FILE_MAX];
-       char cwd[FILE_MAX];
-       char *s;
-       int i;
+       char relfolder[FILE_MAX];
        
 #ifdef PATH_DEBUG2
        printf("get_path_local...\n");
 #endif
        
+       if (subfolder_name) {
+               BLI_join_dirfile(relfolder, folder_name, subfolder_name);
+       } else {
+               BLI_strncpy(relfolder, folder_name, FILE_MAX);
+       }
        
        /* use argv[0] (bprogname) to get the path to the executable */
-       s = BLI_last_slash(bprogname);
-       i = s - bprogname + 1;
-       BLI_strncpy(bprogdir, bprogname, i);
+       BLI_split_dirfile(bprogname, bprogdir, NULL);
        
-       /* try EXECUTABLE_DIR/folder_name */
-       if(test_path(targetpath, bprogdir, "", folder_name))
-               return 1;
-       
-       /* try CWD/release/folder_name */
-       if(test_path(targetpath, BLI_getwdN(cwd), "release", folder_name))
-               return 1;
-       
-       /* try EXECUTABLE_DIR/release/folder_name */
-       if(test_path(targetpath, bprogdir, "release", folder_name))
-               return 1;
-       
-       /* try EXECUTABLE_DIR/2.5/folder_name - new default directory for local blender installed files */
-       if(test_path(targetpath, bprogdir, blender_version_decimal(), folder_name))
-               return 1;
-
-       /* try ./.blender/folder_name -- DEPRECATED, need to update build systems */
-       if(test_path(targetpath, bprogdir, ".blender", folder_name))
+       /* try EXECUTABLE_DIR/2.5x/folder_name - new default directory for local blender installed files */
+       if(test_path(targetpath, bprogdir, blender_version_decimal(), relfolder))
                return 1;
 
        return 0;
 }
 
-static int get_path_user(char *targetpath, char *folder_name, char *envvar)
+static int get_path_user(char *targetpath, char *folder_name, char *subfolder_name, char *envvar)
 {
        char user_path[FILE_MAX];
        const char *user_base_path;
        
        user_path[0] = '\0';
 
-       if (test_env_path(targetpath, envvar))
-               return 1;
-       
+       if (test_env_path(user_path, envvar)) {
+               if (subfolder_name) {
+                       return test_path(targetpath, user_path, NULL, subfolder_name);
+               } else {
+                       BLI_strncpy(targetpath, user_path, FILE_MAX);
+                       return 1;
+               }
+       }
+
        user_base_path = (const char *)GHOST_getUserDir();
        if (user_base_path) {
                BLI_snprintf(user_path, FILE_MAX, BLENDER_USER_FORMAT, user_base_path, blender_version_decimal());
@@ -1024,19 +894,58 @@ static int get_path_user(char *targetpath, char *folder_name, char *envvar)
        printf("get_path_user: %s\n", user_path);
 #endif
        
-       /* try $HOME/folder_name */
-       return test_path(targetpath, user_path, NULL, folder_name);
+       if (subfolder_name) {
+               /* try $HOME/folder_name/subfolder_name */
+               return test_path(targetpath, user_path, folder_name, subfolder_name);
+       } else {
+               /* try $HOME/folder_name */
+               return test_path(targetpath, user_path, NULL, folder_name);
+       }
 }
 
-static int get_path_system(char *targetpath, char *folder_name, char *envvar)
+static int get_path_system(char *targetpath, char *folder_name, char *subfolder_name, char *envvar)
 {
        char system_path[FILE_MAX];
        const char *system_base_path;
 
-       system_path[0] = '\0';
 
-       if (test_env_path(targetpath, envvar))
+       /* first allow developer only overrides to the system path
+        * these are only used when running blender from source */
+       extern char bprogname[]; /* argv[0] from creator.c */
+       char cwd[FILE_MAX];
+       char relfolder[FILE_MAX];
+       char bprogdir[FILE_MAX];
+
+       /* use argv[0] (bprogname) to get the path to the executable */
+       BLI_split_dirfile(bprogname, bprogdir, NULL);
+
+       if (subfolder_name) {
+               BLI_join_dirfile(relfolder, folder_name, subfolder_name);
+       } else {
+               BLI_strncpy(relfolder, folder_name, FILE_MAX);
+       }
+
+       /* try CWD/release/folder_name */
+       if(test_path(targetpath, BLI_getwdN(cwd), "release", relfolder))
                return 1;
+       
+       /* try EXECUTABLE_DIR/release/folder_name */
+       if(test_path(targetpath, bprogdir, "release", relfolder))
+               return 1;
+       /* end developer overrides */
+
+
+
+       system_path[0] = '\0';
+
+       if (test_env_path(system_path, envvar)) {
+               if (subfolder_name) {
+                       return test_path(targetpath, system_path, NULL, subfolder_name);
+               } else {
+                       BLI_strncpy(targetpath, system_path, FILE_MAX);
+                       return 1;
+               }
+       }
 
        system_base_path = (const char *)GHOST_getSystemDir();
        if (system_base_path) {
@@ -1050,8 +959,13 @@ static int get_path_system(char *targetpath, char *folder_name, char *envvar)
        printf("get_path_system: %s\n", system_path);
 #endif
        
-       /* try $BLENDERPATH/folder_name */
-       return test_path(targetpath, system_path, NULL, folder_name);
+       if (subfolder_name) {
+               /* try $BLENDERPATH/folder_name/subfolder_name */
+               return test_path(targetpath, system_path, folder_name, subfolder_name);
+       } else {
+               /* try $BLENDERPATH/folder_name */
+               return test_path(targetpath, system_path, NULL, folder_name);
+       }
 }
 
 /* get a folder out of the 'folder_id' presets for paths */
@@ -1059,91 +973,91 @@ static int get_path_system(char *targetpath, char *folder_name, char *envvar)
 char *BLI_get_folder(int folder_id, char *subfolder)
 {
        static char path[FILE_MAX] = "";
-       char search_path[FILE_MAX];
        
        switch (folder_id) {
                case BLENDER_DATAFILES:         /* general case */
-                       BLI_join_dirfile(search_path, "datafiles", subfolder);
-                       if (get_path_local(path, search_path)) break;
-                       if (get_path_user(path, search_path, "BLENDER_USER_DATAFILES")) break;
-                       if (get_path_system(path, search_path, "BLENDER_SYSTEM_DATAFILES")) break;
+                       if (get_path_local(path, "datafiles", subfolder)) break;
+                       if (get_path_user(path, "datafiles", subfolder, "BLENDER_USER_DATAFILES"))      break;
+                       if (get_path_system(path, "datafiles", subfolder, "BLENDER_SYSTEM_DATAFILES")) break;
                        return NULL;
                        
                case BLENDER_USER_DATAFILES:
-                       BLI_join_dirfile(search_path, "datafiles", subfolder);
-                       if (get_path_local(path, search_path)) break;
-                       if (get_path_user(path, search_path, "BLENDER_USER_DATAFILES")) break;
+                       if (get_path_local(path, "datafiles", subfolder)) break;
+                       if (get_path_user(path, "datafiles", subfolder, "BLENDER_USER_DATAFILES"))      break;
                        return NULL;
                        
                case BLENDER_SYSTEM_DATAFILES:
-                       BLI_join_dirfile(search_path, "datafiles", subfolder);
-                       if (get_path_system(path, search_path, "BLENDER_SYSTEM_DATAFILES"))     break;
+                       if (get_path_local(path, "datafiles", subfolder)) break;
+                       if (get_path_system(path, "datafiles", subfolder, "BLENDER_SYSTEM_DATAFILES"))  break;
                        return NULL;
                        
+               case BLENDER_USER_AUTOSAVE:
+                       if (get_path_local(path, "autosave", subfolder)) break;
+                       if (get_path_user(path, "autosave", subfolder, "BLENDER_USER_DATAFILES"))       break;
+                       return NULL;
+
                case BLENDER_CONFIG:            /* general case */
-                       BLI_join_dirfile(search_path, "config", subfolder);
-                       if (get_path_local(path, search_path)) break;
-                       if (get_path_user(path, search_path, "BLENDER_USER_CONFIG")) break;
-                       if (get_path_system(path, search_path, "BLENDER_SYSTEM_CONFIG")) break;
+                       if (get_path_local(path, "config", subfolder)) break;
+                       if (get_path_user(path, "config", subfolder, "BLENDER_USER_CONFIG")) break;
+                       if (get_path_system(path, "config", subfolder, "BLENDER_SYSTEM_CONFIG")) break;
                        return NULL;
                        
                case BLENDER_USER_CONFIG:
-                       BLI_join_dirfile(search_path, "config", subfolder);
-                       if (get_path_local(path, search_path)) break;
-                       if (get_path_user(path, search_path, "BLENDER_USER_CONFIG")) break;
+                       if (get_path_local(path, "config", subfolder)) break;
+                       if (get_path_user(path, "config", subfolder, "BLENDER_USER_CONFIG")) break;
                        return NULL;
                        
                case BLENDER_SYSTEM_CONFIG:
-                       BLI_join_dirfile(search_path, "config", subfolder);
-                       if (get_path_system(path, search_path, "BLENDER_SYSTEM_CONFIG")) break;
+                       if (get_path_local(path, "config", subfolder)) break;
+                       if (get_path_system(path, "config", subfolder, "BLENDER_SYSTEM_CONFIG")) break;
                        return NULL;
                        
                case BLENDER_SCRIPTS:           /* general case */
-                       BLI_join_dirfile(search_path, "scripts", subfolder);
-                       if (get_path_local(path, search_path)) break;
-                       if (get_path_user(path, search_path, "BLENDER_USER_SCRIPTS")) break;            
-                       if (get_path_system(path, search_path, "BLENDER_SYSTEM_SCRIPTS")) break;
+                       if (get_path_local(path, "scripts", subfolder)) break;
+                       if (get_path_user(path, "scripts", subfolder, "BLENDER_USER_SCRIPTS")) break;           
+                       if (get_path_system(path, "scripts", subfolder, "BLENDER_SYSTEM_SCRIPTS")) break;
                        return NULL;
                        
                case BLENDER_USER_SCRIPTS:
-                       BLI_join_dirfile(search_path, "scripts", subfolder);
-                       if (get_path_local(path, search_path)) break;
-                       if (get_path_user(path, search_path, "BLENDER_USER_SCRIPTS")) break;
+                       if (get_path_local(path, "scripts", subfolder)) break;
+                       if (get_path_user(path, "scripts", subfolder, "BLENDER_USER_SCRIPTS")) break;
                        return NULL;
                        
                case BLENDER_SYSTEM_SCRIPTS:
-                       BLI_join_dirfile(search_path, "scripts", subfolder);
-                       if (get_path_system(path, search_path, "BLENDER_SYSTEM_SCRIPTS")) break;
+                       if (get_path_local(path, "scripts", subfolder)) break;
+                       if (get_path_system(path, "scripts", subfolder, "BLENDER_SYSTEM_SCRIPTS")) break;
                        return NULL;
                        
                case BLENDER_PYTHON:            /* general case */
-                       BLI_join_dirfile(search_path, "python", subfolder);
-                       if (get_path_local(path, search_path)) break;
-                       if (get_path_system(path, search_path, "BLENDER_SYSTEM_PYTHON")) break;
+                       if (get_path_local(path, "python", subfolder)) break;
+                       if (get_path_system(path, "python", subfolder, "BLENDER_SYSTEM_PYTHON")) break;
                        return NULL;
                        
                case BLENDER_SYSTEM_PYTHON:
-                       BLI_join_dirfile(search_path, "python", subfolder);
-                       if (get_path_system(path, search_path, "BLENDER_SYSTEM_PYTHON")) break;
+                       if (get_path_local(path, "python", subfolder)) break;
+                       if (get_path_system(path, "python", subfolder, "BLENDER_SYSTEM_PYTHON")) break;
                        return NULL;
        }
        
        return path;
 }
 
-static char *BLI_get_user_folder_notest(int folder_id, char *subfolder)
+char *BLI_get_user_folder_notest(int folder_id, char *subfolder)
 {
        static char path[FILE_MAX] = "";
-       char search_path[FILE_MAX];
 
        switch (folder_id) {
                case BLENDER_USER_DATAFILES:
-                       BLI_join_dirfile(search_path, "datafiles", subfolder);
-                       get_path_user(path, search_path, "BLENDER_USER_DATAFILES");
+                       get_path_user(path, "datafiles", subfolder, "BLENDER_USER_DATAFILES");
                        break;
                case BLENDER_USER_CONFIG:
-                       BLI_join_dirfile(search_path, "config", subfolder);
-                       get_path_user(path, search_path, "BLENDER_USER_CONFIG");
+                       get_path_user(path, "config", subfolder, "BLENDER_USER_CONFIG");
+                       break;
+               case BLENDER_USER_AUTOSAVE:
+                       get_path_user(path, "autosave", subfolder, "BLENDER_USER_AUTOSAVE");
+                       break;
+               case BLENDER_USER_SCRIPTS:
+                       get_path_user(path, "scripts", subfolder, "BLENDER_USER_SCRIPTS");
                        break;
        }
        if ('\0' == path[0]) {
@@ -1157,7 +1071,7 @@ char *BLI_get_folder_create(int folder_id, char *subfolder)
        char *path;
 
        /* only for user folders */
-       if (!ELEM(folder_id, BLENDER_USER_DATAFILES, BLENDER_USER_CONFIG))
+       if (!ELEM4(folder_id, BLENDER_USER_DATAFILES, BLENDER_USER_CONFIG, BLENDER_USER_SCRIPTS, BLENDER_USER_AUTOSAVE))
                return NULL;
        
        path = BLI_get_folder(folder_id, subfolder);
@@ -1236,49 +1150,35 @@ void BLI_char_switch(char *string, char from, char to)
 void BLI_make_exist(char *dir) {
        int a;
 
-       #ifdef WIN32
-               BLI_char_switch(dir, '/', '\\');
-       #else
-               BLI_char_switch(dir, '\\', '/');
-       #endif  
-       
+       BLI_char_switch(dir, ALTSEP, SEP);
+
        a = strlen(dir);
-       
-#ifdef WIN32   
+
        while(BLI_is_dir(dir) == 0){
                a --;
-               while(dir[a] != '\\'){
+               while(dir[a] != SEP){
                        a--;
                        if (a <= 0) break;
                }
-               if (a >= 0) dir[a+1] = 0;
+               if (a >= 0) {
+                       dir[a+1] = '\0';
+               }
                else {
-                       /* defaulting to drive (usually 'C:') of Windows installation */
+#ifdef WIN32
                        get_default_root(dir);
-                       break;
-               }
-       }
 #else
-       while(BLI_is_dir(dir) == 0){
-               a --;
-               while(dir[a] != '/'){
-                       a--;
-                       if (a <= 0) break;
-               }
-               if (a >= 0) dir[a+1] = 0;
-               else {
                        strcpy(dir,"/");
+#endif
                        break;
                }
        }
-#endif
 }
 
 void BLI_make_existing_file(char *name)
 {
        char di[FILE_MAXDIR+FILE_MAXFILE], fi[FILE_MAXFILE];
 
-       strcpy(di, name);
+       BLI_strncpy(di, name, sizeof(di));
        BLI_splitdirstring(di, fi);
        
        /* test exist */
@@ -1312,8 +1212,7 @@ void BLI_make_file_string(const char *relabase, char *string,  const char *dir,
                /* Get the file name, chop everything past the last slash (ie. the filename) */
                strcpy(string, relabase);
                
-               lslash= (strrchr(string, '/')>strrchr(string, '\\'))?strrchr(string, '/'):strrchr(string, '\\');
-               
+               lslash= BLI_last_slash(string);
                if(lslash) *(lslash+1)= 0;
 
                dir+=2; /* Skip over the relative reference */
@@ -1381,6 +1280,49 @@ int BLI_testextensie(const char *str, const char *ext)
        return (retval);
 }
 
+int BLI_testextensie_array(const char *str, const char **ext_array)
+{
+       int i=0;
+       while(ext_array[i]) {
+               if(BLI_testextensie(str, ext_array[i])) {
+                       return 1;
+               }
+
+               i++;
+       }
+       return 0;
+}
+
+/* semicolon separated wildcards, eg:
+ *  '*.zip;*.py;*.exe' */
+int BLI_testextensie_glob(const char *str, const char *ext_fnmatch)
+{
+       const char *ext_step= ext_fnmatch;
+       char pattern[16];
+
+       while(ext_step[0]) {
+               char *ext_next;
+               int len_ext;
+
+               if((ext_next=strchr(ext_step, ';'))) {
+                       len_ext= (int)(ext_next - ext_step) + 1;
+               }
+               else {
+                       len_ext= sizeof(pattern);
+               }
+
+               BLI_strncpy(pattern, ext_step, len_ext);
+
+               if(fnmatch(pattern, str, FNM_CASEFOLD)==0) {
+                       return 1;
+               }
+               ext_step += len_ext;
+       }
+
+       return 0;
+}
+
+
 int BLI_replace_extension(char *path, int maxlen, const char *ext)
 {
        int a;
@@ -1546,6 +1488,67 @@ int BKE_rebase_path(char *abs, int abs_size, char *rel, int rel_size, const char
        return 1;
 }
 
+char *BLI_first_slash(char *string) {
+       char *ffslash, *fbslash;
+       
+       ffslash= strchr(string, '/');   
+       fbslash= strchr(string, '\\');
+       
+       if (!ffslash) return fbslash;
+       else if (!fbslash) return ffslash;
+       
+       if ((intptr_t)ffslash < (intptr_t)fbslash) return ffslash;
+       else return fbslash;
+}
+
+char *BLI_last_slash(const char *string) {
+       char *lfslash, *lbslash;
+       
+       lfslash= strrchr(string, '/');  
+       lbslash= strrchr(string, '\\');
+
+       if (!lfslash) return lbslash; 
+       else if (!lbslash) return lfslash;
+       
+       if ((intptr_t)lfslash < (intptr_t)lbslash) return lbslash;
+       else return lfslash;
+}
+
+/* adds a slash if there isnt one there already */
+int BLI_add_slash(char *string) {
+       int len = strlen(string);
+#ifdef WIN32
+       if (len==0 || string[len-1]!='\\') {
+               string[len] = '\\';
+               string[len+1] = '\0';
+               return len+1;
+       }
+#else
+       if (len==0 || string[len-1]!='/') {
+               string[len] = '/';
+               string[len+1] = '\0';
+               return len+1;
+       }
+#endif
+       return len;
+}
+
+/* removes a slash if there is one */
+void BLI_del_slash(char *string) {
+       int len = strlen(string);
+       while (len) {
+#ifdef WIN32
+               if (string[len-1]=='\\') {
+#else
+               if (string[len-1]=='/') {
+#endif
+                       string[len-1] = '\0';
+                       len--;
+               } else {
+                       break;
+               }
+       }
+}
 
 static int add_win32_extension(char *name)
 {
@@ -1658,7 +1661,7 @@ void BLI_where_am_i(char *fullname, const char *name)
                                } while (temp);
                        }
                }
-#ifndef NDEBUG
+#if defined(DEBUG)
                if (strcmp(name, fullname)) {
                        printf("guessing '%s' == '%s'\n", name, fullname);
                }
@@ -1671,7 +1674,7 @@ void BLI_where_am_i(char *fullname, const char *name)
                // with spawnv(P_WAIT, bprogname, argv) instead of system() but
                // that's even uglier
                GetShortPathName(fullname, fullname, FILE_MAXDIR+FILE_MAXFILE);
-#ifndef NDEBUG
+#if defined(DEBUG)
                printf("Shortname = '%s'\n", fullname);
 #endif
 #endif
@@ -1716,6 +1719,9 @@ void BLI_where_is_temp(char *fullname, int usertemp)
        } 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. */
+#endif
        }
 }