Index: /trunk/src/gmake/kmkbuiltin/fts.c
===================================================================
--- /trunk/src/gmake/kmkbuiltin/fts.c	(revision 620)
+++ /trunk/src/gmake/kmkbuiltin/fts.c	(revision 621)
@@ -34,5 +34,5 @@
 #endif
 
-#include <sys/cdefs.h>
+/*#include <sys/cdefs.h>*/
 #if defined(LIBC_SCCS) && !defined(lint)
 #if 0
@@ -43,6 +43,8 @@
 #endif /* LIBC_SCCS and not lint */
 
-#include "namespace.h"
+/*#include "namespace.h"*/
+#ifndef _MSC_VER
 #include <sys/param.h>
+#endif
 #include <sys/stat.h>
 
@@ -51,8 +53,14 @@
 #include <errno.h>
 #include <fcntl.h>
-#include <fts.h>
+#include "ftsfake.h"
 #include <stdlib.h>
 #include <string.h>
+#ifndef _MSC_VER
 #include <unistd.h>
+#else
+#include "mscfakes.h"
+#define dirfd(dir) -1
+#endif 
+#include "ftsfake.h"
 
 #if ! HAVE_NBTOOL_CONFIG_H
@@ -60,4 +68,5 @@
 #endif
 
+#if 0
 #ifdef __weak_alias
 #ifdef __LIBC12_SOURCE__
@@ -69,4 +78,5 @@
 #endif /* __LIBC12_SOURCE__ */
 #endif /* __weak_alias */
+#endif 
 
 #ifdef __LIBC12_SOURCE__
@@ -94,16 +104,29 @@
 #endif
 
-static FTSENT	*fts_alloc __P((FTS *, const char *, size_t));
-static FTSENT	*fts_build __P((FTS *, int));
-static void	 fts_lfree __P((FTSENT *));
-static void	 fts_load __P((FTS *, FTSENT *));
-static size_t	 fts_maxarglen __P((char * const *));
-static size_t	 fts_pow2 __P((size_t));
-static int	 fts_palloc __P((FTS *, size_t));
-static void	 fts_padjust __P((FTS *, FTSENT *));
-static FTSENT	*fts_sort __P((FTS *, FTSENT *, size_t));
-static u_short	 fts_stat __P((FTS *, FTSENT *, int));
-static int	 fts_safe_changedir __P((const FTS *, const FTSENT *, int,
-    const char *));
+static FTSENT	*fts_alloc(FTS *, const char *, size_t);
+static FTSENT	*fts_build(FTS *, int);
+static void	 fts_lfree(FTSENT *);
+static void	 fts_load(FTS *, FTSENT *);
+static size_t	 fts_maxarglen(char * const *);
+static size_t	 fts_pow2(size_t);
+static int	 fts_palloc(FTS *, size_t);
+static void	 fts_padjust(FTS *, FTSENT *);
+static FTSENT	*fts_sort(FTS *, FTSENT *, size_t);
+static u_short	 fts_stat(FTS *, FTSENT *, int);
+static int	 fts_safe_changedir(const FTS *, const FTSENT *, int,
+    const char *);
+
+#ifdef _MSC_VER
+#undef HAVE_STRUCT_DIRENT_D_NAMLEN
+#undef HAVE_FCHDIR
+#endif 
+
+#if defined(__EMX__) || defined(_MSC_VER)
+# define NEED_STRRSLASH
+# define IS_SLASH(ch)   ( (ch) == '/' || (ch) == '\\' )
+#else
+# define HAVE_FCHDIR
+# define IS_SLASH(ch)   ( (ch) == '/' )
+#endif
 
 #define	ISDOT(a)	(a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
@@ -114,5 +137,10 @@
 
 #define	CHDIR(sp, path)	(!ISSET(FTS_NOCHDIR) && chdir(path))
+#ifdef HAVE_FCHDIR
 #define	FCHDIR(sp, fd)	(!ISSET(FTS_NOCHDIR) && fchdir(fd))
+#else
+#define	FCHDIR(sp, rdir) CHDIR(sp, rdir)
+#endif
+
 
 /* fts_build flags */
@@ -125,9 +153,14 @@
 #endif
 
+#ifndef _DIAGASSERT
+#define _DIAGASSERT assert
+#endif
+
+
 FTS *
 fts_open(argv, options, compar)
 	char * const *argv;
 	int options;
-	int (*compar) __P((const FTSENT **, const FTSENT **));
+	int (*compar)(const FTSENT **, const FTSENT **);
 {
 	FTS *sp;
@@ -225,4 +258,5 @@
 	 */
 	if (!ISSET(FTS_NOCHDIR)) {
+#ifdef HAVE_FCHDIR
 		if ((sp->fts_rfd = open(".", O_RDONLY, 0)) == -1)
 			SET(FTS_NOCHDIR);
@@ -231,4 +265,8 @@
 			SET(FTS_NOCHDIR);
 		}
+#else
+		if ((sp->fts_rdir = getcwd(NULL, 0)) != NULL)
+			SET(FTS_NOCHDIR);
+#endif 
 	}
 
@@ -242,4 +280,22 @@
 }
 
+#ifdef NEED_STRRSLASH
+static char *strrslash(register char *psz)
+{
+    register char ch;
+    char *pszLast = NULL;
+    for (; (ch = *psz); psz++)
+        switch (ch)
+        {
+            case '/':
+            case '\\':
+            case ':':
+                pszLast = psz;
+                break;
+        }
+    return pszLast;
+}
+#endif
+
 static void
 fts_load(sp, p)
@@ -262,5 +318,9 @@
 	len = p->fts_pathlen = p->fts_namelen;
 	memmove(sp->fts_path, p->fts_name, len + 1);
+#ifdef NEED_STRRSLASH
+	if ((cp = strrslash(p->fts_name)) && (cp != p->fts_name || cp[1])) {
+#else
 	if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
+#endif
 		len = strlen(++cp);
 		memmove(p->fts_name, cp, len + 1);
@@ -286,6 +346,8 @@
 	 */
 	if (sp->fts_cur) {
+#ifndef _MSC_VER
 		if (ISSET(FTS_SYMFOLLOW))
 			(void)close(sp->fts_cur->fts_symfd);
+#endif
 		for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
 			freep = p;
@@ -305,7 +367,14 @@
 	/* Return to original directory, save errno if necessary. */
 	if (!ISSET(FTS_NOCHDIR)) {
+#ifdef HAVE_FCHDIR
 		if (fchdir(sp->fts_rfd))
 			saved_errno = errno;
 		(void)close(sp->fts_rfd);
+#else
+		if (chdir(sp->fts_rdir))
+			saved_errno =  errno;
+        free(sp->fts_rdir); 
+		sp->fts_rdir = NULL;
+#endif 
 	}
 
@@ -368,4 +437,5 @@
 		p->fts_info = fts_stat(sp, p, 1);
 		if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
+#ifdef HAVE_FCHDIR
 			if ((p->fts_symfd = open(".", O_RDONLY, 0)) == -1) {
 				p->fts_errno = errno;
@@ -377,4 +447,5 @@
 			} else
 				p->fts_flags |= FTS_SYMFOLLOW;
+#endif
 		}
 		return (p);
@@ -386,6 +457,8 @@
 		if (instr == FTS_SKIP ||
 		    (ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
+#ifdef HAVE_FCHDIR
 			if (p->fts_flags & FTS_SYMFOLLOW)
 				(void)close(p->fts_symfd);
+#endif
 			if (sp->fts_child) {
 				fts_lfree(sp->fts_child);
@@ -443,5 +516,9 @@
 		 */
 		if (p->fts_level == FTS_ROOTLEVEL) {
+#ifdef HAVE_FCHDIR
 			if (FCHDIR(sp, sp->fts_rfd)) {
+#else
+			if (CHDIR(sp, sp->fts_rdir)) {
+#endif 
 				SET(FTS_STOP);
 				return (NULL);
@@ -461,4 +538,5 @@
 			p->fts_info = fts_stat(sp, p, 1);
 			if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
+#ifdef HAVE_FCHDIR
 				if ((p->fts_symfd =
 				    open(".", O_RDONLY, 0)) == -1) {
@@ -471,4 +549,5 @@
 				} else
 					p->fts_flags |= FTS_SYMFOLLOW;
+#endif
 			}
 			p->fts_instr = FTS_NOINSTR;
@@ -504,8 +583,13 @@
 	 */
 	if (p->fts_level == FTS_ROOTLEVEL) {
+#ifdef HAVE_FCHDIR
 		if (FCHDIR(sp, sp->fts_rfd)) {
+#else
+		if (CHDIR(sp, sp->fts_rdir)) {
+#endif 
 			SET(FTS_STOP);
 			return (NULL);
 		}
+#ifdef HAVE_FCHDIR
 	} else if (p->fts_flags & FTS_SYMFOLLOW) {
 		if (FCHDIR(sp, p->fts_symfd)) {
@@ -517,4 +601,7 @@
 		}
 		(void)close(p->fts_symfd);
+#else
+        (void)saved_errno;
+#endif
 	} else if (!(p->fts_flags & FTS_DONTCHDIR) &&
 	    fts_safe_changedir(sp, p->fts_parent, -1, "..")) {
@@ -558,5 +645,10 @@
 {
 	FTSENT *p;
+#ifdef HAVE_FCHDIR
 	int fd;
+#else
+	char *pszRoot;
+	int rc;
+#endif
 
 	_DIAGASSERT(sp != NULL);
@@ -609,11 +701,16 @@
 	 * fts_read will work.
 	 */
-	if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
+	if (p->fts_level != FTS_ROOTLEVEL || IS_SLASH(p->fts_accpath[0]) ||
 	    ISSET(FTS_NOCHDIR))
 		return (sp->fts_child = fts_build(sp, instr));
 
+#ifdef HAVE_FCHDIR
 	if ((fd = open(".", O_RDONLY, 0)) == -1)
+#else
+	if ((pszRoot = getcwd(NULL, 0)) == NULL)
+#endif
 		return (sp->fts_child = NULL);
 	sp->fts_child = fts_build(sp, instr);
+#ifdef HAVE_FCHDIR
 	if (fchdir(fd)) {
 		(void)close(fd);
@@ -621,4 +718,11 @@
 	}
 	(void)close(fd);
+#else
+	rc = chdir(pszRoot);
+	free(pszRoot);
+	if (rc)
+		return (NULL);
+#endif
+
 	return (sp->fts_child);
 }
@@ -718,5 +822,9 @@
 	cderrno = 0;
 	if (nlinks || type == BREAD) {
+#ifdef HAVE_FCHDIR
 		if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) {
+#else
+		if (fts_safe_changedir(sp, cur, dirfd(dirp), cur->fts_accpath)) {
+#endif
 			if (nlinks && type == BREAD)
 				cur->fts_errno = errno;
@@ -867,5 +975,9 @@
 	if (descend && (type == BCHILD || !nitems) &&
 	    (cur->fts_level == FTS_ROOTLEVEL ?
+#ifdef HAVE_FCHDIR
 	    FCHDIR(sp, sp->fts_rfd) :
+#else
+	    CHDIR(sp, sp->fts_rdir) :
+#endif 
 	    fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
 		cur->fts_info = FTS_ERR;
@@ -1003,5 +1115,5 @@
 		*ap++ = p;
 	qsort((void *)sp->fts_array, nitems, sizeof(FTSENT *), 
-		(int (*) __P((const void *, const void *)))sp->fts_compar);
+		(int (*)(const void *, const void *))sp->fts_compar);
 	for (head = *(ap = sp->fts_array); --nitems; ++ap)
 		ap[0]->fts_link = ap[1];
@@ -1041,4 +1153,5 @@
 		    (struct STAT *)ALIGN((u_long)(p->fts_name + namelen + 2));
 #else
+	(void)len;
 	if ((p = malloc(sizeof(FTSENT) + namelen)) == NULL)
 		return (NULL);
@@ -1202,4 +1315,5 @@
 		return 0;
 
+#ifdef HAVE_FCHDIR
 	if (oldfd < 0 && (fd = open(path, O_RDONLY)) == -1)
 		return -1;
@@ -1207,4 +1321,8 @@
 	if (fstat(fd, &sb) == -1)
 		goto bail;
+#else
+	if (stat(path, &sb))
+		goto bail;
+#endif
 
 	if (sb.st_ino != p->fts_ino || sb.st_dev != p->fts_dev) {
@@ -1213,7 +1331,12 @@
 	}
 
+#ifdef HAVE_FCHDIR
 	ret = fchdir(fd);
+#else
+	ret = chdir(path);
+#endif
 
 bail:
+#ifdef HAVE_FCHDIR
 	if (oldfd < 0) {
 		int save_errno = errno;
@@ -1221,4 +1344,5 @@
 		errno = save_errno;
 	}
+#endif
 	return ret;
 }
Index: /trunk/src/gmake/kmkbuiltin/ftsfake.h
===================================================================
--- /trunk/src/gmake/kmkbuiltin/ftsfake.h	(revision 620)
+++ /trunk/src/gmake/kmkbuiltin/ftsfake.h	(revision 621)
@@ -41,5 +41,9 @@
 	dev_t fts_dev;			/* starting device # */
 	char *fts_path;			/* path for this descent */
+#ifdef _MSC_VER
+	char *fts_rdir;		        /* path of root */
+#else
 	int fts_rfd;			/* fd for root */
+#endif
 	u_int fts_pathlen;		/* sizeof(path) */
 	u_int fts_nitems;		/* elements in the sort array */
@@ -54,5 +58,7 @@
 #define	FTS_SEEDOT	0x020		/* return dot and dot-dot */
 #define	FTS_XDEV	0x040		/* don't cross devices */
+#ifndef _MSC_VER
 #define	FTS_WHITEOUT	0x080		/* return whiteout information */
+#endif 
 #define	FTS_OPTIONMASK	0x0ff		/* valid user option mask */
 
@@ -71,5 +77,7 @@
 	char *fts_path;			/* root path */
 	int fts_errno;			/* errno for this node */
+#ifndef _MSC_VER
 	int fts_symfd;			/* fd for symlink */
+#endif 
 	u_short fts_pathlen;		/* strlen(fts_path) */
 	u_short fts_namelen;		/* strlen(fts_name) */
@@ -80,5 +88,9 @@
 	u_int16_t fts_nlink;		/* link count */
 #else
+#ifndef _MSC_VER
 	nlink_t fts_nlink;		/* link count */
+#else
+	int fts_nlink;		/* link count */
+#endif 
 #endif
 
@@ -100,10 +112,14 @@
 #define	FTS_SL		12		/* symbolic link */
 #define	FTS_SLNONE	13		/* symbolic link without target */
+#ifndef _MSC_VER
 #define	FTS_W		14		/* whiteout object */
+#endif
 	u_short fts_info;		/* user flags for FTSENT structure */
 
 #define	FTS_DONTCHDIR	 0x01		/* don't chdir .. to the parent */
 #define	FTS_SYMFOLLOW	 0x02		/* followed a symlink to get here */
+#ifndef _MSC_VER
 #define	FTS_ISW		 0x04		/* this is a whiteout object */
+#endif 
 	u_short fts_flags;		/* private flags for FTSENT structure */
 
@@ -122,7 +138,11 @@
 } FTSENT;
 
+#ifndef _MSC_VER
 #include <sys/cdefs.h>
+__BEGIN_DECLS
+#else
+#define __RENAME(a) 
+#endif 
 
-__BEGIN_DECLS
 #ifdef __LIBC12_SOURCE__
 FTSENT	*fts_children(FTS *, int);
@@ -141,5 +161,8 @@
 int	 fts_set(FTS *, FTSENT *, int)	__RENAME(__fts_set13);
 #endif
+
+#ifndef _MSC_VER
 __END_DECLS
+#endif
 
 #endif /* !_FTS_H_ */
