]> git.sur5r.net Git - cc65/blobdiff - src/cc65/input.c
Fixed several type conversion issues
[cc65] / src / cc65 / input.c
index 307d3557ed533f7419961633e52479449a6ccc36..0f254f4921bb6034ad7ec79a62425aae5c46fae6 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 /* common */
 #include "check.h"
 #include "coll.h"
+#include "print.h"
 #include "xmalloc.h"
 
 /* cc65 */
 #include "asmcode.h"
+#include "codegen.h"
 #include "error.h"
 #include "incpath.h"
 #include "lineinfo.h"
@@ -86,7 +90,7 @@ static Collection AFiles = STATIC_COLLECTION_INITIALIZER;
 
 
 /*****************************************************************************/
-/*                              struct IFile                                */
+/*                              struct IFile                                */
 /*****************************************************************************/
 
 
@@ -103,6 +107,8 @@ static IFile* NewIFile (const char* Name)
     /* Initialize the fields */
     IF->Index = CollCount (&IFiles) + 1;
     IF->Usage = 0;
+    IF->Size  = 0;
+    IF->MTime = 0;
     memcpy (IF->Name, Name, Len+1);
 
     /* Insert the new structure into the IFile collection */
@@ -131,8 +137,24 @@ static AFile* NewAFile (IFile* IF, FILE* F)
     AF->F     = F;
     AF->Input = IF;
 
-    /* Increment the usage counter of the corresponding IFile */
-    ++IF->Usage;
+    /* Increment the usage counter of the corresponding IFile. If this
+     * is the first use, set the file data and output debug info if
+     * requested.
+     */
+    if (IF->Usage++ == 0) {
+
+       /* Get file size and modification time */
+       struct stat Buf;
+       if (fstat (fileno (F), &Buf) != 0) {
+           /* Error */
+           Fatal ("Cannot stat `%s': %s", IF->Name, strerror (errno));
+       }
+               IF->Size  = (unsigned long) Buf.st_size;
+       IF->MTime = (unsigned long) Buf.st_mtime;
+
+       /* Set the debug data */
+       g_fileinfo (IF->Name, IF->Size, IF->MTime);
+    }
 
     /* Insert the new structure into the AFile collection */
     CollAppend (&AFiles, AF);
@@ -238,6 +260,9 @@ void OpenIncludeFile (const char* Name, unsigned DirSpec)
        return;
     }
 
+    /* Debugging output */
+    Print (stdout, 1, "Opened include file `%s'\n", IF->Name);
+
     /* Allocate a new AFile structure */
     (void) NewAFile (IF, F);
 }
@@ -362,11 +387,14 @@ int NextLine (void)
        /* We got a new line */
        ++Input->Line;
 
-       /* Remove the trailing newline if we have one */
+       /* Remove the trailing cr/lf if we have one. We will ignore both, cr
+        * and lf on all systems since this enables us to compile DOS/Windows
+        * stuff also on unix systems (where fgets does not remove the cr).
+        */
        Part = strlen (line + Len);
        Start = Len;
        Len += Part;
-       while (Len > 0 && line [Len-1] == '\n') {
+       while (Len > 0 && (line[Len-1] == '\n' || line[Len-1] == '\r')) {
            --Len;
        }
        line [Len] = '\0';