c230060
--- dos2unix-3.1/dos2unix.c.tmppath	2004-10-20 16:00:00.342561008 +0200
c230060
+++ dos2unix-3.1/dos2unix.c	2004-10-20 16:01:42.210074792 +0200
c230060
@@ -69,6 +69,7 @@
c230060
 #ifdef __MSDOS__
c230060
 #  include <dir.h>
c230060
 #endif __MSDOS__
c230060
+#include <libgen.h>
c230060
 #include <stdio.h>
c230060
 #include <stdlib.h>
c230060
 #include <string.h>
c230060
@@ -267,6 +268,39 @@
c230060
     return RetVal;
c230060
 }
c230060
 
c230060
+static int MakeTempFileFrom(const char *OutFN, char **fname_ret)
c230060
+{
c230060
+  char *cpy = strdup(OutFN);
c230060
+  char *dir = NULL;
c230060
+  size_t fname_len = 0;
c230060
+  char  *fname_str = NULL;
c230060
+  int fd = -1;
c230060
+  
c230060
+  *fname_ret = NULL;
c230060
+  
c230060
+  if (!cpy)
c230060
+    goto make_failed;
c230060
+  
c230060
+  dir = dirname(cpy);
c230060
+  
e1129ee
+  fname_len = strlen(dir) + strlen("/d2utmpXXXXXX") + sizeof (char);
c230060
+  if (!(fname_str = malloc(fname_len)))
c230060
+    goto make_failed;
c230060
+  sprintf(fname_str, "%s%s", dir, "/d2utmpXXXXXX");
c230060
+  *fname_ret = fname_str;
c230060
+  
c230060
+  free(cpy);
c230060
+  
c230060
+  if ((fd = mkstemp(fname_str)) == -1)
c230060
+    goto make_failed;
c230060
+  
c230060
+  return (fd);
c230060
+  
c230060
+ make_failed:
c230060
+  free(*fname_ret);
c230060
+  *fname_ret = NULL;
c230060
+  return (-1);
c230060
+}
c230060
 
c230060
 /* convert file ipInFN to UNIX format text and write to file ipOutFN
c230060
  * RetVal: 0 if success
c230060
@@ -277,7 +311,7 @@
c230060
   int RetVal = 0;
c230060
   FILE *InF = NULL;
c230060
   FILE *TempF = NULL;
c230060
-  char TempPath[16];
c230060
+  char *TempPath;
c230060
   struct stat StatBuf;
c230060
   struct utimbuf UTimeBuf;
c230060
   int fd;
c230060
@@ -286,8 +320,7 @@
c230060
   if ((ipFlag->KeepDate) && stat(ipInFN, &StatBuf))
c230060
     RetVal = -1;
c230060
 
c230060
-  strcpy (TempPath, "./d2utmpXXXXXX");
c230060
-  if((fd=mkstemp (TempPath))<0) {
c230060
+  if((fd = MakeTempFileFrom(ipOutFN, &TempPath))<0) {
c230060
 	  perror("Failed to open output temp file");
c230060
 	  RetVal = -1;
c230060
   }
c230060
@@ -304,6 +337,7 @@
c230060
   if ((!RetVal) && (InF) && ((TempF=OpenOutFile(fd)) == NULL))
c230060
   {
c230060
     fclose (InF);
c230060
+    InF = NULL;
c230060
     RetVal = -1;
c230060
   }
c230060
 
c230060
@@ -337,9 +371,6 @@
c230060
   /* can rename temp file to out file? */
c230060
   if (!RetVal)
c230060
   {
c230060
-    if (stat(ipOutFN, &StatBuf) == 0)
c230060
-      unlink(ipOutFN);
c230060
-
c230060
     if ((rename(TempPath, ipOutFN) == -1) && (!ipFlag->Quiet))
c230060
     {
c230060
       fprintf(stderr, "dos2unix: problems renaming '%s' to '%s'\n", TempPath, ipOutFN);
c230060
@@ -347,6 +378,7 @@
c230060
       RetVal = -1;
c230060
     }
c230060
   }
c230060
+  free(TempPath);
c230060
   return RetVal;
c230060
 }
c230060
 
c230060
@@ -362,7 +394,7 @@
c230060
   int RetVal = 0;
c230060
   FILE *InF = NULL;
c230060
   FILE *TempF = NULL;
c230060
-  char TempPath[16];
c230060
+  char *TempPath;
c230060
   struct stat StatBuf;
c230060
   struct utimbuf UTimeBuf;
c230060
   mode_t mode = S_IRUSR | S_IWUSR;
c230060
@@ -374,8 +406,7 @@
c230060
   else
c230060
     mode = StatBuf.st_mode;
c230060
 
c230060
-  strcpy (TempPath, "./u2dtmpXXXXXX");
c230060
-  if((fd=mkstemp (TempPath))<0) {
c230060
+  if((fd = MakeTempFileFrom(ipInFN, &TempPath))<0) {
c230060
 	  perror("Failed to open output temp file");
c230060
 	  RetVal = -1;
c230060
   }
c230060
@@ -395,6 +426,7 @@
c230060
   if ((!RetVal) && (InF) && ((TempF=OpenOutFile(fd)) == NULL))
c230060
   {
c230060
     fclose (InF);
c230060
+    InF = NULL;
c230060
     RetVal = -1;
c230060
   }
c230060
 
c230060
@@ -422,10 +454,6 @@
c230060
       RetVal = -1;
c230060
   }
c230060
 
c230060
-  /* can delete in file? */
c230060
-  if ((!RetVal) && (unlink(ipInFN) == -1))
c230060
-    RetVal = -1;
c230060
-
c230060
   /* any error? */
c230060
   if ((RetVal) && (unlink(TempPath)))
c230060
     RetVal = -1;
c230060
@@ -440,6 +468,7 @@
c230060
     }
c230060
     RetVal = -1;
c230060
   }
c230060
+  free(TempPath);
c230060
   return RetVal;
c230060
 }
c230060