Page 1 of 1

How to access Files and Directories on the SD card with CC65

Posted: Wed Aug 11, 2021 2:00 pm
by rje

The commands for file and directory access are in <cbm.h>.  Here are the example functions I used to try them out.

unsigned char readFile(const char *filename)
{
    char s[200];
    unsigned char lfn = 1;
    unsigned char dev = 8;
    unsigned char sec_addr = 0;
    unsigned char res = cbm_open(lfn, dev, sec_addr, filename);
    if (res == 0)
    {
        cbm_read(lfn, s, 200);
        cbm_close(lfn);
        printf("data: %s\r\n", s);
    }
    
    return res;
}

void readFile2(const char *filename)
{
char buf[200];
FILE *fp = fopen( filename, "r" );

if (fp)
{
fgets(buf, 100, fp);
fclose(fp);
cprintf("data: %s\r\n", buf);
}
}

unsigned char readDir(const char* path)
{
    struct cbm_dirent dir;
    unsigned char res = cbm_opendir(1, 8, path);
    printf("dir: [%s]\n", path);
    if (res == 0)
    {
        printf("listing for %s:\n", path);
        cbm_readdir(1, &dir); // disk header
        printf("file              blocks     type  access\n"
               "----              ------     ----  ------\n");
        while (cbm_readdir(1, &dir) == 0) 
        {
            switch(dir.type)
            {
                case 2:  textcolor(8); break;
                case 17: textcolor(7); break;
            }
            cprintf("%-18s %5u     %3s   %u\r\n", 
                dir.name, 
                dir.size, 
                dir.type == 2? "dir" : dir.type == 17? "prg" : "???",
                dir.access);
            textcolor(1);
        }
        cbm_closedir(1);
    }
    return res;
}


How to access Files and Directories on the SD card with CC65

Posted: Wed Aug 11, 2021 2:13 pm
by rje

Tom adds an additional note about changing directories:


Quote




To change directories on the CMDR-DOS device, you need to open a file with device 8 and secondary address 15. Set the filename to the DOS command to change directory:



"CD:directoryname"



After that, you can close the channel. (In fact, you should close it. If you don't, you will eventually get an error as the system runs out of free I/O channels.)