1 /* This is a very simplified version of the POSIX opendir(), */
2 /* readdir(), and closedir() -- for Commodore computers. */
3 /* Created by Josef Soucek, 2003. E-mail: josef.soucek@ct.cz */
5 /* 2003-01-21 -- Version 0.1 */
6 /* 2009-10-10 -- Version 0.3 */
7 /* 2011-04-07 -- Version 0.4, groepaz */
8 /* 2011-04-14 -- Version 0.5, Greg King */
10 /* Tested with floppy-drive and IDE64 devices. */
11 /* Not tested with messed (buggy) directory listings. */
12 /* Limits filenames to 16 chars. (VICE supports more */
13 /* in directory listings). */
22 /* Opens directory listing. Returns 0 if opening directory was successful;
23 ** otherwise, an error-code corresponding to cbm_open(). As an optional
24 ** argument, the name of the directory may be passed to the function. If
25 ** no explicit name is specified, "$" is used.
27 unsigned char cbm_opendir (unsigned char lfn, unsigned char device, ...)
30 const char* name = "$";
32 /* The name used in cbm_open may optionally be passed */
33 if (__argsize__ == 4) {
34 va_start (ap, device);
35 name = va_arg (ap, const char*);
39 /* Open the directory */
40 if (cbm_open (lfn, device, CBM_READ, name) == 0) {
41 if ((_oserror = cbm_k_chkin (lfn)) == 0) {
42 /* Ignore start address */
48 _oserror = 4; /* directory cannot be read */
57 /* Reads one directory line into cbm_dirent structure.
58 ** Returns 0 if reading directory-line was successful.
59 ** Returns non-zero if reading directory failed, or no more file-names to read.
60 ** Returns 2 on last line. Then, l_dirent->size = the number of "blocks free."
62 unsigned char __fastcall__ cbm_readdir (unsigned char lfn, register struct cbm_dirent* l_dirent)
64 unsigned char byte, i = 0;
65 unsigned char is_header = 0;
68 if (!cbm_k_chkin(lfn)) {
69 if (!cbm_k_readst()) {
70 /* skip 2 bytes, next-BASIC-line pointer */
74 /* File-size or drive/partition number */
75 l_dirent->size = cbm_k_basin() | (cbm_k_basin() << 8);
80 /* "B" BLOCKS FREE. */
82 /* Read until end; careless callers might call us again. */
83 while (!cbm_k_readst()) {
89 /* Reverse-text shows when this is the directory header. */
90 case 0x12: /* RVS_ON */
94 while (byte != '\"') {
95 /* prevent endless loop */
100 byte = cbm_k_basin();
103 while ((byte = cbm_k_basin()) != '\"') {
104 /* prevent endless loop */
105 if (cbm_k_readst()) {
110 if (i < sizeof (l_dirent->name) - 1) {
111 l_dirent->name[i] = byte;
115 l_dirent->name[i] = '\0';
118 l_dirent->type = CBM_T_HEADER;
120 /* Get the disk-format code. */
123 l_dirent->access = byte = cbm_k_basin();
127 /* Go to the file-type column. */
128 while ((byte = cbm_k_basin()) == ' ') {
129 /* prevent endless loop */
130 if (cbm_k_readst()) {
136 l_dirent->access = CBM_A_RW;
138 /* "Splat" files shouldn't be read. */
140 l_dirent->access = CBM_A_WO;
141 byte = cbm_k_basin();
144 /* Determine the file type */
145 l_dirent->type = _cbm_filetype (byte);
147 /* Notice whether it's a directory or a deleted file. */
148 if (cbm_k_basin() == 'i' && byte == 'd') {
149 l_dirent->type = CBM_T_DIR;
153 /* Locked files shouldn't be written. */
154 if ((byte = cbm_k_basin()) == '<') {
155 l_dirent->access = (l_dirent->access == CBM_A_WO)
160 /* Read to the end of the line. */
162 /* prevent endless loop */
163 if (cbm_k_readst()) {
167 byte = cbm_k_basin();
182 void __fastcall__ cbm_closedir (unsigned char lfn)