144 lines
3.4 KiB
Diff
144 lines
3.4 KiB
Diff
Index: dos2unix.c
|
|
===================================================================
|
|
--- dos2unix.c.orig
|
|
+++ dos2unix.c
|
|
@@ -69,6 +69,7 @@ static int macmode = 0;
|
|
#ifdef __MSDOS__
|
|
# include <dir.h>
|
|
#endif __MSDOS__
|
|
+#include <libgen.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
@@ -247,6 +248,39 @@ int ConvertDosToUnix(FILE* ipInF, FILE*
|
|
return RetVal;
|
|
}
|
|
|
|
+static int MakeTempFileFrom(const char *OutFN, char **fname_ret)
|
|
+{
|
|
+ char *cpy = strdup(OutFN);
|
|
+ char *dir = NULL;
|
|
+ size_t fname_len = 0;
|
|
+ char *fname_str = NULL;
|
|
+ int fd = -1;
|
|
+
|
|
+ *fname_ret = NULL;
|
|
+
|
|
+ if (!cpy)
|
|
+ goto make_failed;
|
|
+
|
|
+ dir = dirname(cpy);
|
|
+
|
|
+ fname_len = strlen(dir) + strlen("/d2utmpXXXXXX") + sizeof (char);
|
|
+ if (!(fname_str = malloc(fname_len)))
|
|
+ goto make_failed;
|
|
+ sprintf(fname_str, "%s%s", dir, "/d2utmpXXXXXX");
|
|
+ *fname_ret = fname_str;
|
|
+
|
|
+ free(cpy);
|
|
+
|
|
+ if ((fd = mkstemp(fname_str)) == -1)
|
|
+ goto make_failed;
|
|
+
|
|
+ return (fd);
|
|
+
|
|
+ make_failed:
|
|
+ free(*fname_ret);
|
|
+ *fname_ret = NULL;
|
|
+ return (-1);
|
|
+}
|
|
|
|
/* convert file ipInFN to UNIX format text and write to file ipOutFN
|
|
* RetVal: 0 if success
|
|
@@ -257,7 +291,7 @@ int ConvertDosToUnixNewFile(char *ipInFN
|
|
int RetVal = 0;
|
|
FILE *InF = NULL;
|
|
FILE *TempF = NULL;
|
|
- char TempPath[16];
|
|
+ char *TempPath;
|
|
struct stat StatBuf;
|
|
struct utimbuf UTimeBuf;
|
|
int fd;
|
|
@@ -266,8 +300,7 @@ int ConvertDosToUnixNewFile(char *ipInFN
|
|
if ((ipFlag->KeepDate) && stat(ipInFN, &StatBuf))
|
|
RetVal = -1;
|
|
|
|
- strcpy (TempPath, "./d2utmpXXXXXX");
|
|
- if((fd=mkstemp (TempPath))<0) {
|
|
+ if((fd = MakeTempFileFrom(ipOutFN, &TempPath))<0) {
|
|
perror("Failed to open output temp file");
|
|
RetVal = -1;
|
|
}
|
|
@@ -284,6 +317,7 @@ int ConvertDosToUnixNewFile(char *ipInFN
|
|
if ((!RetVal) && (InF) && ((TempF=OpenOutFile(fd)) == NULL))
|
|
{
|
|
fclose (InF);
|
|
+ InF = NULL;
|
|
RetVal = -1;
|
|
}
|
|
|
|
@@ -317,9 +351,6 @@ int ConvertDosToUnixNewFile(char *ipInFN
|
|
/* can rename temp file to out file? */
|
|
if (!RetVal)
|
|
{
|
|
- if (stat(ipOutFN, &StatBuf) == 0)
|
|
- unlink(ipOutFN);
|
|
-
|
|
if ((rename(TempPath, ipOutFN) == -1) && (!ipFlag->Quiet))
|
|
{
|
|
fprintf(stderr, "dos2unix: problems renaming '%s' to '%s'\n", TempPath, ipOutFN);
|
|
@@ -327,6 +358,7 @@ int ConvertDosToUnixNewFile(char *ipInFN
|
|
RetVal = -1;
|
|
}
|
|
}
|
|
+ free(TempPath);
|
|
return RetVal;
|
|
}
|
|
|
|
@@ -342,7 +374,7 @@ int ConvertDosToUnixOldFile(char* ipInFN
|
|
int RetVal = 0;
|
|
FILE *InF = NULL;
|
|
FILE *TempF = NULL;
|
|
- char TempPath[16];
|
|
+ char *TempPath;
|
|
struct stat StatBuf;
|
|
struct utimbuf UTimeBuf;
|
|
mode_t mode = S_IRUSR | S_IWUSR;
|
|
@@ -354,8 +386,7 @@ int ConvertDosToUnixOldFile(char* ipInFN
|
|
else
|
|
mode = StatBuf.st_mode;
|
|
|
|
- strcpy (TempPath, "./u2dtmpXXXXXX");
|
|
- if((fd=mkstemp (TempPath))<0) {
|
|
+ if((fd = MakeTempFileFrom(ipInFN, &TempPath))<0) {
|
|
perror("Failed to open output temp file");
|
|
RetVal = -1;
|
|
}
|
|
@@ -375,6 +406,7 @@ int ConvertDosToUnixOldFile(char* ipInFN
|
|
if ((!RetVal) && (InF) && ((TempF=OpenOutFile(fd)) == NULL))
|
|
{
|
|
fclose (InF);
|
|
+ InF = NULL;
|
|
RetVal = -1;
|
|
}
|
|
|
|
@@ -402,10 +434,6 @@ int ConvertDosToUnixOldFile(char* ipInFN
|
|
RetVal = -1;
|
|
}
|
|
|
|
- /* can delete in file? */
|
|
- if ((!RetVal) && (unlink(ipInFN) == -1))
|
|
- RetVal = -1;
|
|
-
|
|
/* any error? */
|
|
if ((RetVal) && (unlink(TempPath)))
|
|
RetVal = -1;
|
|
@@ -420,6 +448,7 @@ int ConvertDosToUnixOldFile(char* ipInFN
|
|
}
|
|
RetVal = -1;
|
|
}
|
|
+ free(TempPath);
|
|
return RetVal;
|
|
}
|
|
|