2011-04-16 12:28:06 +00:00
|
|
|
/* This is a very simplified version of the POSIX opendir(), */
|
|
|
|
/* readdir(), and closedir() -- for Commodore computers. */
|
|
|
|
/* Created by Josef Soucek, 2003. E-mail: josef.soucek@ct.cz */
|
2003-02-02 13:18:40 +00:00
|
|
|
|
2011-04-16 12:28:06 +00:00
|
|
|
/* 2003-01-21 -- Version 0.1 */
|
|
|
|
/* 2009-10-10 -- Version 0.3 */
|
|
|
|
/* 2011-04-07 -- Version 0.4, groepaz */
|
|
|
|
/* 2011-04-14 -- Version 0.5, Greg King */
|
2003-02-02 13:18:40 +00:00
|
|
|
|
2011-04-16 12:28:06 +00:00
|
|
|
/* Tested with floppy-drive and IDE64 devices. */
|
|
|
|
/* Not tested with messed (buggy) directory listings. */
|
|
|
|
/* Limits filenames to 16 chars. (VICE supports more */
|
|
|
|
/* in directory listings). */
|
2003-02-02 13:18:40 +00:00
|
|
|
|
|
|
|
|
2012-06-04 18:32:38 +00:00
|
|
|
#include <stdarg.h>
|
2003-02-02 13:18:40 +00:00
|
|
|
#include <cbm.h>
|
2011-04-16 12:28:06 +00:00
|
|
|
#include <errno.h>
|
2003-02-02 13:18:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-04 18:32:38 +00:00
|
|
|
/* Opens directory listing. Returns 0 if opening directory was successful;
|
|
|
|
** otherwise, an error-code corresponding to cbm_open(). As an optional
|
|
|
|
** argument, the name of the directory may be passed to the function. If
|
|
|
|
** no explicit name is specified, "$" is used.
|
2011-04-16 12:28:06 +00:00
|
|
|
*/
|
2012-06-04 18:32:38 +00:00
|
|
|
unsigned char cbm_opendir (unsigned char lfn, unsigned char device, ...)
|
2009-10-10 20:50:15 +00:00
|
|
|
{
|
2012-06-04 18:32:38 +00:00
|
|
|
va_list ap;
|
|
|
|
const char* name = "$";
|
|
|
|
|
|
|
|
/* The name used in cbm_open may optionally be passed */
|
|
|
|
if (__argsize__ == 4) {
|
|
|
|
va_start (ap, device);
|
|
|
|
name = va_arg (ap, const char*);
|
|
|
|
va_end (ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the directory */
|
|
|
|
if (cbm_open (lfn, device, CBM_READ, name) == 0) {
|
2011-04-16 12:28:06 +00:00
|
|
|
if ((_oserror = cbm_k_chkin (lfn)) == 0) {
|
2009-10-03 18:38:39 +00:00
|
|
|
/* Ignore start address */
|
|
|
|
cbm_k_basin();
|
|
|
|
cbm_k_basin();
|
2011-04-16 12:28:06 +00:00
|
|
|
cbm_k_clrch();
|
2009-10-03 18:38:39 +00:00
|
|
|
if (cbm_k_readst()) {
|
|
|
|
cbm_close(lfn);
|
2011-04-16 12:28:06 +00:00
|
|
|
_oserror = 4; /* directory cannot be read */
|
2003-02-02 13:18:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-16 12:28:06 +00:00
|
|
|
return _oserror;
|
2003-02-02 13:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-04-16 12:28:06 +00:00
|
|
|
/* Reads one directory line into cbm_dirent structure.
|
|
|
|
** Returns 0 if reading directory-line was successful.
|
|
|
|
** Returns non-zero if reading directory failed, or no more file-names to read.
|
|
|
|
** Returns 2 on last line. Then, l_dirent->size = the number of "blocks free."
|
|
|
|
*/
|
2003-02-02 13:18:40 +00:00
|
|
|
unsigned char __fastcall__ cbm_readdir (unsigned char lfn, register struct cbm_dirent* l_dirent)
|
|
|
|
{
|
2011-04-16 12:28:06 +00:00
|
|
|
unsigned char byte, i = 0;
|
|
|
|
unsigned char is_header = 0;
|
|
|
|
unsigned char rv = 1;
|
2009-09-28 22:01:58 +00:00
|
|
|
|
|
|
|
if (!cbm_k_chkin(lfn)) {
|
|
|
|
if (!cbm_k_readst()) {
|
2011-04-16 12:28:06 +00:00
|
|
|
/* skip 2 bytes, next-BASIC-line pointer */
|
2009-09-28 22:01:58 +00:00
|
|
|
cbm_k_basin();
|
|
|
|
cbm_k_basin();
|
|
|
|
|
2011-04-16 12:28:06 +00:00
|
|
|
/* File-size or drive/partition number */
|
|
|
|
l_dirent->size = cbm_k_basin() | (cbm_k_basin() << 8);
|
2003-02-02 13:18:40 +00:00
|
|
|
|
|
|
|
byte = cbm_k_basin();
|
2011-04-16 12:28:06 +00:00
|
|
|
switch (byte) {
|
2003-02-02 13:18:40 +00:00
|
|
|
|
2011-04-16 12:28:06 +00:00
|
|
|
/* "B" BLOCKS FREE. */
|
|
|
|
case 'b':
|
|
|
|
/* Read until end; careless callers might call us again. */
|
2009-09-28 22:01:58 +00:00
|
|
|
while (!cbm_k_readst()) {
|
|
|
|
cbm_k_basin();
|
2003-02-02 13:18:40 +00:00
|
|
|
}
|
2009-09-28 22:01:58 +00:00
|
|
|
rv = 2; /* EOF */
|
|
|
|
goto ret_val;
|
2003-02-02 13:18:40 +00:00
|
|
|
|
2011-04-16 12:28:06 +00:00
|
|
|
/* Reverse-text shows when this is the directory header. */
|
|
|
|
case 0x12: /* RVS_ON */
|
2009-09-28 22:01:58 +00:00
|
|
|
is_header = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (byte != '\"') {
|
|
|
|
/* prevent endless loop */
|
|
|
|
if (cbm_k_readst()) {
|
|
|
|
rv = 3;
|
|
|
|
goto ret_val;
|
2003-02-02 13:18:40 +00:00
|
|
|
}
|
2011-04-16 12:28:06 +00:00
|
|
|
byte = cbm_k_basin();
|
2003-02-02 13:18:40 +00:00
|
|
|
}
|
|
|
|
|
2009-09-28 22:01:58 +00:00
|
|
|
while ((byte = cbm_k_basin()) != '\"') {
|
|
|
|
/* prevent endless loop */
|
|
|
|
if (cbm_k_readst()) {
|
|
|
|
rv = 4;
|
|
|
|
goto ret_val;
|
|
|
|
}
|
2003-02-02 13:18:40 +00:00
|
|
|
|
|
|
|
if (i < sizeof (l_dirent->name) - 1) {
|
|
|
|
l_dirent->name[i] = byte;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l_dirent->name[i] = '\0';
|
|
|
|
|
2009-09-28 22:01:58 +00:00
|
|
|
if (is_header) {
|
|
|
|
l_dirent->type = CBM_T_HEADER;
|
2011-04-16 12:28:06 +00:00
|
|
|
|
|
|
|
/* Get the disk-format code. */
|
|
|
|
i = 6;
|
|
|
|
do {
|
|
|
|
l_dirent->access = byte = cbm_k_basin();
|
|
|
|
} while (--i != 0);
|
|
|
|
|
2009-09-28 22:01:58 +00:00
|
|
|
} else {
|
2011-04-16 12:28:06 +00:00
|
|
|
/* Go to the file-type column. */
|
|
|
|
while ((byte = cbm_k_basin()) == ' ') {
|
|
|
|
/* prevent endless loop */
|
|
|
|
if (cbm_k_readst()) {
|
|
|
|
rv = 5;
|
|
|
|
goto ret_val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l_dirent->access = CBM_A_RW;
|
|
|
|
|
|
|
|
/* "Splat" files shouldn't be read. */
|
|
|
|
if (byte == '*') {
|
|
|
|
l_dirent->access = CBM_A_WO;
|
|
|
|
byte = cbm_k_basin();
|
|
|
|
}
|
|
|
|
|
2012-06-02 22:56:14 +00:00
|
|
|
/* Determine the file type */
|
|
|
|
l_dirent->type = _cbm_filetype (byte);
|
2003-02-02 13:18:40 +00:00
|
|
|
|
2011-04-16 12:28:06 +00:00
|
|
|
/* Notice whether it's a directory or a deleted file. */
|
|
|
|
if (cbm_k_basin() == 'i' && byte == 'd') {
|
|
|
|
l_dirent->type = CBM_T_DIR;
|
|
|
|
}
|
2009-09-28 22:01:58 +00:00
|
|
|
cbm_k_basin();
|
2003-02-02 13:18:40 +00:00
|
|
|
|
2011-04-16 12:28:06 +00:00
|
|
|
/* Locked files shouldn't be written. */
|
|
|
|
if ((byte = cbm_k_basin()) == '<') {
|
|
|
|
l_dirent->access = (l_dirent->access == CBM_A_WO)
|
|
|
|
? 0 : CBM_A_RO;
|
|
|
|
}
|
2009-09-28 22:01:58 +00:00
|
|
|
}
|
2003-02-02 13:18:40 +00:00
|
|
|
|
2011-04-16 12:28:06 +00:00
|
|
|
/* Read to the end of the line. */
|
2009-09-28 22:01:58 +00:00
|
|
|
while (byte != 0) {
|
|
|
|
/* prevent endless loop */
|
|
|
|
if (cbm_k_readst()) {
|
|
|
|
rv = 6;
|
|
|
|
goto ret_val;
|
2003-02-02 13:18:40 +00:00
|
|
|
}
|
2011-04-16 12:28:06 +00:00
|
|
|
byte = cbm_k_basin();
|
2003-02-02 13:18:40 +00:00
|
|
|
}
|
|
|
|
|
2009-09-28 22:01:58 +00:00
|
|
|
rv = 0;
|
|
|
|
goto ret_val;
|
2003-02-02 13:18:40 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-28 22:01:58 +00:00
|
|
|
|
|
|
|
ret_val:
|
|
|
|
cbm_k_clrch();
|
|
|
|
return rv;
|
2003-02-02 13:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-16 12:28:06 +00:00
|
|
|
|
|
|
|
void __fastcall__ cbm_closedir (unsigned char lfn)
|
2003-02-02 13:18:40 +00:00
|
|
|
{
|
|
|
|
cbm_close(lfn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|