]> git.gag.com Git - fw/sdcc/blobdiff - src/SDCCglue.c
* Added z80 blurb.
[fw/sdcc] / src / SDCCglue.c
index 43ab6992ef71c2d559500e4a2b40de2a357274be..8b4629595e1f10acff04feda0076f854c1c3eded 100644 (file)
@@ -38,6 +38,7 @@ symbol *mainf;
 extern char *VersionString;
 extern FILE *codeOutFile;
 set *tmpfileSet = NULL; /* set of tmp file created by the compiler */
+set *tmpfileNameSet = NULL; /* All are unlinked at close. */
 /*-----------------------------------------------------------------*/
 /* closeTmpFiles - closes all tmp files created by the compiler    */
 /*                 because of BRAIN DEAD MS/DOS & CYGNUS Libraries */
@@ -52,6 +53,21 @@ DEFSETFUNC(closeTmpFiles)
     return 0;
 }
 
+/*-----------------------------------------------------------------*/
+/* rmTmpFiles - closes all tmp files created by the compiler    */
+/*                 because of BRAIN DEAD MS/DOS & CYGNUS Libraries */
+/*-----------------------------------------------------------------*/
+DEFSETFUNC(rmTmpFiles)
+{
+    char *name = item;
+
+    if (name) {
+       unlink(name);
+       free(name);
+    }
+    return 0;
+}
+
 /*-----------------------------------------------------------------*/
 /* copyFile - copies source file to destination file               */
 /*-----------------------------------------------------------------*/
@@ -885,14 +901,14 @@ void glue ()
 {
     FILE *vFile;
     FILE *asmFile;
-    FILE *ovrFile = tmpfile();
+    FILE *ovrFile = tempfile();
     
     addSetHead(&tmpfileSet,ovrFile);
     /* print the global struct definitions */
     if (options.debug)
        cdbStructBlock (0,cdbFile);
 
-    vFile = tmpfile();
+    vFile = tempfile();
     /* PENDING: this isnt the best place but it will do */
     if (port->general.glue_up_main) {
        /* create the interrupt vector table */
@@ -1092,4 +1108,31 @@ void glue ()
     
     fclose (asmFile);
     applyToSet(tmpfileSet,closeTmpFiles);
+    applyToSet(tmpfileNameSet, rmTmpFiles);
+}
+
+/** Creates a temporary file a'la tmpfile which avoids the bugs
+    in cygwin wrt c:\tmp.
+    Scans, in order: TMP, TEMP, TMPDIR, else uses tmpfile().
+*/
+FILE *tempfile(void)
+{
+    const char *tmpdir = NULL;
+    if (getenv("TMP"))
+       tmpdir = getenv("TMP");
+    else if (getenv("TEMP"))
+       tmpdir = getenv("TEMP");
+    else if (getenv("TMPDIR"))
+       tmpdir = getenv("TMPDIR");
+    if (tmpdir) {
+       char *name = tempnam(tmpdir, "sdcc");
+       if (name) {
+           FILE *fp = fopen(name, "w+b");
+           if (fp)
+               addSetHead(&tmpfileNameSet, name);
+           return fp;
+       }
+       return NULL;
+    }
+    return tmpfile();
 }