Fix temporary file name creation.
Use the original name as template for readability in the map file, and use the process PID instead of platform-dependant or deprecated random filename functions to make unique temporary filenames. Also, create these temporary files in the output directory.
This commit is contained in:
@@ -122,6 +122,11 @@ static int DoAssemble = 1;
|
|||||||
/* The name of the output file, NULL if none given */
|
/* The name of the output file, NULL if none given */
|
||||||
static const char* OutputName = 0;
|
static const char* OutputName = 0;
|
||||||
|
|
||||||
|
/* The path part of the output file, NULL if none given
|
||||||
|
** or the OutputName is just a filename with no path
|
||||||
|
** information. */
|
||||||
|
static char *OutputDirectory = 0;
|
||||||
|
|
||||||
/* The name of the linker configuration file if given */
|
/* The name of the linker configuration file if given */
|
||||||
static const char* LinkerConfig = 0;
|
static const char* LinkerConfig = 0;
|
||||||
|
|
||||||
@@ -555,7 +560,7 @@ static void AssembleFile (const char* File, const char* TmpFile, unsigned ArgCou
|
|||||||
if (TmpFile) {
|
if (TmpFile) {
|
||||||
ObjName = MakeFilename (TmpFile, ".o");
|
ObjName = MakeFilename (TmpFile, ".o");
|
||||||
} else {
|
} else {
|
||||||
ObjName = MakeTmpFilename (".o");
|
ObjName = MakeTmpFilename (OutputDirectory, File, ".o");
|
||||||
}
|
}
|
||||||
CmdSetOutput (&CA65, ObjName);
|
CmdSetOutput (&CA65, ObjName);
|
||||||
CmdAddFile (&LD65, ObjName);
|
CmdAddFile (&LD65, ObjName);
|
||||||
@@ -684,7 +689,7 @@ static void Compile (const char* File)
|
|||||||
|
|
||||||
if (DoAssemble) {
|
if (DoAssemble) {
|
||||||
/* set a temporary output file name */
|
/* set a temporary output file name */
|
||||||
TmpFile = MakeTmpFilename(".s");
|
TmpFile = MakeTmpFilename(OutputDirectory, File, ".s");
|
||||||
CmdSetOutput (&CC65, TmpFile);
|
CmdSetOutput (&CC65, TmpFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -729,7 +734,7 @@ static void CompileRes (const char* File)
|
|||||||
** BEFORE adding the file
|
** BEFORE adding the file
|
||||||
*/
|
*/
|
||||||
if (DoAssemble && DoLink) {
|
if (DoAssemble && DoLink) {
|
||||||
AsmName = MakeTmpFilename(".s");
|
AsmName = MakeTmpFilename(OutputDirectory, File, ".s");
|
||||||
CmdSetAsmOutput(&GRC, AsmName);
|
CmdSetAsmOutput(&GRC, AsmName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1623,6 +1628,7 @@ int main (int argc, char* argv [])
|
|||||||
case 'o':
|
case 'o':
|
||||||
/* Name the output file */
|
/* Name the output file */
|
||||||
OutputName = GetArg (&I, 2);
|
OutputName = GetArg (&I, 2);
|
||||||
|
OutputDirectory = GetFileDirectory(OutputName);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'r':
|
case 'r':
|
||||||
@@ -1713,6 +1719,9 @@ int main (int argc, char* argv [])
|
|||||||
}
|
}
|
||||||
|
|
||||||
RemoveTempFiles ();
|
RemoveTempFiles ();
|
||||||
|
if (OutputDirectory != NULL) {
|
||||||
|
xfree(OutputDirectory);
|
||||||
|
}
|
||||||
|
|
||||||
/* Return an apropriate exit code */
|
/* Return an apropriate exit code */
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|||||||
@@ -33,8 +33,17 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
# include <process.h>
|
||||||
|
#else
|
||||||
|
# include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "xmalloc.h"
|
#include "xmalloc.h"
|
||||||
#include "fname.h"
|
#include "fname.h"
|
||||||
@@ -93,7 +102,28 @@ const char* FindName (const char* Path)
|
|||||||
return Path + Len;
|
return Path + Len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *GetFileDirectory (const char* File)
|
||||||
|
/* Return a copy of the path part of a File, or NULL if there is none. */
|
||||||
|
{
|
||||||
|
char *Out, *P;
|
||||||
|
|
||||||
|
if (File == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
Out = xmalloc (strlen (File) + 1);
|
||||||
|
strcpy(Out, File);
|
||||||
|
|
||||||
|
P = (char *)FindName (Out);
|
||||||
|
if (P == Out) {
|
||||||
|
/* This is a simple filename. */
|
||||||
|
xfree (Out);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
*P = '\0';
|
||||||
|
|
||||||
|
return Out;
|
||||||
|
}
|
||||||
|
|
||||||
char* MakeFilename (const char* Origin, const char* Ext)
|
char* MakeFilename (const char* Origin, const char* Ext)
|
||||||
/* Make a new file name from Origin and Ext. If Origin has an extension, it
|
/* Make a new file name from Origin and Ext. If Origin has an extension, it
|
||||||
@@ -119,35 +149,22 @@ char* MakeFilename (const char* Origin, const char* Ext)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
char* MakeTmpFilename (const char* Ext)
|
char* MakeTmpFilename (const char *Directory, const char *Origin, const char* Ext)
|
||||||
/* Make a new temporary file name from Ext. tmpnam(3) is called
|
/* Make a new temporary file name from Origin and Ext.
|
||||||
** and Ext is appended to generate the filename.
|
|
||||||
** The result is placed in a malloc'ed buffer and returned.
|
** The result is placed in a malloc'ed buffer and returned.
|
||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
char* Out;
|
char* Out;
|
||||||
char Buffer[L_tmpnam * 2]; /* a lazy way to ensure we have space for Ext */
|
size_t Len = 0;
|
||||||
|
|
||||||
/*
|
/* Allocate template */
|
||||||
** gcc emits the following warning here:
|
if (Directory != NULL) {
|
||||||
**
|
Len = strlen (Directory);
|
||||||
** warning: the use of `tmpnam' is dangerous, better use `mkstemp'
|
}
|
||||||
**
|
Len += strlen (Origin) + strlen (".2147483648") + strlen (Ext) + 1;
|
||||||
** however, mkstemp actually opens a file, which we do not want.
|
Out = xmalloc (Len);
|
||||||
** tmpfile() is unsuitable for the same reason.
|
|
||||||
**
|
|
||||||
** we could write our own version, but then we would have to struggle
|
|
||||||
** with supporting multiple build environments.
|
|
||||||
**
|
|
||||||
** tmpnam(3) is safe here, because ca65 / cc65 / ld65 will simply clobber
|
|
||||||
** an existing file, or exit if with an error if they are unable to.
|
|
||||||
**
|
|
||||||
** gcc will also complain, if you don't use the return value from tmpnam(3)
|
|
||||||
*/
|
|
||||||
strcat(tmpnam(Buffer), Ext);
|
|
||||||
|
|
||||||
Out = xmalloc (strlen (Buffer) + 1);
|
|
||||||
strcpy (Out, Buffer);
|
|
||||||
|
|
||||||
|
snprintf (Out, Len, "%s%s.%u%s", (Directory != NULL ? Directory : ""),
|
||||||
|
FindName(Origin), getpid(), Ext);
|
||||||
return Out;
|
return Out;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,6 +52,9 @@ const char* FindName (const char* Path);
|
|||||||
** the file, the function returns Path as name.
|
** the file, the function returns Path as name.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
char *GetFileDirectory (const char* File);
|
||||||
|
/* Return a copy of the path part of a File, or NULL if there is none. */
|
||||||
|
|
||||||
char* MakeFilename (const char* Origin, const char* Ext);
|
char* MakeFilename (const char* Origin, const char* Ext);
|
||||||
/* Make a new file name from Origin and Ext. If Origin has an extension, it
|
/* Make a new file name from Origin and Ext. If Origin has an extension, it
|
||||||
** is removed and Ext is appended. If Origin has no extension, Ext is simply
|
** is removed and Ext is appended. If Origin has no extension, Ext is simply
|
||||||
@@ -59,9 +62,10 @@ char* MakeFilename (const char* Origin, const char* Ext);
|
|||||||
** The function may be used to create "foo.o" from "foo.s".
|
** The function may be used to create "foo.o" from "foo.s".
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char* MakeTmpFilename (const char* Ext);
|
char* MakeTmpFilename (const char *Directory, const char *Origin, const char* Ext);
|
||||||
/* Make a new temporary file name from Ext. tmpnam(3) is called
|
/* Make a new temporary file name from Directory, Origin, and Ext.
|
||||||
** and Ext is appended to generate the filename.
|
** A temporary path is generated from the Directory,
|
||||||
|
** the Origin filename, the compiler's PID and the Extension.
|
||||||
** The result is placed in a malloc'ed buffer and returned.
|
** The result is placed in a malloc'ed buffer and returned.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user