2 * Ullrich von Bassewitz, 2012-05-30. Based on code by Groepaz.
19 struct dirent* __fastcall__ readdir (register DIR* dir)
21 static struct dirent entry;
22 register unsigned char* b;
23 register unsigned char i;
27 unsigned char buffer[0x40];
30 /* Remember the directory offset for this entry */
31 entry.d_off = dir->off;
33 /* Skip the basic line-link */
34 if (!_dirskip (2, dir)) {
35 /* errno already set */
40 /* Read the number of blocks */
41 if (read (dir->fd, &entry.d_blocks, sizeof (entry.d_blocks)) !=
42 sizeof (entry.d_blocks)) {
46 /* Read the next file entry into the buffer */
47 for (count = 0, b = buffer; count < sizeof (buffer); ++count, ++b) {
48 if (read (dir->fd, b, 1) != 1) {
49 /* ### Check for EOF */
57 /* End of directory is reached if the buffer contains "blocks free". It is
58 * sufficient here to check for the leading 'b'. To avoid problems if we're
59 * called again, read until end of directory.
61 if (count > 0 && buffer[0] == 'b') {
62 while (read (dir->fd, buffer, 1) == 1) ;
66 /* Bump the directory offset */
69 /* Parse the buffer for the filename and file type */
78 /* Searching for start of file name */
85 /* Within file name */
87 entry.d_name[j] = '\0';
90 } else if (j < sizeof (entry.d_name) - 1) {
97 /* Searching for file type */
99 entry.d_type = _cbm_filetype (*b);
101 /* May be DEL or DIR, check next char */
111 /* Distinguish DEL or DIR file type entries */
114 case 'i': entry.d_type = CBM_T_DIR; break;
115 default: entry.d_type = CBM_T_OTHER; break;
123 /* Something went wrong - terminating quote not found */