]> git.sur5r.net Git - cc65/blob - libsrc/cbm/readdir.c
Small optimization for shorter code.
[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     /* 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                     entry.d_name[j] = '\0';
82                     entry.d_namlen = j;
83                     s = 2;
84                 } else if (j < sizeof (entry.d_name) - 1) {
85                     entry.d_name[j] = *b;
86                     ++j;
87                 }
88                 break;
89
90             case 2:
91                 /* Searching for file type */
92                 if (*b != ' ') {
93                     entry.d_type = _cbm_filetype (*b);
94                     if (*b == 'd') {
95                         /* May be DEL or DIR, check next char */
96                         s = 3;
97                     } else {
98                         /* Done */
99                         return &entry;
100                     }
101                 }
102                 break;
103
104             case 3:
105                 /* Distinguish DEL or DIR file type entries */
106                 switch (*b) {
107                     case 'e':                                   break;
108                     case 'i': entry.d_type = CBM_T_DIR;         break;
109                     default:  entry.d_type = CBM_T_OTHER;       break;
110                 }
111                 return &entry;
112         }
113         ++i;
114         ++b;
115     }
116
117     /* Something went wrong when parsing the directory entry */
118     _errno = EIO;
119 exitpoint:
120     return 0;
121 }
122
123
124