]> git.sur5r.net Git - cc65/blobdiff - src/da65/code.c
Added capability to conver o65 object files by using the new co65 utility
[cc65] / src / da65 / code.c
index 2a879459c1a174137a8d069b0cc0f4bee83a7d42..210bb26e9a17d2f2bb23d3ba288a7c6c87ab414e 100644 (file)
@@ -68,7 +68,7 @@ unsigned long PC;                     /* Current PC */
 void LoadCode (const char* Name, unsigned long StartAddress)
 /* Load the code from the given file */
 {
-    unsigned Count, MaxCount;
+    long Count, MaxCount, Size;
     FILE* F;
 
 
@@ -80,18 +80,36 @@ void LoadCode (const char* Name, unsigned long StartAddress)
     /* Open the file */
     F = fopen (Name, "rb");
     if (F == 0) {
-       Error ("Cannot open `%s': %s", Name, strerror (errno));
+       Error ("Cannot open `%s': %s", Name, strerror (errno));
+    }
+
+    /* Seek to the end to get the size of the file */
+    if (fseek (F, 0, SEEK_END) != 0) {
+       Error ("Cannot seek on file `%s': %s", Name, strerror (errno));
+    }
+    Size = ftell (F);
+    rewind (F);
+
+    /* Check if the size is larger than what we can read */
+    if (Size == 0) {
+       Error ("File `%s' contains no data", Name);
+    }
+    if (Size > MaxCount) {
+       Warning ("File `%s' is too large, ignoring %ld bytes",
+                Name, Size - MaxCount);
+    } else if (MaxCount > Size) {
+       MaxCount = (unsigned) Size;
     }
 
     /* Read from the file and remember the number of bytes read */
     Count = fread (CodeBuf + StartAddress, 1, MaxCount, F);
-    if (ferror (F)) {
-       Error ("Error reading from `%s': %s", Name, strerror (errno));
-    }
-    if (Count == 0) {
-       Error ("File `%s' contains no data", Name);
+    if (ferror (F) || Count != MaxCount) {
+       Error ("Error reading from `%s': %s", Name, strerror (errno));
     }
 
+    /* Close the file */
+    fclose (F);
+
     /* Set the buffer variables */
     CodeStart = PC = StartAddress;
     CodeEnd = CodeStart + Count - 1;   /* CodeEnd is inclusive */