From cd8e80aad5cc327d26cea0bda3f151c008bb06b1 Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Sat, 26 Aug 2017 06:44:14 +0000
Subject: [PATCH] Added getfilecount(), similar, but not identical to
 getdirsize().

---
 src/xpdev/dirwrap.c | 29 +++++++++++++++++++++++++++++
 src/xpdev/dirwrap.h |  1 +
 2 files changed, 30 insertions(+)

diff --git a/src/xpdev/dirwrap.c b/src/xpdev/dirwrap.c
index eb8e46a58f..5bfbaa2ede 100644
--- a/src/xpdev/dirwrap.c
+++ b/src/xpdev/dirwrap.c
@@ -300,6 +300,10 @@ void DLLCALL globfree(glob_t* glob)
 
 #endif /* !defined(__unix__) */
 
+/****************************************************************************/
+/* Returns number of files and/or sub-directories in directory (path)		*/
+/* Similar, but not identical, to getfilecount()							*/
+/****************************************************************************/
 long DLLCALL getdirsize(const char* path, BOOL include_subdirs, BOOL subdir_only)
 {
 	char		match[MAX_PATH+1];
@@ -735,6 +739,31 @@ ulong DLLCALL delfiles(const char *inpath, const char *spec)
 	return(files);
 }
 
+/****************************************************************************/
+/* Returns number of files in a directory (inpath) matching 'pattern'		*/
+/* Similar, but not identical, to getdirsize(), e.g. subdirs never counted	*/
+/****************************************************************************/
+ulong DLLCALL getfilecount(const char *inpath, const char* pattern)
+{
+	char path[MAX_PATH+1];
+	glob_t	g;
+	uint	gi;
+	ulong	count = 0;
+
+	SAFECOPY(path, inpath);
+	backslash(path);
+	strcat(path, pattern);
+	if(glob(path, GLOB_MARK, NULL, &g))
+		return 0;
+	for(gi = 0; gi < g.gl_pathc; ++gi) {
+		if(*lastchar(g.gl_pathv[gi]) == '/')
+			continue;
+		count++;
+	}
+	globfree(&g);
+	return count;
+}
+
 /****************************************************************************/
 /* Return free disk space in bytes (up to a maximum of 4GB)					*/
 /****************************************************************************/
diff --git a/src/xpdev/dirwrap.h b/src/xpdev/dirwrap.h
index e1203fb451..dd3de391ee 100644
--- a/src/xpdev/dirwrap.h
+++ b/src/xpdev/dirwrap.h
@@ -227,6 +227,7 @@ DLLEXPORT BOOL		DLLCALL isfullpath(const char* filename);
 DLLEXPORT char*		DLLCALL getfname(const char* path);
 DLLEXPORT char*		DLLCALL getfext(const char* path);
 DLLEXPORT int		DLLCALL getfattr(const char* filename);
+DLLEXPORT ulong		DLLCALL getfilecount(const char *inpath, const char* spec);
 DLLEXPORT long		DLLCALL	getdirsize(const char* path, BOOL include_subdirs, BOOL subdir_only);
 DLLEXPORT ulong		DLLCALL getdisksize(const char* path, ulong unit);
 DLLEXPORT ulong		DLLCALL getfreediskspace(const char* path, ulong unit);
-- 
GitLab