📄 findx.c
字号:
/*************************************************************
File Name: findx.C (RAMDisk 4.1) *
**************************************************************
Programmer: MSC
Last Modified Date: 2000/10/30
Compiler : GNU Cross-compiler/SDS
Platform : X86 protection mode, MIPS, Dragonball
Usage :
int RD_findfirst(unsigned char *target, struct ffblk *ffres, int attrib);
int RD_findnext(struct ffblk *ffres);
int RD_findclose(struct ffblk *ffres);
*************************************************************/
/*************************************************************
Header Files
**************************************************************/
#include <sys/syscall.h>
//#include "myansi.h"
#include <ramdisk.h>
#include <findx.h>
#ifdef RAMDISK_ID
/* basic.c */
extern int InitRD;
//extern int RDerrno;
extern void *RD_BootBegin;
extern int RD_SemaphoreID;
/*************************************************************
Function : RD_findfirst
Description:
find the first file that match the requirement
Input:
target - a wildcard that specifies the directory and files to search
ffres - a structure to hold the results of the search
attrib - the target attribute
Output:
0 if a match has been found
-1 if the searching failed
Note:
There is a hidden attribute in [attrib]. If the highest
bit in [attrib] is 0, only directories will be found
if DA_DIR is set. If the highest bit in [attrib] is 1,
both files and directories will be found if DA_DIR is
set.
**************************************************************/
int RD_findfirst(unsigned char *target, struct ffblk *ffres, int attrib)
{
int status;
RDerrno = 0;
if (InitRD == FALSE)
{
ffres->ff_reserved = 0;
RDerrno = ERROR_FILE_SYSTEM_NOT_INIT;
return -1;
}
sc_waitSemaphore(RD_SemaphoreID);
status = RD_findfirst_r(target, ffres, attrib);
sc_signalSemaphore(RD_SemaphoreID);
return status;
}
int RD_findfirst_r(unsigned char *target, struct ffblk *ffres, int attrib)
{
struct ffInternal *ffInfo; /* internal information for findnext */
struct diskBlock *curBlock;
struct directory *curEntry;
long dirSize;
long i;
long namePos = -1;
unsigned char *subDirName;
unsigned char *wildName;
struct RD_FILE *pfile;
#ifdef CURRENT_DIR_ENABLE
unsigned char *completeName = NULL;
#endif
#ifdef CURRENT_DIR_ENABLE
completeName = (unsigned char *)ap_malloc(MAX_PATH_LENGTH);
if (completeName == NULL)
{
RDerrno = ERROR_ALLOC_MEM;
return -1;
}
// get the complete pathname
if (RD_getCompletePathname(target, completeName) == -1)
{
ap_free(completeName);
return -1;
}
target = completeName;
#endif
// this allocated buffer is actually used to store 2 data structures
// the first MAX_PATH_LENGTH bytes is used to store the sub-dir pathname
// and the remaining portion is used as a temporary file structure for RD_f_search
subDirName = (unsigned char *)ap_malloc(MAX_PATH_LENGTH + sizeof(struct RD_FILE) + 10);
if (subDirName == NULL)
{
#ifdef CURRENT_DIR_ENABLE
ap_free(completeName);
#endif
ffres->ff_reserved = 0;
RDerrno = ERROR_ALLOC_MEM;
return -1;
}
memoryZero((void *)subDirName, (unsigned int)(MAX_PATH_LENGTH + sizeof(struct RD_FILE) + 10));
// find the last '\\' in target
i = 0;
while (target[i] != '\0')
{
/* marked by chilong 05/30/2002
if (target[i] == '\\' || target[i] == '/')
namePos = i;
subDirName[i] = target[i];
i++;
if (i >= MAX_PATH_LENGTH)
{
ffres->ff_reserved = 0;
ap_free(subDirName);
#ifdef CURRENT_DIR_ENABLE
ap_free(completeName);
#endif
RDerrno = ERROR_PATHNAME_TOO_LONG;
return -1;
}
marked by chilong 05/30/2002 */
/**** modified by chilong 05/30/2002 ****/
#ifdef CHINESE_CODE_USED
if (target[i] > 0x80)
{
subDirName[i] = target[i];
subDirName[i+1] = target[i+1];
i+=2;
}
else
{
if (target[i] == '\\' || target[i] == '/')
namePos = i;
subDirName[i] = target[i];
i++;
}
if (i >= MAX_PATH_LENGTH)
{
ffres->ff_reserved = 0;
ap_free(subDirName);
#ifdef CURRENT_DIR_ENABLE
ap_free(completeName);
#endif
RDerrno = ERROR_PATHNAME_TOO_LONG;
return -1;
}
#else
if (target[i] == '\\' || target[i] == '/')
namePos = i;
subDirName[i] = target[i];
i++;
if (i >= MAX_PATH_LENGTH)
{
ffres->ff_reserved = 0;
ap_free(subDirName);
#ifdef CURRENT_DIR_ENABLE
ap_free(completeName);
#endif
RDerrno = ERROR_PATHNAME_TOO_LONG;
return -1;
}
#endif
/**** modified by chilong 05/30/2002 ****/
}
namePos++;
wildName = &target[namePos];
if (wildName[0] == '\0')
{
ffres->ff_reserved = 0;
ap_free(subDirName);
#ifdef CURRENT_DIR_ENABLE
ap_free(completeName);
#endif
RDerrno = ERROR_FILE_NOT_EXIST;
return -1;
}
subDirName[namePos] = '\0';
namePos--;
/* marked by chilong 05/30/2002
while (namePos >= 0 && (subDirName[namePos] == '\\' || subDirName[namePos] == '/'))
{
subDirName[namePos] = '\0';
namePos--;
}
marked by chilong 05/30/2002 */
/**** modified by chilong 05/30/2002 ****/
#ifdef CHINESE_CODE_USED
while ( (namePos == 0 || (namePos > 0 && subDirName[namePos-1] <= 0x80)) &&
(subDirName[namePos] == '\\' || subDirName[namePos] == '/')
)
#else
while (namePos >= 0 && (subDirName[namePos] == '\\' || subDirName[namePos] == '/'))
#endif
{
subDirName[namePos] = '\0';
namePos--;
}
/**** modified by chilong 05/30/2002 ****/
// if target = "\\dir1\\dir2\\dir3\\*.*"
// now wildName = "*.*"
// subDirName = "\\dir1\\dir2\\dir3"
if (subDirName[0] == '\0')
{
// searching in the root dir
curBlock = (struct diskBlock *)(((struct signature *)RD_BootBegin)->rootBegin);
}
else
{
// searching in the sub-dir
pfile = (struct RD_FILE *)((unsigned long)subDirName + MAX_PATH_LENGTH);
if (RD_f_search(pfile, subDirName) != 0)
{
// search failed, directory not found
ffres->ff_reserved = 0;
ap_free(subDirName);
#ifdef CURRENT_DIR_ENABLE
ap_free(completeName);
#endif
RDerrno = ERROR_DIR_NOT_EXIST;
return -1;
}
curBlock = (pfile->dirEntry)->startBlock;
}
ap_free(subDirName);
dirSize = sizeof(struct directory);
// start looking for the file now
// curBlock is the starting block of the directory to be searched
// wildName contains only the wildcard name to be searched
while (curBlock != NULL)
{
curEntry = (struct directory *)((unsigned long)curBlock + sizeof(struct diskBlock));
// compare entry by entry in the sector
for (i = 0; i < DIR_BLOCK_SIZE; i += dirSize)
{
// this entry and every following entry are empty => not found
if (curEntry->name[0] == '\0')
{
#ifdef CURRENT_DIR_ENABLE
ap_free(completeName);
#endif
ffres->ff_reserved = 0;
RDerrno = ERROR_FILE_NOT_EXIST;
return -1;
}
// skip this entry if it is an deleted entry or "." or ".."
if (curEntry->name[0] == DELETED_ENTRY)
{
curEntry = (struct directory *)((unsigned long)curEntry + dirSize);
continue;
}
// compare the attribute of this entry with the given attribute
// DA_READONLY and DA_ARCHIVE files can always be seen
// DA_HIDDEN, DA_SYSTEM, DA_VOLUME, and DA_DIR are not
attrib = attrib | DA_READONLY;
attrib = attrib | DA_ARCHIVE;
// go to next entry if the attributes unmatch
if ((curEntry->attribute & attrib) != curEntry->attribute)
{
curEntry = (struct directory *)((unsigned long)curEntry + dirSize);
continue;
}
// DIR are shown only if DA_DIR is specified by user
/* marked by chilong 03/07/2002
if ((attrib & DA_DIR) == DA_DIR)
if ((curEntry->attribute & DA_DIR) != DA_DIR)
{
curEntry = (struct directory *)((unsigned long)curEntry + dirSize);
continue;
}
marked by chilong 03/07/2002 */
/**** modified by MSC 03/07/2002 ****/
if ((attrib & FF_DA_DIR_OR) == 0)
{
// DIR are shown only if DA_DIR is specified by user
if ((attrib & DA_DIR) == DA_DIR)
if ((curEntry->attribute & DA_DIR) != DA_DIR)
{
curEntry = (struct directory *)((unsigned long)curEntry + dirSize);
continue;
}
}
/**** modified by MSC 03/07/2002 ****/
// attributes match, compare filename with the wildcard name
if (RD_nameCompare(wildName, curEntry->name) == 1)
{
// allocate the internal findfile structure, used by findnext()
// this structure is deallocated by findclose()
ffInfo = (struct ffInternal *)ap_malloc(sizeof(struct ffInternal));
if (ffInfo == NULL)
{
#ifdef CURRENT_DIR_ENABLE
ap_free(completeName);
#endif
ffres->ff_reserved = 0;
RDerrno = ERROR_ALLOC_MEM;
return -1;
}
memoryZero((void *)ffInfo, (unsigned int)sizeof(struct ffInternal));
// fill in the ffInternal structure
ffInfo->attrib = attrib;
// the entry number, counting from the 1st root sector
ffInfo->nThEntry = i / dirSize;
// the cluster number of the directory, 0 for root
ffInfo->block = curBlock;
// the wildcard name
myStrCpy((char *)ffInfo->wildname, (const char *)wildName);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -