Index: /trunk/src/emx/src/lib/bsd/gen/fts.c
===================================================================
--- /trunk/src/emx/src/lib/bsd/gen/fts.c	(revision 1965)
+++ /trunk/src/emx/src/lib/bsd/gen/fts.c	(revision 1966)
@@ -39,7 +39,14 @@
 #endif /* LIBC_SCCS and not lint */
 #endif
+#ifndef __EMX__
+# define HAVE_FCHDIR
+# define NEED_STRRSLASH
+# define IS_SLASH(ch)   ( (ch) == '/' || (ch) == '\\' )
+#else
+# define IS_SLASH(ch)   ( (ch) == '/' )
+#endif
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/lib/libc/gen/fts.c,v 1.27 2004/06/08 06:23:23 das Exp $");
+//__FBSDID("$FreeBSD: src/lib/libc/gen/fts.c,v 1.27 2004/06/08 06:23:23 das Exp $");
 
 #include "namespace.h"
@@ -75,5 +82,9 @@
 #define	SET(opt)	(sp->fts_options |= (opt))
 
+#ifdef HAVE_FCHDIR
 #define	FCHDIR(sp, fd)	(!ISSET(FTS_NOCHDIR) && fchdir(fd))
+#else
+#define	FCHDIR(sp, rdir) (!ISSET(FTS_NOCHDIR) && chdir(rdir))
+#endif
 
 /* fts_build flags */
@@ -94,4 +105,5 @@
 };
 
+#ifndef __EMX__
 /*
  * The "FTS_NOSTAT" option can avoid a lot of calls to stat(2) if it
@@ -110,4 +122,5 @@
 	0
 };
+#endif /* !__EMX__ */
 
 FTS *
@@ -212,6 +225,10 @@
 	 * descriptor we run anyway, just more slowly.
 	 */
+#ifdef HAVE_FCHDIR
 	if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = _open(".", O_RDONLY, 0)) < 0)
-		SET(FTS_NOCHDIR);
+#else
+        if (!ISSET(FTS_NOCHDIR) && (sp->fts_rdir = getcwd(NULL, 0)) != NULL)
+#endif
+            SET(FTS_NOCHDIR);
 
 	return (sp);
@@ -224,4 +241,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)
@@ -241,5 +276,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);
@@ -280,6 +319,11 @@
 	/* Return to original directory, save errno if necessary. */
 	if (!ISSET(FTS_NOCHDIR)) {
+#ifdef HAVE_FCHDIR
 		saved_errno = fchdir(sp->fts_rfd) ? errno : 0;
 		(void)_close(sp->fts_rfd);
+#else
+		saved_errno = chdir(sp->fts_rdir) ? errno : 0;
+                free(sp->fts_rdir); sp->fts_rdir = NULL;
+#endif
 
 		/* Set errno and return. */
@@ -297,4 +341,5 @@
 }
 
+
 /*
  * Special case of "/" at the end of the path so that slashes aren't
@@ -302,5 +347,5 @@
  */
 #define	NAPPEND(p)							\
-	(p->fts_path[p->fts_pathlen - 1] == '/'				\
+	(IS_SLASH(p->fts_path[p->fts_pathlen - 1])			\
 	    ? p->fts_pathlen - 1 : p->fts_pathlen)
 
@@ -341,4 +386,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)) < 0) {
 				p->fts_errno = errno;
@@ -346,4 +392,5 @@
 			} else
 				p->fts_flags |= FTS_SYMFOLLOW;
+#endif
 		}
 		return (p);
@@ -355,6 +402,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);
@@ -413,5 +462,9 @@
 		 */
 		if (p->fts_level == FTS_ROOTLEVEL) {
+#ifdef HAVE_FCHDIR
 			if (FCHDIR(sp, sp->fts_rfd)) {
+#else
+			if (FCHDIR(sp, sp->fts_rdir)) {
+#endif
 				SET(FTS_STOP);
 				return (NULL);
@@ -431,4 +484,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)) < 0) {
@@ -437,4 +491,5 @@
 				} else
 					p->fts_flags |= FTS_SYMFOLLOW;
+#endif
 			}
 			p->fts_instr = FTS_NOINSTR;
@@ -470,8 +525,13 @@
 	 */
 	if (p->fts_level == FTS_ROOTLEVEL) {
+#ifdef HAVE_FCHDIR
 		if (FCHDIR(sp, sp->fts_rfd)) {
+#else
+		if (FCHDIR(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)) {
@@ -483,4 +543,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, "..")) {
@@ -520,6 +583,10 @@
 {
 	FTSENT *p;
+#ifdef HAVE_FCHDIR
 	int fd;
-
+#else
+	char *pszRoot;
+        int rc;
+#endif
 	if (instr != 0 && instr != FTS_NAMEONLY) {
 		errno = EINVAL;
@@ -569,14 +636,25 @@
 	 * 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)) < 0)
+#else
+        if ((pszRoot = getcwd(NULL, 0)) == NULL)
+#endif
 		return (NULL);
 	sp->fts_child = fts_build(sp, instr);
+#ifdef HAVE_FCHDIR
 	if (fchdir(fd))
 		return (NULL);
 	(void)_close(fd);
+#else
+        rc = chdir(pszRoot);
+        free(pszRoot);
+        if (rc)
+            return NULL;
+#endif
 	return (sp->fts_child);
 }
@@ -653,4 +731,5 @@
 		oflag = DTF_HIDEW | DTF_NODUP | DTF_REWIND;
 #else
+        (void)(oflag);
 #define __opendir2(path, flag) opendir(path)
 #endif
@@ -705,5 +784,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;
@@ -870,5 +953,9 @@
 	if (descend && (type == BCHILD || !nitems) &&
 	    (cur->fts_level == FTS_ROOTLEVEL ?
+#ifdef HAVE_FCHDIR
 	    FCHDIR(sp, sp->fts_rfd) :
+#else
+	    FCHDIR(sp, sp->fts_rdir) :
+#endif
 	    fts_safe_changedir(sp, cur->fts_parent, -1, ".."))) {
 		cur->fts_info = FTS_ERR;
@@ -1172,7 +1259,12 @@
 	if (ISSET(FTS_NOCHDIR))
 		return (0);
+#ifdef HAVE_FCHDIR
 	if (fd < 0 && (newfd = _open(path, O_RDONLY, 0)) < 0)
 		return (-1);
 	if (_fstat(newfd, &sb)) {
+#else
+        (void)newfd;
+	if (_stat(path, &sb)) {
+#endif
 		ret = -1;
 		goto bail;
@@ -1183,10 +1275,18 @@
 		goto bail;
 	}
+#ifdef HAVE_FCHDIR
 	ret = fchdir(newfd);
+#else
+	ret = chdir(path);
+#endif
 bail:
+#ifdef HAVE_FCHDIR
 	oerrno = errno;
 	if (fd < 0)
 		(void)_close(newfd);
 	errno = oerrno;
+#else
+        (void)oerrno;
+#endif
 	return (ret);
 }
@@ -1202,4 +1302,9 @@
 
 	priv = (struct _fts_private *)sp;
+#ifdef __EMX__
+        /* we don't have reliable links */
+        priv->ftsp_linksreliable = 0;
+        (void)cpp;
+#else
 	/*
 	 * If this node's device is different from the previous, grab
@@ -1223,4 +1328,5 @@
 		}
 	}
+#endif
 	return (priv->ftsp_linksreliable);
 }
