diff --git a/firmware/common/dir.c b/firmware/common/dir.c index e1f4c064a0..5fb1415fb0 100644 --- a/firmware/common/dir.c +++ b/firmware/common/dir.c @@ -17,15 +17,16 @@ * ****************************************************************************/ #include +#include #include #include #include "fat.h" #include "dir.h" #include "debug.h" -static DIR thedir; -static struct dirent theent; -static bool busy=false; +#define MAX_OPEN_DIRS 8 + +static DIR opendirs[MAX_OPEN_DIRS]; DIR* opendir(char* name) { @@ -33,20 +34,30 @@ DIR* opendir(char* name) char* part; char* end; struct fat_direntry entry; - struct fat_dir* dir = &(thedir.fatdir); + int dd; - if ( busy ) { - DEBUGF("Only one open dir at a time\n"); + /* find a free dir descriptor */ + for ( dd=0; ddbusy=false; return 0; } struct dirent* readdir(DIR* dir) { struct fat_direntry entry; + struct dirent* theent = &(dir->theent); if (fat_getnext(&(dir->fatdir),&entry) < 0) return NULL; @@ -95,12 +108,12 @@ struct dirent* readdir(DIR* dir) if ( !entry.name[0] ) return NULL; - strncpy(theent.d_name, entry.name, sizeof( theent.d_name ) ); - theent.attribute = entry.attr; - theent.size = entry.filesize; - theent.startcluster = entry.firstcluster; + strncpy(theent->d_name, entry.name, sizeof( theent->d_name ) ); + theent->attribute = entry.attr; + theent->size = entry.filesize; + theent->startcluster = entry.firstcluster; - return &theent; + return theent; } /* diff --git a/firmware/common/dir.h b/firmware/common/dir.h index 274c0b1ea4..0cd35d063b 100644 --- a/firmware/common/dir.h +++ b/firmware/common/dir.h @@ -19,6 +19,8 @@ #ifndef _DIR_H_ #define _DIR_H_ +#include + #ifndef DIRENT_DEFINED #define ATTR_READ_ONLY 0x01 @@ -42,8 +44,10 @@ struct dirent { #include "fat.h" typedef struct { + bool busy; int startcluster; struct fat_dir fatdir; + struct dirent theent; } DIR; #else // SIMULATOR