applied MSVC compatibility patch from Dwight Kelly
[swftools.git] / lib / os.c
index eba887b..1554d35 100755 (executable)
--- a/lib/os.c
+++ b/lib/os.c
@@ -50,13 +50,13 @@ char* getRegistryEntry(char*path)
        fprintf(stderr, "RegOpenKeyEx failed\n");
        return 0;
     }
-    rc = RegQueryValueEx(key, NULL, 0, 0, 0, &size) ;
+    rc = RegQueryValueEx(key, NULL, 0, 0, 0, (LPDWORD)&size) ;
     if(rc != ERROR_SUCCESS) {
        fprintf(stderr, "RegQueryValueEx(1) failed: %d\n", rc);
        return 0;
     }
-    buf = malloc(size+1);
-    rc = RegQueryValueEx(key, NULL, 0, &type, (BYTE*)buf, &size);
+    buf = (char*)malloc(size+1);
+    rc = RegQueryValueEx(key, NULL, 0, &type, (BYTE*)buf, (LPDWORD)&size);
     if(rc != ERROR_SUCCESS) {
        fprintf(stderr, "RegQueryValueEx(2) failed: %d\n", rc);
        return 0;
@@ -101,7 +101,7 @@ char* getInstallationPath()
     if(path)
        return path;
     else
-       return "C:\\swftools";
+       return 0;
 #elif defined(CYGWIN)
     return SWFTOOLS_DATADIR;
 #else
@@ -109,7 +109,7 @@ char* getInstallationPath()
 #endif
 }
 
-char* concatPaths(char*base, char*add)
+char* concatPaths(const char*base, const char*add)
 {
     int l1 = strlen(base);
     int l2 = strlen(add);
@@ -120,18 +120,18 @@ char* concatPaths(char*base, char*add)
     while(pos < l2 && add[pos] == seperator)
        pos++;
 
-    n = malloc(l1 + (l2-pos) + 2);
+    n = (char*)malloc(l1 + (l2-pos) + 2);
     memcpy(n,base,l1);
     n[l1]=seperator;
     strcpy(&n[l1+1],&add[pos]);
     return n;
 }
 
-char* stripFilename(char*filename, char*newext)
+char* stripFilename(const char*filename, const char*newext)
 {
     char*last1 = strrchr(filename, '/');
     char*last2 = strrchr(filename, '\\');
-    char*pos = filename;
+    const char*pos = filename;
     char*name;
     char*dot;
     if(last1>pos) pos = last1 + 1;
@@ -147,3 +147,48 @@ char* stripFilename(char*filename, char*newext)
     return name;
 }
 
+static char* getTempDir()
+{
+#ifdef WIN32
+    char*dir = getenv("TMP");
+    if(!dir) dir = getenv("TEMP");
+    if(!dir) dir = getenv("tmp");
+    if(!dir) dir = getenv("temp");
+    if(!dir) dir = "C:\\";
+#else
+    char* dir = "/tmp/";
+#endif
+    return dir;
+}
+
+char* mktempname(char*ptr) {
+    static char tmpbuf[128];
+    char*dir = getTempDir();
+    int l = strlen(dir);
+    char*sep = "";
+    if(!ptr)
+       ptr = tmpbuf;
+    if(l && dir[l-1]!='/' && dir[l-1]!='\\') {
+#ifdef WIN32
+       sep = "\\";
+#else
+       sep = "/";
+#endif
+    }
+
+ //   used to be mktemp. This does remove the warnings, but
+ //   It's not exactly an improvement.
+#ifdef HAVE_LRAND48
+    sprintf(ptr, "%s%s%08x%08x",dir,sep,lrand48(),lrand48());
+#else
+#   ifdef HAVE_RAND
+       sprintf(ptr, "%s%s%08x%08x",dir,sep,rand(),rand());
+#   else
+       static int count = 1;
+       sprintf(ptr, "%s%s%08x%04x%04x",dir,sep,time(0),(unsigned int)tmpbuf^((unsigned int)tmpbuf)>>16,count);
+       count ++;
+#   endif
+#endif
+     return ptr;
+}
+