From 79da9cf8e7169408119b8cf1f8121235cabcd620 Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Sat, 24 Aug 2002 21:58:02 +0000
Subject: [PATCH] Added Watcom C/C++ support.

---
 src/xpdev/dirwrap.c | 77 ++++++++++++++++++++++++++++++++++++++++-----
 src/xpdev/dirwrap.h |  2 ++
 2 files changed, 71 insertions(+), 8 deletions(-)

diff --git a/src/xpdev/dirwrap.c b/src/xpdev/dirwrap.c
index 03d80cee03..c788a0181a 100644
--- a/src/xpdev/dirwrap.c
+++ b/src/xpdev/dirwrap.c
@@ -62,6 +62,10 @@
 
 #endif /* __unix__ */
 
+#if defined(__WATCOMC__)
+	#include <dos.h>
+#endif
+	
 #include <sys/types.h>	/* _dev_t */
 #include <sys/stat.h>	/* struct stat */
 
@@ -136,6 +140,65 @@ static int glob_compare( const void *arg1, const void *arg2 )
 	#pragma argsused
 #endif
 
+#if defined(__WATCOMC__)
+
+int	DLLCALL	glob(const char *pattern, int flags, void* unused, glob_t* glob)
+{
+    struct	find_t ff;
+	size_t	found=0;
+	char	path[MAX_PATH+1];
+	char*	p;
+	char**	new_pathv;
+
+	if(!(flags&GLOB_APPEND)) {
+		glob->gl_pathc=0;
+		glob->gl_pathv=NULL;
+	}
+
+	if(_dos_findfirst((char*)pattern,_A_NORMAL,&ff)!=0)
+		return(GLOB_NOMATCH);
+
+	do {
+		if(!(flags&GLOB_ONLYDIR) || ff.attrib&_A_SUBDIR) {
+			if((new_pathv=realloc(glob->gl_pathv
+				,(glob->gl_pathc+1)*sizeof(char*)))==NULL) {
+				globfree(glob);
+				return(GLOB_NOSPACE);
+			}
+			glob->gl_pathv=new_pathv;
+
+			/* build the full pathname */
+			SAFECOPY(path,pattern);
+			p=getfname(path);
+			*p=0;
+			strcat(path,ff.name);
+
+			if((glob->gl_pathv[glob->gl_pathc]=malloc(strlen(path)+2))==NULL) {
+				globfree(glob);
+				return(GLOB_NOSPACE);
+			}
+			strcpy(glob->gl_pathv[glob->gl_pathc],path);
+			if(flags&GLOB_MARK && ff.attrib&_A_SUBDIR)
+				strcat(glob->gl_pathv[glob->gl_pathc],"/");
+
+			glob->gl_pathc++;
+			found++;
+		}
+	} while(_dos_findnext(&ff)==0);
+	_dos_findclose(&ff);
+
+	if(found==0)
+		return(GLOB_NOMATCH);
+
+	if(!(flags&GLOB_NOSORT)) {
+		qsort(glob->gl_pathv,found,sizeof(char*),glob_compare);
+	}
+
+	return(0);	/* success */
+}
+
+#else
+
 int	DLLCALL	glob(const char *pattern, int flags, void* unused, glob_t* glob)
 {
     struct	_finddata_t ff;
@@ -193,6 +256,8 @@ int	DLLCALL	glob(const char *pattern, int flags, void* unused, glob_t* glob)
 	return(0);	/* success */
 }
 
+#endif
+
 void DLLCALL globfree(glob_t* glob)
 {
 	size_t i;
@@ -344,7 +409,9 @@ BOOL DLLCALL fexist(const char *filespec)
 
 	return(TRUE);
 
-#elif defined(__unix__)	/* portion by cmartin */
+#else /* Unix or OS/2 */
+	
+	/* portion by cmartin */
 
 	glob_t g;
     int c;
@@ -373,12 +440,6 @@ BOOL DLLCALL fexist(const char *filespec)
     globfree(&g);
     return FALSE;
 
-#else
-
-#warning "fexist() port needs to support wildcards!"
-
-	return(FALSE);
-
 #endif
 }
 
@@ -555,7 +616,7 @@ ulong DLLCALL getfreediskspace(const char* path)
     
 #else
 
-	#warning OS-specific code needed here
+	fprintf(stderr,"\n*** !Missing getfreediskspace implementation ***\n");
 	return(0);
 
 #endif
diff --git a/src/xpdev/dirwrap.h b/src/xpdev/dirwrap.h
index c5687505cb..f903ac8d61 100644
--- a/src/xpdev/dirwrap.h
+++ b/src/xpdev/dirwrap.h
@@ -137,6 +137,8 @@ extern "C" {
 	struct dirent  *	readdir  (DIR *__dir);
 	int                 closedir (DIR *__dir);
 	void                rewinddir(DIR *__dir);
+#elif defined(__WATCOMC__)
+	#include <direct.h>	/* opendir, etc defined here in Watcom */
 #else
 	#include <dirent.h>	/* POSIX directory functions */
 #endif
-- 
GitLab