fix up get_tmp_fd for windows
authorWolf Vollprecht <w.vollprecht@gmail.com>
Thu, 6 Jan 2022 08:03:03 +0000 (09:03 +0100)
committerWolf Vollprecht <w.vollprecht@gmail.com>
Fri, 7 Jan 2022 09:08:36 +0000 (10:08 +0100)
src/lib/win32/basename.c
src/lib/zck.c

index 0d6a8e5e391217403a11942a8880708fcea06260..61ae8a3bfe6d3e76d26c91ccf25651acb5e560e4 100644 (file)
@@ -3,6 +3,10 @@
 
 char* basename(char* path)
 {
+    // note this is not a proper basename implementation
+    char *p = strrchr (path, '\\');
+    return p ? p + 1 : (char *) path;
+
     // char full_path[MAX_PATH], drive[MAX_PATH], dir[MAX_PATH], filename[MAX_PATH], ext[MAX_PATH];
 
     // printf("Input: %s", path);
@@ -17,8 +21,6 @@ char* basename(char* path)
     // const char* res = malloc(MAX_PATH);
     // sprintf(res, "%s%s", filename, ext);
     // printf("Result: %s", res);
-    char *p = strrchr (path, '\\');
-    return p ? p + 1 : (char *) path;
 
     // return res;
 }
\ No newline at end of file
index a256dff0cc6997e88039efe746d05f2097eb89f0..3e6571b629eabbdd939ccd5f5eaf5d04c1ba090d 100644 (file)
@@ -147,7 +147,12 @@ int get_tmp_fd(zckCtx *zck) {
     int temp_fd;
     char *fname = NULL;
     char template[] = "zcktempXXXXXX";
+
+    #ifdef _WIN32
+    char *tmpdir = getenv("TEMP");
+    #else
     char *tmpdir = getenv("TMPDIR");
+    #endif
 
     if(tmpdir == NULL) {
         tmpdir = "/tmp/";
@@ -165,7 +170,11 @@ int get_tmp_fd(zckCtx *zck) {
     for(i=0; i<strlen(tmpdir); i++)
         fname[i] = tmpdir[i];
     int offset = i;
+    #ifdef _WIN32
+    fname[offset] = '\\';
+    #else
     fname[offset] = '/';
+    #endif
     offset++;
     for(i=0; i<strlen(template); i++)
         fname[offset + i] = template[i];
@@ -178,9 +187,9 @@ int get_tmp_fd(zckCtx *zck) {
     #ifdef _WIN32
     errno_t out = _mktemp_s(
         fname,
-        offset
+        offset + 1
     );
-    temp_fd = open(fname);
+    temp_fd = open(fname, O_CREAT | O_EXCL | O_RDWR | O_BINARY);
     #else
     old_mode_mask = umask (S_IXUSR | S_IRWXG | S_IRWXO);
     temp_fd = mkstemp(fname);
@@ -191,11 +200,14 @@ int get_tmp_fd(zckCtx *zck) {
         set_error(zck, "Unable to create temporary file");
         return -1;
     }
+#ifndef _WIN32
+    // Files with open file handle cannot be removed on Windows
     if(unlink(fname) < 0) {
         free(fname);
         set_error(zck, "Unable to delete temporary file");
         return -1;
     }
+#endif
     free(fname);
     return temp_fd;
 }