]> git.sur5r.net Git - cc65/blobdiff - libsrc/cbm/cbm_dir.c
Merge remote-tracking branch 'upstream/master'
[cc65] / libsrc / cbm / cbm_dir.c
index 0b97e980390a0ba8896958adc6dc38e6f9775229..8f31c502ad70a6ed0bf18931031161310d328d1a 100644 (file)
 /* in directory listings).                            */
 
 
+#include <stdarg.h>
 #include <cbm.h>
 #include <errno.h>
 
 
 
-/* Opens directory listing.
-** Returns 0 if openning directory was successful;
-** otherwise, an error-code corresponding to cbm_open().
+/* 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.
 */
-unsigned char __fastcall__ cbm_opendir (unsigned char lfn, unsigned char device)
+unsigned char cbm_opendir (unsigned char lfn, unsigned char device, ...)
 {
-    if (cbm_open (lfn, device, CBM_READ, "$") == 0) {
+    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) {
         if ((_oserror = cbm_k_chkin (lfn)) == 0) {
             /* Ignore start address */
             cbm_k_basin();
@@ -51,13 +64,6 @@ unsigned char __fastcall__ cbm_readdir (unsigned char lfn, register struct cbm_d
     unsigned char byte, i = 0;
     unsigned char is_header = 0;
     unsigned char rv = 1;
-    static const unsigned char types[] = {
-        CBM_T_CBM,   CBM_T_DEL,   CBM_T_OTHER, CBM_T_OTHER, /* c d e f */
-        CBM_T_OTHER, CBM_T_OTHER, CBM_T_OTHER, CBM_T_OTHER, /* g h i j */
-        CBM_T_OTHER, CBM_T_LNK,   CBM_T_OTHER, CBM_T_OTHER, /* k l m n */
-        CBM_T_OTHER, CBM_T_PRG,   CBM_T_OTHER, CBM_T_REL,   /* o p q r */
-        CBM_T_SEQ,   CBM_T_OTHER, CBM_T_USR,   CBM_T_VRP    /* s t u v */
-    };
 
     if (!cbm_k_chkin(lfn)) {
         if (!cbm_k_readst()) {
@@ -111,11 +117,11 @@ unsigned char __fastcall__ cbm_readdir (unsigned char lfn, register struct cbm_d
             if (is_header) {
                 l_dirent->type = CBM_T_HEADER;
 
-               /* Get the disk-format code. */
-               i = 6;
-               do {
-                   l_dirent->access = byte = cbm_k_basin();
-               } while (--i != 0);
+                /* Get the disk-format code. */
+                i = 6;
+                do {
+                    l_dirent->access = byte = cbm_k_basin();
+                } while (--i != 0);
 
             } else {
                 /* Go to the file-type column. */
@@ -135,16 +141,13 @@ unsigned char __fastcall__ cbm_readdir (unsigned char lfn, register struct cbm_d
                     byte = cbm_k_basin();
                 }
 
-                if (byte >= 'c' && byte < 'c' + sizeof types) {
-                    l_dirent->type = types[byte - 'c'];
-                } else {
-                    l_dirent->type = CBM_T_OTHER;
-                }
+                /* Determine the file type */
+                l_dirent->type = _cbm_filetype (byte);
 
-               /* Notice whether it's a directory or a deleted file. */
-               if (cbm_k_basin() == 'i' && byte == 'd') {
-                   l_dirent->type = CBM_T_DIR;
-               }
+                /* Notice whether it's a directory or a deleted file. */
+                if (cbm_k_basin() == 'i' && byte == 'd') {
+                    l_dirent->type = CBM_T_DIR;
+                }
                 cbm_k_basin();
 
                 /* Locked files shouldn't be written. */