]> git.sur5r.net Git - cc65/blob - libsrc/cbm/readdir.c
0a20bdd06660504a296d40bcd2a79cd95964efd0
[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     dir->off += 2;
36
37     /* Read the number of blocks */
38     if (!_dirread (dir, &entry.d_blocks, sizeof (entry.d_blocks))) {
39         goto exitpoint;
40     }
41
42     /* Read the next file entry into the buffer */
43     for (count = 0, b = buffer; count < sizeof (buffer); ++count, ++b) {
44         if (!_dirread1 (dir, b)) {
45             goto exitpoint;
46         }
47         if (*b == '\0') {
48             break;
49         }
50     }
51
52     /* End of directory is reached if the buffer contains "blocks free". It is
53      * sufficient here to check for the leading 'b'. To avoid problems if we're
54      * called again, read until end of directory.
55      */
56     if (count > 0 && buffer[0] == 'b') {
57         while (_dirread1 (dir, buffer)) ;
58         return 0;
59     }
60
61     /* Bump the directory offset */
62     dir->off += count;
63
64     /* Parse the buffer for the filename and file type */
65     i = 0;
66     j = 0;
67     s = 0;
68     b = buffer;
69     while (i < count) {
70         switch (s) {
71
72             case 0:
73                 /* Searching for start of file name */
74                 if (*b == '"') {
75                     s = 1;
76                 }
77                 break;
78
79             case 1:
80                 /* Within file name */
81                 if (*b == '"') {
82                     entry.d_name[j] = '\0';
83                     entry.d_namlen = j;
84                     s = 2;
85                 } else if (j < sizeof (entry.d_name) - 1) {
86                     entry.d_name[j] = *b;
87                     ++j;
88                 }
89                 break;
90
91             case 2:
92                 /* Searching for file type */
93                 if (*b != ' ') {
94                     entry.d_type = _cbm_filetype (*b);
95                     if (*b == 'd') {
96                         /* May be DEL or DIR, check next char */
97                         s = 3;
98                     } else {
99                         /* Done */
100                         return &entry;
101                     }
102                 }
103                 break;
104
105             case 3:
106                 /* Distinguish DEL or DIR file type entries */
107                 switch (*b) {
108                     case 'e':                                   break;
109                     case 'i': entry.d_type = CBM_T_DIR;         break;
110                     default:  entry.d_type = CBM_T_OTHER;       break;
111                 }
112                 return &entry;
113         }
114         ++i;
115         ++b;
116     }
117
118     /* Something went wrong when parsing the directory entry */
119     _errno = EIO;
120 exitpoint:
121     return 0;
122 }
123
124
125