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;
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 /* Adjust the directory offset */
44 /* Read the next file entry into the buffer */
45 for (count = 0, b = buffer; count < sizeof (buffer); ++b) {
46 if (!_dirread1 (dir, b)) {
55 /* End of directory is reached if the buffer contains "blocks free". It is
56 * sufficient here to check for the leading 'b'. To avoid problems if we're
57 * called again, read until end of directory.
59 if (count > 0 && buffer[0] == 'b') {
60 while (_dirread1 (dir, buffer)) ;
64 /* Bump the directory offset */
67 /* Parse the buffer for the filename and file type */
76 /* Searching for start of file name */
83 /* Within file name */
85 entry.d_name[j] = '\0';
88 } else if (j < sizeof (entry.d_name) - 1) {
95 /* Searching for file type */
97 entry.d_type = _cbm_filetype (*b);
99 /* May be DEL or DIR, check next char */
109 /* Distinguish DEL or DIR file type entries */
112 case 'i': entry.d_type = CBM_T_DIR; break;
113 default: entry.d_type = CBM_T_OTHER; break;
121 /* Something went wrong when parsing the directory entry */