Fixing compiling on windows
[blender.git] / source / blender / blenlib / intern / path_util.c
index 06dfd62..1a930e7 100644 (file)
@@ -459,7 +459,7 @@ void BLI_path_rel(char *file, const char *relfile)
                char *p= temp;
                char *q= file;
 
-               while ((*p == *q)) {
+               while (*p == *q) {
                        ++p; ++q;
                        /* dont search beyond the end of the string
                         * in the rare case they match */
@@ -752,8 +752,8 @@ int BLI_path_cwd(char *path)
 #endif
        
        if (wasrelative==1) {
-               char cwd[FILE_MAXDIR + FILE_MAXFILE];
-               BLI_getwdN(cwd); /* incase the full path to the blend isnt used */
+               char cwd[FILE_MAXDIR + FILE_MAXFILE]= "";
+               BLI_getwdN(cwd, sizeof(cwd)); /* incase the full path to the blend isnt used */
                
                if (cwd[0] == '\0') {
                        printf( "Could not get the current working directory - $PWD for an unknown reason.");
@@ -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), "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;
@@ -1315,24 +1318,20 @@ void BLI_make_file_string(const char *relabase, char *string,  const char *dir,
        BLI_clean(string);
 }
 
-/* if ext is .blend*, it doesn't compare last char */
 int BLI_testextensie(const char *str, const char *ext)
 {
        short a, b;
-       int retval= 0;
+       int retval;
        
        a= strlen(str);
        b= strlen(ext);
        
        if(a==0 || b==0 || b>=a) {
                retval = 0;
-       }
-       else {
-               /* allow .blend1 .blend2 */
-               char *loc= BLI_strcasestr(str+a-b-1, ext);
-               
-               if(loc)
-                       retval= 1;
+       } else if (BLI_strcasecmp(ext, str + a - b)) {
+               retval = 0;     
+       } else {
+               retval = 1;
        }
        
        return (retval);
@@ -1424,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;
@@ -1437,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);
        }
 }
 
@@ -1512,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)
@@ -1649,17 +1648,16 @@ static int add_win32_extension(char *name)
        return (retval);
 }
 
-void BLI_where_am_i(char *fullname, const char *name)
+/* filename must be FILE_MAX length minimum */
+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
 
        
@@ -1667,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);
-                       
+                       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
@@ -1711,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);
@@ -1731,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
@@ -1739,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);
        }
        
        
@@ -1752,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
@@ -1760,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
        }
 }