diff --git a/include/compat/getopt.h b/include/compat/getopt.h
index 930e002a139142c8600538cc6f7fbcd0a4a4bd68..ad597691ef4860e511b619d585415ee7208412e8 100644
--- a/include/compat/getopt.h
+++ b/include/compat/getopt.h
@@ -13,7 +13,9 @@
 #define __GETOPT_H__
 
 /* All the headers include this file. */
+#ifdef _WIN32
 #include <crtdefs.h>
+#endif
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/tools/compat/getopt.c b/tools/compat/getopt.c
index ac1fda426ecc59c017d4a56da7cb4ceb728e60b0..ab375bdb17e2add76ef884b1877ad93731dee3c6 100644
--- a/tools/compat/getopt.c
+++ b/tools/compat/getopt.c
@@ -55,7 +55,11 @@
 #include <getopt.h>
 #include <stdarg.h>
 #include <stdio.h>
+#ifdef _WIN32
 #include <windows.h>
+#else
+#include <err.h>
+#endif
 
 #define	REPLACE_GETOPT		/* use this getopt as the system getopt(3) */
 
@@ -80,12 +84,6 @@ char    *optarg;		/* argument associated with option */
 #define	BADARG		((*options == ':') ? (int)':' : (int)'?')
 #define	INORDER 	(int)1
 
-#ifndef __CYGWIN__
-#define __progname __argv[0]
-#else
-extern char __declspec(dllimport) *__progname;
-#endif
-
 #ifdef __CYGWIN__
 static char EMSG[] = "";
 #else
@@ -113,6 +111,13 @@ static const char noarg[] = "option doesn't take an argument -- %.*s";
 static const char illoptchar[] = "unknown option -- %c";
 static const char illoptstring[] = "unknown option -- %s";
 
+#ifdef _WIN32
+#ifndef __CYGWIN__
+#define __progname __argv[0]
+#else
+extern char __declspec(dllimport) *__progname;
+#endif
+
 static void
 _vwarnx(const char *fmt,va_list ap)
 {
@@ -130,6 +135,7 @@ warnx(const char *fmt,...)
   _vwarnx(fmt,ap);
   va_end(ap);
 }
+#endif
 
 /*
  * Compute the greatest common divisor of a and b.