]> git.sur5r.net Git - cc65/blob - libsrc/cbm/readdir.c
C90 param, void
[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     static unsigned char s;
22     static 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     /* Read the next file entry into the buffer */
42     for (count = 0, b = buffer; count < sizeof (buffer); ++b) {
43         if (!_dirread1 (dir, b)) {
44             goto exitpoint;
45         }
46         ++count;
47         if (*b == '\0') {
48             break;
49         }
50     }
51
52     /* Bump the directory offset and include the bytes for line-link and size */
53     dir->off += count + 4;
54
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.
58     */
59     if (buffer[0] == 'b') {
60         goto exitpoint;
61     }
62
63     /* Parse the buffer for the filename and file type */
64     i = 0;
65     j = 0;
66     s = 0;
67     b = buffer;
68     while (i < count) {
69         switch (s) {
70
71             case 0:
72                 /* Searching for start of file name */
73                 if (*b == '"') {
74                     s = 1;
75                 }
76                 break;
77
78             case 1:
79                 /* Within file name */
80                 if (*b == '"') {
81                     /* End of file name found. */
82                     entry.d_name[j] = '\0';
83                     entry.d_namlen = j;
84                     if (entry.d_off > 2) {
85                         /* Proceed with file type */
86                         s = 2;
87                     } else {
88                         /* This is a disk header, so we're done */
89                         entry.d_type = _CBM_T_HEADER;
90                         return &entry;
91                     }
92                 } else if (j < sizeof (entry.d_name) - 1) {
93                     entry.d_name[j] = *b;
94                     ++j;
95                 }
96                 break;
97
98             case 2:
99                 /* Searching for file type */
100                 if (*b != ' ') {
101                     entry.d_type = _cbm_filetype (*b);
102                     if (*b == 'd') {
103                         /* May be DEL or DIR, check next char */
104                         s = 3;
105                     } else {
106                         /* Done */
107                         return &entry;
108                     }
109                 }
110                 break;
111
112             case 3:
113                 /* Distinguish DEL or DIR file type entries */
114                 switch (*b) {
115                     case 'e':                                   break;
116                     case 'i': entry.d_type = _CBM_T_DIR;        break;
117                     default:  entry.d_type = _CBM_T_OTHER;      break;
118                 }
119                 return &entry;
120         }
121         ++i;
122         ++b;
123     }
124
125     /* Something went wrong when parsing the directory entry */
126     _errno = EIO;
127 exitpoint:
128     return 0;
129 }
130
131
132