]> git.sur5r.net Git - cc65/blobdiff - libsrc/cbm/readdir.c
Merge remote-tracking branch 'upstream/master'
[cc65] / libsrc / cbm / readdir.c
index 678c343fce25c9d25064980eaf40604f42d37712..d6947316bf0039466b7c0ac7b810c787572b509b 100644 (file)
@@ -18,8 +18,8 @@ struct dirent* __fastcall__ readdir (register DIR* dir)
     register unsigned char* b;
     register unsigned char i;
     register unsigned char count;
-    unsigned char s;
-    unsigned char j;
+    static unsigned char s;
+    static unsigned char j;
     unsigned char buffer[0x40];
     static struct dirent entry;
 
@@ -38,9 +38,6 @@ struct dirent* __fastcall__ readdir (register DIR* dir)
         goto exitpoint;
     }
 
-    /* Adjust the directory offset */
-    dir->off += 4;
-
     /* Read the next file entry into the buffer */
     for (count = 0, b = buffer; count < sizeof (buffer); ++b) {
         if (!_dirread1 (dir, b)) {
@@ -52,8 +49,8 @@ struct dirent* __fastcall__ readdir (register DIR* dir)
         }
     }
 
-    /* Bump the directory offset */
-    dir->off += count;
+    /* Bump the directory offset and include the bytes for line-link and size */
+    dir->off += count + 4;
 
     /* End of directory is reached if the buffer contains "blocks free". It is
      * sufficient here to check for the leading 'b'. buffer will contain at
@@ -81,9 +78,17 @@ struct dirent* __fastcall__ readdir (register DIR* dir)
             case 1:
                 /* Within file name */
                 if (*b == '"') {
+                    /* End of file name found. */
                     entry.d_name[j] = '\0';
                     entry.d_namlen = j;
-                    s = 2;
+                    if (entry.d_off > 2) {
+                        /* Proceed with file type */
+                        s = 2;
+                    } else {
+                        /* This is a disk header, so we're done */
+                        entry.d_type = _CBM_T_HEADER;
+                        return &entry;
+                    }
                 } else if (j < sizeof (entry.d_name) - 1) {
                     entry.d_name[j] = *b;
                     ++j;
@@ -108,8 +113,8 @@ struct dirent* __fastcall__ readdir (register DIR* dir)
                 /* Distinguish DEL or DIR file type entries */
                 switch (*b) {
                     case 'e':                                   break;
-                    case 'i': entry.d_type = CBM_T_DIR;         break;
-                    default:  entry.d_type = CBM_T_OTHER;       break;
+                    case 'i': entry.d_type = _CBM_T_DIR;        break;
+                    default:  entry.d_type = _CBM_T_OTHER;      break;
                 }
                 return &entry;
         }