+/* 5653270
+ * For directories containing > 64k subdirectories (or HFS+ with > 64k files
+ * and subdirectories), struct stat's st_nlink (16 bits) will overflow. This
+ * causes the case with FTS_NOSTAT and FTS_PHYSICAL set to prematurely stop
+ * recursing into subdirectories, because of an optimization that expects
+ * st_nlink to be the number of subdirectories (once that number has been
+ * encountered, no further calls to stat should be needed).
+ *
+ * However, on Mac OS X, another optimization largely nullifies the st_nlink
+ * optimization. struct dirent contains d_type, which can distinguish
+ * directories from files without initially calling stat. So stat is only
+ * called on known directories, rather than on other files. With this
+ * optimization, the difference in also using the st_nlink optimization is
+ * pretty minimal (tests show an improvement of a percent or two, probably
+ * due to additional if statement clauses that need to be evaluated).
+ *
+ * So removing the st_nlink optimization code will fix the > 64k subdirectories
+ * problem. And if we replace the multiple if clause logic with a single
+ * switch statement, we can recover the minimal performance lose. We can
+ * go even further and for the case of FTS_NOSTAT and FTS_LOGICAL set, we
+ * can use d_type to also distinguish symbolic links, and so we only need to
+ * call stat on directories and symlinks, not on all files. This provides
+ * a significant performance boost in that special case.
+ */
+/*
+ * The following macros defines values of the dostat variable, which is or-ed
+ * with the value of d_type, and the result used in a switch statement to
+ * determine whether to call stat or not. (We order the macros to minimize
+ * the size of any jump table that the compiler may generate.)
+ */
+#define F_SHIFT 4 /* shift to leave space for d_type */
+#define F_NOSTAT (0 << F_SHIFT) /* don't do any stat's */
+#define F_STATDIRSYM (1 << F_SHIFT) /* only stat directories and symlinks (and unknowns) */
+#define F_ALWAYSSTAT (2 << F_SHIFT) /* always stat */
+#define F_STATDIR (3 << F_SHIFT) /* only stat directories (and unknowns) */
+
+static FTS *
+__fts_open(argv, sp)