From bb721a409713f232e6a125e339e7ea39f40ee372 Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Sat, 12 Jan 2019 11:56:27 +0000
Subject: [PATCH] Added getdirname(): returns the filename or lastdirectory
 portion of a full pathname (directories must end in a '/'). Unlike getfname()
 which returns an empty string if passed a path ending in a '/'. Useful with
 glob(.., GLOB_MARK).

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

diff --git a/src/xpdev/dirwrap.c b/src/xpdev/dirwrap.c
index d536206ea1..74b6a69309 100644
--- a/src/xpdev/dirwrap.c
+++ b/src/xpdev/dirwrap.c
@@ -108,7 +108,26 @@ char* DLLCALL getfname(const char* path)
 }
 
 /****************************************************************************/
-/* Return a pointer to a file's extesion (beginning with '.')				*/
+/* Return the filename or last directory portion of a full pathname			*/
+/* A directory pathname is expected to end in a '/'							*/
+/****************************************************************************/
+char* DLLCALL getdirname(const char* path)
+{
+	char* last = lastchar(path);
+	if(*last == '/') {
+		if(last == path)
+			return last;
+		for(last--; last > path; last--) {
+			if(*last == '/' || *last == '\\')
+				return last + 1;
+		}
+		return last;
+	}
+	return getfname(path);
+}
+
+/****************************************************************************/
+/* Return a pointer to a file's extension/suffix (beginning with '.')		*/
 /****************************************************************************/
 char* DLLCALL getfext(const char* path)
 {
diff --git a/src/xpdev/dirwrap.h b/src/xpdev/dirwrap.h
index 50c1199ff6..d33e940e31 100644
--- a/src/xpdev/dirwrap.h
+++ b/src/xpdev/dirwrap.h
@@ -228,6 +228,7 @@ 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 char*		DLLCALL getdirname(const char* path);
 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