diff --git a/src/cl65/main.c b/src/cl65/main.c index 42126e6d7..528e64e56 100644 --- a/src/cl65/main.c +++ b/src/cl65/main.c @@ -122,6 +122,11 @@ static int DoAssemble = 1; /* The name of the output file, NULL if none given */ 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 */ static const char* LinkerConfig = 0; @@ -555,7 +560,7 @@ static void AssembleFile (const char* File, const char* TmpFile, unsigned ArgCou if (TmpFile) { ObjName = MakeFilename (TmpFile, ".o"); } else { - ObjName = MakeTmpFilename (".o"); + ObjName = MakeTmpFilename (OutputDirectory, File, ".o"); } CmdSetOutput (&CA65, ObjName); CmdAddFile (&LD65, ObjName); @@ -684,7 +689,7 @@ static void Compile (const char* File) if (DoAssemble) { /* set a temporary output file name */ - TmpFile = MakeTmpFilename(".s"); + TmpFile = MakeTmpFilename(OutputDirectory, File, ".s"); CmdSetOutput (&CC65, TmpFile); } @@ -729,7 +734,7 @@ static void CompileRes (const char* File) ** BEFORE adding the file */ if (DoAssemble && DoLink) { - AsmName = MakeTmpFilename(".s"); + AsmName = MakeTmpFilename(OutputDirectory, File, ".s"); CmdSetAsmOutput(&GRC, AsmName); } @@ -1623,6 +1628,7 @@ int main (int argc, char* argv []) case 'o': /* Name the output file */ OutputName = GetArg (&I, 2); + OutputDirectory = GetFileDirectory(OutputName); break; case 'r': @@ -1713,6 +1719,9 @@ int main (int argc, char* argv []) } RemoveTempFiles (); + if (OutputDirectory != NULL) { + xfree(OutputDirectory); + } /* Return an apropriate exit code */ return EXIT_SUCCESS; diff --git a/src/common/fname.c b/src/common/fname.c index e67470b33..d835ee7a0 100644 --- a/src/common/fname.c +++ b/src/common/fname.c @@ -33,8 +33,17 @@ +#include #include +#include #include +#include + +#if defined(_MSC_VER) +# include +#else +# include +#endif #include "xmalloc.h" #include "fname.h" @@ -93,7 +102,28 @@ const char* FindName (const char* Path) 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) /* 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) -/* Make a new temporary file name from Ext. tmpnam(3) is called -** and Ext is appended to generate the filename. +char* MakeTmpFilename (const char *Directory, const char *Origin, const char* Ext) +/* Make a new temporary file name from Origin and Ext. ** The result is placed in a malloc'ed buffer and returned. */ { char* Out; - char Buffer[L_tmpnam * 2]; /* a lazy way to ensure we have space for Ext */ + size_t Len = 0; - /* - ** gcc emits the following warning here: - ** - ** warning: the use of `tmpnam' is dangerous, better use `mkstemp' - ** - ** however, mkstemp actually opens a file, which we do not want. - ** 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); + /* Allocate template */ + if (Directory != NULL) { + Len = strlen (Directory); + } + Len += strlen (Origin) + strlen (".2147483648") + strlen (Ext) + 1; + Out = xmalloc (Len); + snprintf (Out, Len, "%s%s.%u%s", (Directory != NULL ? Directory : ""), + FindName(Origin), getpid(), Ext); return Out; } diff --git a/src/common/fname.h b/src/common/fname.h index 852c4ae56..ede34152d 100644 --- a/src/common/fname.h +++ b/src/common/fname.h @@ -52,6 +52,9 @@ const char* FindName (const char* Path); ** 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); /* 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 @@ -59,9 +62,10 @@ char* MakeFilename (const char* Origin, const char* Ext); ** The function may be used to create "foo.o" from "foo.s". */ -char* MakeTmpFilename (const char* Ext); -/* Make a new temporary file name from Ext. tmpnam(3) is called -** and Ext is appended to generate the filename. +char* MakeTmpFilename (const char *Directory, const char *Origin, const char* Ext); +/* Make a new temporary file name from Directory, Origin, and Ext. +** 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. */