diff --git a/HelloScript.gm9 b/HelloScript.gm9 index 3704a36..f687186 100644 --- a/HelloScript.gm9 +++ b/HelloScript.gm9 @@ -107,7 +107,8 @@ mv -w -k $[TESTPATH] $[RENPATH] # Here we use it to check for RENPATH, thus we use NULL as second argument (we're not interested in the output) find $[RENPATH] NULL # Wildcards ('*' / '?') are allowed when searching for a file / directory name -# If wildcards are used, 'find' will always return the last alphanumerical match +# If wildcards are used, 'find' will return the last alphanumerical match +# -f / --first return the first alphanumerical match instead find S:/nand.* NANDIMAGE # 'sha' COMMAND diff --git a/source/filesys/vff.c b/source/filesys/vff.c index 48c3382..ff44ba3 100644 --- a/source/filesys/vff.c +++ b/source/filesys/vff.c @@ -308,7 +308,7 @@ FRESULT fvx_preaddir (DIR* dp, FILINFO* fno, const TCHAR* pattern) { return res; } -FRESULT fvx_findpath (TCHAR* path, const TCHAR* pattern) { +FRESULT fvx_findpath (TCHAR* path, const TCHAR* pattern, BYTE mode) { strncpy(path, pattern, _MAX_FN_LEN); TCHAR* fname = strrchr(path, '/'); if (!fname) return FR_DENIED; @@ -327,8 +327,10 @@ FRESULT fvx_findpath (TCHAR* path, const TCHAR* pattern) { *fname = '\0'; while ((fvx_preaddir(&pdir, &fno, npattern) == FR_OK) && *(fno.fname)) { - if (strncmp(fno.fname, fname, _MAX_FN_LEN) > 0) + int cmp = strncmp(fno.fname, fname, _MAX_FN_LEN); + if (((mode & FN_HIGHEST) && (cmp > 0)) || ((mode & FN_LOWEST) && (cmp < 0)) || !(*fname)) strncpy(fname, fno.fname, _MAX_FN_LEN - (fname - path)); + if (!(mode & (FN_HIGHEST|FN_LOWEST))) break; } fvx_closedir( &pdir ); diff --git a/source/filesys/vff.h b/source/filesys/vff.h index b42459a..ffee4dd 100644 --- a/source/filesys/vff.h +++ b/source/filesys/vff.h @@ -8,6 +8,10 @@ #define fvx_tell(fp) ((fp)->fptr) #define fvx_size(fp) ((fp)->obj.objsize) +#define FN_ANY 0x00 +#define FN_HIGHEST 0x01 +#define FN_LOWEST 0x02 + // wrapper functions for ff.h + sddata.h // incomplete(!) extension to FatFS to support a common interface for virtual and FAT FRESULT fvx_open (FIL* fp, const TCHAR* path, BYTE mode); @@ -36,5 +40,5 @@ FRESULT fvx_runlink (const TCHAR* path); // additional wildcard based functions FRESULT fvx_match_name(const TCHAR* path, const TCHAR* pattern); FRESULT fvx_preaddir (DIR* dp, FILINFO* fno, const TCHAR* pattern); -FRESULT fvx_findpath (TCHAR* path, const TCHAR* pattern); +FRESULT fvx_findpath (TCHAR* path, const TCHAR* pattern, BYTE mode); FRESULT fvx_findnopath (TCHAR* path, const TCHAR* pattern); diff --git a/source/utils/scripting.c b/source/utils/scripting.c index 0d5f199..5655730 100644 --- a/source/utils/scripting.c +++ b/source/utils/scripting.c @@ -100,7 +100,7 @@ Gm9ScriptCmd cmd_list[] = { { CMD_ID_MKDIR , "mkdir" , 1, 0 }, { CMD_ID_MOUNT , "imgmount", 1, 0 }, { CMD_ID_UMOUNT , "imgumount",0, 0 }, - { CMD_ID_FIND , "find" , 2, 0 }, + { CMD_ID_FIND , "find" , 2, _FLG('f') }, { CMD_ID_FINDNOT , "findnot" , 2, 0 }, { CMD_ID_SHA , "sha" , 2, 0 }, { CMD_ID_SHAGET , "shaget" , 2, 0 }, @@ -369,6 +369,7 @@ u32 get_flag(char* str, u32 len, char* err_str) { if ((len < 2) || (*str != '-')) flag_char = '\0'; else if (len == 2) flag_char = str[1]; else if (strncmp(str, "--all", len) == 0) flag_char = 'a'; + else if (strncmp(str, "--first", len) == 0) flag_char = 'f'; else if (strncmp(str, "--hash", len) == 0) flag_char = 'h'; else if (strncmp(str, "--skip", len) == 0) flag_char = 'k'; else if (strncmp(str, "--legit", len) == 0) flag_char = 'l'; @@ -533,7 +534,8 @@ bool run_cmd(cmd_id id, u32 flags, char** argv, char* err_str) { InitImgFS(NULL); } else if (id == CMD_ID_FIND) { char path[_VAR_CNT_LEN]; - ret = (fvx_findpath(path, argv[0]) == FR_OK); + u8 mode = (flags & _FLG('f')) ? FN_LOWEST : FN_HIGHEST; + ret = (fvx_findpath(path, argv[0], mode) == FR_OK); if (err_str) snprintf(err_str, _ERR_STR_LEN, "find fail"); if (ret) { ret = set_var(argv[1], path);