From 930d1c530e71c97695e13827232c36f7f5558911 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Wed, 30 Dec 2020 12:04:19 +0100
Subject: [PATCH] Clean up POSIX namespace in <dirent.h>, <iconv.h>, and
 <sys/stat.h>.

---
 include/iconv.h  | 14 +++++++++-
 sys/sys/dirent.h | 21 ++++++++++-----
 sys/sys/stat.h   | 69 ++++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 85 insertions(+), 19 deletions(-)

diff --git a/include/iconv.h b/include/iconv.h
index c258a46eb6..58658d0a5b 100644
--- a/include/iconv.h
+++ b/include/iconv.h
@@ -33,10 +33,18 @@
 #define _ICONV_H_
 
 #include <sys/cdefs.h>
-#include <sys/types.h>
+#include <machine/stdint.h>
 
+#if __BSD_VISIBLE
 #include <wchar.h>
+#endif
+
+#ifndef _SIZE_T_DECLARED
+typedef	__size_t	size_t;
+#define	_SIZE_T_DECLARED
+#endif
 
+#if __BSD_VISIBLE
 #ifdef __cplusplus
 typedef	bool	__iconv_bool;
 #elif __STDC_VERSION__ >= 199901L
@@ -44,6 +52,7 @@ typedef	_Bool	__iconv_bool;
 #else
 typedef	int	__iconv_bool;
 #endif
+#endif
 
 struct __tag_iconv_t;
 typedef	struct __tag_iconv_t	*iconv_t;
@@ -56,6 +65,7 @@ size_t	iconv(iconv_t, char ** __restrict,
 	      size_t * __restrict);
 int	iconv_close(iconv_t);
 #endif
+#if __BSD_VISIBLE
 /*
  * non-portable interfaces for iconv
  */
@@ -131,6 +141,8 @@ void		 iconvlist(int (*do_one) (unsigned int, const char * const *,
 		    void *), void *);
 const char	*iconv_canonicalize(const char *);
 int		 iconvctl(iconv_t, int, void *);
+#endif /* __BSD_VISIBLE */
+
 __END_DECLS
 
 #endif /* !_ICONV_H_ */
diff --git a/sys/sys/dirent.h b/sys/sys/dirent.h
index 4bd1ef7368..749f8d91e0 100644
--- a/sys/sys/dirent.h
+++ b/sys/sys/dirent.h
@@ -54,7 +54,12 @@
  */
 
 #include <sys/cdefs.h>
-#include <sys/types.h>
+#include <machine/stdint.h>
+
+#ifndef _INO_T_DECLARED
+typedef	__uint64_t	ino_t;		/* inode number */
+#define	_INO_T_DECLARED
+#endif
 
 struct dirent {
 #if defined(_KERNEL) || !__BSD_VISIBLE
@@ -62,21 +67,22 @@ struct dirent {
 #else
 	ino_t		d_fileno;	/* file number of entry */
 #endif
-	uint16_t	d_namlen;	/* strlen(d_name) */
-	uint8_t		d_type;		/* file type, see blow */
-	uint8_t		d_unused1;	/* padding, reserved */
-	uint32_t	d_unused2;	/* reserved */
+	__uint16_t	d_namlen;	/* strlen(d_name) */
+	__uint8_t	d_type;		/* file type, see blow */
+	__uint8_t	d_unused1;	/* padding, reserved */
+	__uint32_t	d_unused2;	/* reserved */
 	char		d_name[255 + 1];
 					/* name, NUL-terminated */
 };
 
+#if __BSD_VISIBLE
 /*
  * Linux compatibility, but its a good idea anyhow
  */
 #define _DIRENT_HAVE_D_NAMLEN
 #define _DIRENT_HAVE_D_TYPE
 
-#if !defined(_KERNEL) && __BSD_VISIBLE
+#if !defined(_KERNEL)
 #define	d_ino		d_fileno
 #endif
 
@@ -109,6 +115,7 @@ struct dirent {
 	((__offsetof(struct dirent, d_name) + (namelen) + 1 + 7) & ~7)
 #define	_DIRENT_DIRSIZ(dp)	_DIRENT_RECLEN((dp)->d_namlen)
 #define	_DIRENT_NEXT(dp) \
-	((struct dirent *)((uint8_t *)(dp) + _DIRENT_DIRSIZ(dp)))
+	((struct dirent *)((__uint8_t *)(dp) + _DIRENT_DIRSIZ(dp)))
+#endif /* __BSD_VISIBLE */
 
 #endif /* !_SYS_DIRENT_H_ */
diff --git a/sys/sys/stat.h b/sys/sys/stat.h
index 3df4bb480e..a464efa63e 100644
--- a/sys/sys/stat.h
+++ b/sys/sys/stat.h
@@ -39,37 +39,84 @@
 #define	_SYS_STAT_H_
 
 #include <sys/cdefs.h>
-#include <sys/types.h>
+#include <machine/stdint.h>
 
 #if !defined(_KERNEL) && __BSD_VISIBLE
-/*
- * XXX We get miscellaneous namespace pollution with this.
- */
 #include <sys/time.h>
 #else
 #include <sys/_timespec.h>
 #endif
 
+#ifndef _BLKCNT_T_DECLARED
+typedef	__int64_t	blkcnt_t;	/* file block count */
+#define	_BLKCNT_T_DECLARED
+#endif
+
+#ifndef _BLKSIZE_T_DECLARED
+typedef	__int64_t	blksize_t;	/* block size */
+#define	_BLKSIZE_T_DECLARED
+#endif
+
+#ifndef _DEV_T_DECLARED
+typedef	__uint32_t	dev_t;		/* device number */
+#define	_DEV_T_DECLARED
+#endif
+
+#ifndef _GID_T_DECLARED
+typedef	__uint32_t	gid_t;		/* group id */
+#define	_GID_T_DECLARED
+#endif
+
+#ifndef _INO_T_DECLARED
+typedef	__uint64_t	ino_t;		/* inode number */
+#define	_INO_T_DECLARED
+#endif
+
+#ifndef _MODE_T_DECLARED
+typedef	__uint16_t	mode_t;		/* permissions */
+#define	_MODE_T_DECLARED
+#endif
+
+#ifndef _NLINK_T_DECLARED
+typedef	__uint32_t	nlink_t;	/* link count */
+#define	_NLINK_T_DECLARED
+#endif
+
+#ifndef _OFF_T_DECLARED
+typedef	__off_t		off_t;		/* file offset */
+#define	_OFF_T_DECLARED
+#endif
+
+#ifndef _TIME_T_DECLARED
+typedef	__time_t	time_t;
+#define	_TIME_T_DECLARED
+#endif
+
+#ifndef _UID_T_DECLARED
+typedef	__uint32_t	uid_t;		/* user id */
+#define	_UID_T_DECLARED
+#endif
+
 struct stat {
 	ino_t		st_ino;		/* inode's number */
 	nlink_t		st_nlink;	/* number of hard links */
 	dev_t		st_dev;		/* inode's device */
 	mode_t		st_mode;	/* inode protection mode */
-	uint16_t	st_padding1;
+	__uint16_t	st_padding1;
 	uid_t		st_uid;		/* user ID of the file's owner */
 	gid_t		st_gid;		/* group ID of the file's group */
-	dev_t		st_rdev;		/* device type */
+	dev_t		st_rdev;	/* device type */
 	struct timespec	st_atim;	/* time of last access */
 	struct timespec	st_mtim;	/* time of last data modification */
 	struct timespec	st_ctim;	/* time of last file status change */
 	off_t		st_size;	/* file size, in bytes */
 	blkcnt_t	st_blocks;	/* blocks allocated for file */
-	u_int32_t	__old_st_blksize; /* old ABI compatibility only */
-	u_int32_t	st_flags;	/* user defined flags for file */
-	u_int32_t	st_gen;		/* file generation number */
-	int32_t		st_lspare;
+	__uint32_t	__old_st_blksize; /* old ABI compatibility only */
+	__uint32_t	st_flags;	/* user defined flags for file */
+	__uint32_t	st_gen;		/* file generation number */
+	__int32_t	st_lspare;
 	blksize_t	st_blksize;	/* optimal blocksize for I/O */
-	int64_t		st_qspare2;
+	__int64_t	st_qspare2;
 };
 
 #define	_ST_FLAGS_PRESENT_
-- 
2.28.0

