]> git.sur5r.net Git - cc65/blob - libsrc/cbm/readdir.c
678c343fce25c9d25064980eaf40604f42d37712
[cc65] / libsrc / cbm / readdir.c
1 /*
2  * Ullrich von Bassewitz, 2012-05-30. Based on code by Groepaz.
3  */
4
5
6
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <cbm.h>
11 #include "dir.h"
12
13 #include <stdio.h>
14
15
16 struct dirent* __fastcall__ readdir (register DIR* dir)
17 {
18     register unsigned char* b;
19     register unsigned char i;
20     register unsigned char count;
21     unsigned char s;
22     unsigned char j;
23     unsigned char buffer[0x40];
24     static struct dirent entry;
25
26
27     /* Remember the directory offset for this entry */
28     entry.d_off = dir->off;
29
30     /* Skip the basic line-link */
31     if (!_dirread (dir, buffer, 2)) {
32         /* errno already set */
33         goto exitpoint;
34     }
35
36     /* Read the number of blocks */
37     if (!_dirread (dir, &entry.d_blocks, sizeof (entry.d_blocks))) {
38         goto exitpoint;
39     }
40
41     /* Adjust the directory offset */
42     dir->off += 4;
43
44     /* Read the next file entry into the buffer */
45     for (count = 0, b = buffer; count < sizeof (buffer); ++b) {
46         if (!_dirread1 (dir, b)) {
47             goto exitpoint;
48         }
49         ++count;
50         if (*b == '\0') {
51             break;
52         }
53     }
54
55     /* Bump the directory offset */
56     dir->off += count;
57
58     /* End of directory is reached if the buffer contains "blocks free". It is
59      * sufficient here to check for the leading 'b'. buffer will contain at
60      * least one byte if we come here.
61      */
62     if (buffer[0] == 'b') {
63         goto exitpoint;
64     }
65
66     /* Parse the buffer for the filename and file type */
67     i = 0;
68     j = 0;
69     s = 0;
70     b = buffer;
71     while (i < count) {
72         switch (s) {
73
74             case 0:
75                 /* Searching for start of file name */
76                 if (*b == '"') {
77                     s = 1;
78                 }
79                 break;
80
81             case 1:
82                 /* Within file name */
83                 if (*b == '"') {
84                     entry.d_name[j] = '\0';
85                     entry.d_namlen = j;
86                     s = 2;
87                 } else if (j < sizeof (entry.d_name) - 1) {
88                     entry.d_name[j] = *b;
89                     ++j;
90                 }
91                 break;
92
93             case 2:
94                 /* Searching for file type */
95                 if (*b != ' ') {
96                     entry.d_type = _cbm_filetype (*b);
97                     if (*b == 'd') {
98                         /* May be DEL or DIR, check next char */
99                         s = 3;
100                     } else {
101                         /* Done */
102                         return &entry;
103                     }
104                 }
105                 break;
106
107             case 3:
108                 /* Distinguish DEL or DIR file type entries */
109                 switch (*b) {
110                     case 'e':                                   break;
111                     case 'i': entry.d_type = CBM_T_DIR;         break;
112                     default:  entry.d_type = CBM_T_OTHER;       break;
113                 }
114                 return &entry;
115         }
116         ++i;
117         ++b;
118     }
119
120     /* Something went wrong when parsing the directory entry */
121     _errno = EIO;
122 exitpoint:
123     return 0;
124 }
125
126
127