2 ** Ullrich von Bassewitz, 2012-05-30. Based on code by Groepaz.
16 struct dirent* __fastcall__ readdir (register DIR* dir)
18 register unsigned char* b;
19 register unsigned char i;
20 register unsigned char count;
21 static unsigned char s;
22 static unsigned char j;
23 unsigned char buffer[0x40];
24 static struct dirent entry;
27 /* Remember the directory offset for this entry */
28 entry.d_off = dir->off;
30 /* Skip the basic line-link */
31 if (!_dirread (dir, buffer, 2)) {
32 /* errno already set */
36 /* Read the number of blocks */
37 if (!_dirread (dir, &entry.d_blocks, sizeof (entry.d_blocks))) {
41 /* Read the next file entry into the buffer */
42 for (count = 0, b = buffer; count < sizeof (buffer); ++b) {
43 if (!_dirread1 (dir, b)) {
52 /* Bump the directory offset and include the bytes for line-link and size */
53 dir->off += count + 4;
55 /* End of directory is reached if the buffer contains "blocks free". It is
56 ** sufficient here to check for the leading 'b'. buffer will contain at
57 ** least one byte if we come here.
59 if (buffer[0] == 'b') {
63 /* Parse the buffer for the filename and file type */
72 /* Searching for start of file name */
79 /* Within file name */
81 /* End of file name found. */
82 entry.d_name[j] = '\0';
84 if (entry.d_off > 2) {
85 /* Proceed with file type */
88 /* This is a disk header, so we're done */
89 entry.d_type = _CBM_T_HEADER;
92 } else if (j < sizeof (entry.d_name) - 1) {
99 /* Searching for file type */
101 entry.d_type = _cbm_filetype (*b);
103 /* May be DEL or DIR, check next char */
113 /* Distinguish DEL or DIR file type entries */
116 case 'i': entry.d_type = _CBM_T_DIR; break;
117 default: entry.d_type = _CBM_T_OTHER; break;
125 /* Something went wrong when parsing the directory entry */