Commits (2)
......@@ -304,9 +304,28 @@ bool sbbs_t::menu_exists(const char *code, const char* ext, char* path)
FULLPATH(path, prefix, MAX_PATH);
SAFECOPY(prefix, path);
}
safe_snprintf(path, MAX_PATH, "%s.%lucol.%s", prefix, cols, ext);
if(fexistcase(path))
return true;
glob_t g = {0};
safe_snprintf(path, MAX_PATH, "%s.*col.%s", prefix, ext);
if(globi(path, GLOB_NOESCAPE|GLOB_MARK, NULL, &g) == 0) {
char* p;
char term[MAX_PATH + 1];
safe_snprintf(term, sizeof(term), "col.%s", ext);
size_t skip = safe_snprintf(path, MAX_PATH, "%s.", prefix);
long max = 0;
for(size_t i = 0; i < g.gl_pathc; i++) {
long c = strtol(g.gl_pathv[i] + skip, &p, 10);
if(stricmp(p, term) != 0) // Some other weird pattern ending in col.<ext>
continue;
if(c <= cols && c > max) {
max = c;
safe_snprintf(path, MAX_PATH, "%s", g.gl_pathv[i]);
}
}
globfree(&g);
if(max > 0)
return true;
}
safe_snprintf(path, MAX_PATH, "%s.%s", prefix, ext);
return fexistcase(path) ? true : false;
}
......
......@@ -305,7 +305,29 @@ void globfree(glob_t* glob)
glob->gl_pathc=0;
}
#endif /* !defined(__unix__) */
#else /* __unix__ */
// Case-insensitive version of glob()
int globi(const char *p, int flags,
int (*errfunc) (const char *epath, int eerrno),
glob_t *g)
{
char pattern[MAX_PATH * 2] = "";
int len = 0;
if(p != NULL) {
while(*p != '\0' && len < MAX_PATH) {
if(IS_ALPHA(*p))
len += sprintf(pattern + len, "[%c%c]", toupper(*p), tolower(*p));
else
pattern[len++] = *p;
}
}
pattern[len] = '\0';
return glob(pattern, flags, errfunc, g);
}
#endif
/****************************************************************************/
/* Returns number of files and/or sub-directories in directory (path) */
......
......@@ -50,6 +50,10 @@ extern "C" {
#include <sys/types.h>
#include <sys/stat.h>
#include <glob.h> /* POSIX.2 directory pattern matching function */
// Case-insensitive glob
int globi(const char *pattern, int flags, int (*errfunc) (const char *epath, int eerrno), glob_t *pglob);
#define MKDIR(dir) mkdir(dir,0777)
#if defined(__CYGWIN__)
......@@ -107,6 +111,7 @@ extern "C" {
DLLEXPORT int glob(const char *pattern, int flags, void* unused, glob_t*);
DLLEXPORT void globfree(glob_t*);
#define globi(a,b,c,d) glob(a,b,c,d)
#endif
......