Topic: POSIX API Experimentation - ls.c
This may be a question for the z88dk forum, I'm not sure, but I thought I'd try here first - please excuse me if I am in the wrong place...
I wanted to experiment with the POSIX API, so have been trying to reduce each use case to as simple an example as I could, to make them easier to explain to others....
In that regard some of the GNU tools seemed obvious, and thought I would *TRY* and do some tutorials, simple goals with with excessive commenting, to help other users get started with the filesystem access from z88dk.
So, far from perfect here is my first very-most-simple port of ls.c, the objective is to purely get me a list of files in the current folder... https://gist.github.com/Xalior/11f4f129 … 027f850dfd
Enclosed inline for ease, but github has syntax highlights, etc.
#include <stdio.h>
#include <stdlib.h>
#include <input.h>
#include <arch/zx.h>
#include <arch/zx/esxdos.h>
void main() {
unsigned char *cwd[128];
char *curr_dir = NULL;
unsigned char *dp = NULL;
struct esxdos_dirent *dptr = NULL;
unsigned int count = 0;
// Get the value of environment variable PWD
esxdos_f_getcwd(cwd);
if(NULL == cwd)
{
printf("\n ERROR : Could not get the working directory\n");
return -1;
} else {
printf(" CWD : %s\n", cwd);
}
// Open the current directory
dp = esxdos_f_opendir(cwd);
if(NULL == dp)
{
printf("\n ERROR : Could not open the working directory.\n");
return -1;
} else {
printf(" DONE : Opened the working directory. (%d)\n", dp);
}
// Go through and display all the names (files or folders)
// Contained in the directory.
for(count = 0; NULL != (esxdos_f_readdir(dp, dptr)); count++)
{
printf("Entry #%u: %u\n",count, *dptr->dir);
printf("Str@1 #%u: %s\n",count, *dptr+sizeof(unsigned char));
printf("-----\n");
}
esxdos_f_close(dp);
return 0;
}
Compiled with:
zcc +zx -vn -startup=30 -clib=new LS.c -o LS -subtype=dot -create-app
When I run the code I seem to end in an almost infinite loop with no filenames output.
The "logic" of '*dptr+sizeof(unsigned char)' is derived from reading the structure itself in the ESXDOS header code, to try and figure out the shape...
Can someone please give me a shove in the right direction? Hopefully that will be enough to unstick me...
-Dx
Edited: to fix a typo, and clarify source of sizeof logics.