Fix various ftw/nftw "shall fail"s from POSIX.

POSIX says ftw/nftw "shall fail" in various cases where BSD's fts_open
doesn't. Since our ftw/nftw are written in terms of fts_open, add a back
door so we can hint to ourselves when we should have the POSIX semantics.

Also pull several O_CLOEXEC and don't-null-check-before-free cleanups
from upstream, and add a couple of tests.

Bug: http://b/31152735
Test: ran bionic tests and LTP "nftw01" test
Change-Id: Ib05facacc1da4c8b2ab48e9ecce88f11a5406630
diff --git a/libc/include/fts.h b/libc/include/fts.h
index 8658fab..7e63111 100644
--- a/libc/include/fts.h
+++ b/libc/include/fts.h
@@ -58,8 +58,9 @@
 #define	FTS_XDEV	0x0040		/* don't cross devices */
 #define	FTS_OPTIONMASK	0x00ff		/* valid user option mask */
 
-#define	FTS_NAMEONLY	0x1000		/* (private) child names only */
-#define	FTS_STOP	0x2000		/* (private) unrecoverable error */
+#define FTS_NAMEONLY 0x1000  /* (private) child names only */
+#define FTS_STOP 0x2000      /* (private) unrecoverable error */
+#define FTS_FOR_FTW 0x4000   /* (private) fts is being called by ftw/nftw */
 	int fts_options;		/* fts_open options, global flags */
 } FTS;