unistd et al.: Don't assume <unistd.h> exists.
authorBruno Haible <bruno@clisp.org>
Sun, 11 Sep 2011 19:44:32 +0000 (21:44 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 11 Sep 2011 19:44:32 +0000 (21:44 +0200)
* m4/chdir-long.m4 (gl_FUNC_CHDIR_LONG): Don't include <unistd.h> if it
does not exist.
* m4/environ.m4 (gl_ENVIRON): Don't include <unistd.h> if it does not
exist. But include <stdlib.h>.
* m4/fcntl-o.m4 (gl_FCNTL_O_FLAGS): If <unistd.h> does not exist,
include <io.h> and <stdlib.h> instead. Don't test symbolink links if
symlink() does not exist.
* m4/fflush.m4 (gl_FUNC_FFLUSH_STDIN): If <unistd.h> does not exist,
include <io.h> instead.
* m4/free.m4 (gl_FUNC_FREE): Assume free(NULL) works on native Windows.
* m4/getcwd.m4 (gl_FUNC_GETCWD_NULL): If <unistd.h> does not exist,
include <direct.h> instead.
* m4/getcwd-abort-bug.m4 (gl_FUNC_GETCWD_ABORT_BUG): Likewise.
* m4/getcwd-path-max.m4 (gl_FUNC_GETCWD_PATH_MAX): Likewise.
* m4/lseek.m4 (gl_FUNC_LSEEK): If <unistd.h> does not exist, include
<io.h> instead.
* m4/rename.m4 (gl_FUNC_RENAME): Assume rename() manages hard links
correctly if the system does not have hard links.
* m4/rmdir.m4 (gl_FUNC_RMDIR): If <unistd.h> does not exist, include
<direct.h> instead.
* m4/unistd_h.m4 (gl_UNISTD_H): If <unistd.h> does not exist, bypass
it when looking for function declarations.
* m4/unlink.m4 (gl_FUNC_UNLINK): If <unistd.h> does not exist, include
<direct.h> and <io.h> instead.
* doc/posix-headers/unistd.texi: More details about MSVC problem.

15 files changed:
ChangeLog
doc/posix-headers/unistd.texi
m4/chdir-long.m4
m4/environ.m4
m4/fcntl-o.m4
m4/fflush.m4
m4/free.m4
m4/getcwd-abort-bug.m4
m4/getcwd-path-max.m4
m4/getcwd.m4
m4/lseek.m4
m4/rename.m4
m4/rmdir.m4
m4/unistd_h.m4
m4/unlink.m4

index 4c88f6f..7a8168f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,34 @@
 2011-09-11  Bruno Haible  <bruno@clisp.org>
 
+       unistd et al.: Don't assume <unistd.h> exists.
+       * m4/chdir-long.m4 (gl_FUNC_CHDIR_LONG): Don't include <unistd.h> if it
+       does not exist.
+       * m4/environ.m4 (gl_ENVIRON): Don't include <unistd.h> if it does not
+       exist. But include <stdlib.h>.
+       * m4/fcntl-o.m4 (gl_FCNTL_O_FLAGS): If <unistd.h> does not exist,
+       include <io.h> and <stdlib.h> instead. Don't test symbolink links if
+       symlink() does not exist.
+       * m4/fflush.m4 (gl_FUNC_FFLUSH_STDIN): If <unistd.h> does not exist,
+       include <io.h> instead.
+       * m4/free.m4 (gl_FUNC_FREE): Assume free(NULL) works on native Windows.
+       * m4/getcwd.m4 (gl_FUNC_GETCWD_NULL): If <unistd.h> does not exist,
+       include <direct.h> instead.
+       * m4/getcwd-abort-bug.m4 (gl_FUNC_GETCWD_ABORT_BUG): Likewise.
+       * m4/getcwd-path-max.m4 (gl_FUNC_GETCWD_PATH_MAX): Likewise.
+       * m4/lseek.m4 (gl_FUNC_LSEEK): If <unistd.h> does not exist, include
+       <io.h> instead.
+       * m4/rename.m4 (gl_FUNC_RENAME): Assume rename() manages hard links
+       correctly if the system does not have hard links.
+       * m4/rmdir.m4 (gl_FUNC_RMDIR): If <unistd.h> does not exist, include
+       <direct.h> instead.
+       * m4/unistd_h.m4 (gl_UNISTD_H): If <unistd.h> does not exist, bypass
+       it when looking for function declarations.
+       * m4/unlink.m4 (gl_FUNC_UNLINK): If <unistd.h> does not exist, include
+       <direct.h> and <io.h> instead.
+       * doc/posix-headers/unistd.texi: More details about MSVC problem.
+
+2011-09-11  Bruno Haible  <bruno@clisp.org>
+
        strcase: Support for MSVC.
        * modules/strcase (Status, Notice): Remove obsoletion mark.
        * doc/posix-functions/strcasecmp.texi: Mention MSVC problem.
index 41e743a..7beb4c2 100644 (file)
@@ -8,7 +8,8 @@ Gnulib module: unistd
 Portability problems fixed by Gnulib:
 @itemize
 @item
-This header file is missing on some platforms: msvc.
+This header file is missing on some platforms:
+MSVC 9.
 @item
 The SEEK_* macros are not defined in this file on some platforms:
 mingw.
index 9a343ac..fdba191 100644 (file)
@@ -1,4 +1,4 @@
-#serial 12
+#serial 13
 
 # Use Gnulib's robust chdir function.
 # It can handle arbitrarily long directory names, which means
@@ -15,10 +15,14 @@ dnl with or without modifications, as long as this notice is preserved.
 
 AC_DEFUN([gl_FUNC_CHDIR_LONG],
 [
+  AC_CHECK_HEADERS_ONCE([unistd.h])
   AC_CACHE_CHECK([whether this system has an arbitrary file name length limit],
     gl_cv_have_arbitrary_file_name_length_limit,
     [AC_EGREP_CPP([have_arbitrary_file_name_length_limit],
-                  [#include <unistd.h>
+                  [
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#endif
 #include <limits.h>
 #if defined PATH_MAX || defined MAXPATHLEN
 have_arbitrary_file_name_length_limit
index 7457ad1..6c7982f 100644 (file)
@@ -1,4 +1,4 @@
-# environ.m4 serial 5
+# environ.m4 serial 6
 dnl Copyright (C) 2001-2004, 2006-2011 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -9,7 +9,16 @@ AC_DEFUN_ONCE([gl_ENVIRON],
   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
   dnl Persuade glibc <unistd.h> to declare environ.
   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
-  gt_CHECK_VAR_DECL([#include <unistd.h>], environ)
+
+  AC_CHECK_HEADERS_ONCE([unistd.h])
+  gt_CHECK_VAR_DECL(
+    [#if HAVE_UNISTD_H
+     #include <unistd.h>
+     #endif
+     /* mingw, BeOS, Haiku declare environ in <stdlib.h>, not in <unistd.h>.  */
+     #include <stdlib.h>
+    ],
+    [environ])
   if test $gt_cv_var_environ_declaration != yes; then
     HAVE_DECL_ENVIRON=0
   fi
index 88db07e..bee6edc 100644 (file)
@@ -1,4 +1,4 @@
-# fcntl-o.m4 serial 3
+# fcntl-o.m4 serial 4
 dnl Copyright (C) 2006, 2009-2011 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -17,12 +17,21 @@ AC_DEFUN([gl_FCNTL_O_FLAGS],
   m4_ifdef([AC_USE_SYSTEM_EXTENSIONS],
     [AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])],
     [AC_REQUIRE([AC_GNU_SOURCE])])
+
+  AC_CHECK_HEADERS_ONCE([unistd.h])
+  AC_CHECK_FUNCS_ONCE([symlink])
   AC_CACHE_CHECK([for working fcntl.h], [gl_cv_header_working_fcntl_h],
     [AC_RUN_IFELSE(
        [AC_LANG_PROGRAM(
           [[#include <sys/types.h>
            #include <sys/stat.h>
-           #include <unistd.h>
+           #if HAVE_UNISTD_H
+           # include <unistd.h>
+           #else /* on Windows with MSVC */
+           # include <io.h>
+           # include <stdlib.h>
+           # defined sleep(n) _sleep ((n) * 1000)
+           #endif
            #include <fcntl.h>
            #ifndef O_NOATIME
             #define O_NOATIME 0
@@ -38,6 +47,7 @@ AC_DEFUN([gl_FCNTL_O_FLAGS],
           ]],
           [[
             int result = !constants;
+            #if HAVE_SYMLINK
             {
               static char const sym[] = "conftest.sym";
               if (symlink (".", sym) != 0)
@@ -53,6 +63,7 @@ AC_DEFUN([gl_FCNTL_O_FLAGS],
                 }
               unlink (sym);
             }
+            #endif
             {
               static char const file[] = "confdefs.h";
               int fd = open (file, O_RDONLY | O_NOATIME);
index a91ee0b..f74a1f8 100644 (file)
@@ -1,4 +1,4 @@
-# fflush.m4 serial 12
+# fflush.m4 serial 13
 
 # Copyright (C) 2007-2011 Free Software Foundation, Inc.
 # This file is free software; the Free Software Foundation
@@ -24,13 +24,18 @@ dnl Sets gl_cv_func_fflush_stdin.
 
 AC_DEFUN([gl_FUNC_FFLUSH_STDIN],
 [
+  AC_CHECK_HEADERS_ONCE([unistd.h])
   AC_CACHE_CHECK([whether fflush works on input streams],
     [gl_cv_func_fflush_stdin],
     [echo hello world > conftest.txt
      AC_RUN_IFELSE([AC_LANG_PROGRAM(
        [[
 #include <stdio.h>
-#include <unistd.h>
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#else /* on Windows with MSVC */
+# include <io.h>
+#endif
        ]], [[FILE *f = fopen ("conftest.txt", "r");
          char buffer[10];
          int fd;
index 24bbbb1..76bca66 100644 (file)
 
 AC_DEFUN([gl_FUNC_FREE],
 [
+  AC_REQUIRE([AC_CANONICAL_HOST])
   AC_CACHE_CHECK([whether free (NULL) is known to work],
     [gl_cv_func_free],
-    [AC_COMPILE_IFELSE(
-       [AC_LANG_PROGRAM(
-          [[@%:@include <unistd.h>]],
-          [[@%:@if _POSIX_VERSION < 199009L && \
-                (defined unix || defined _unix || defined _unix_ \
-                 || defined __unix || defined __unix__)
-              @%:@error "'free (NULL)' is not known to work"
-            @%:@endif]])],
-       [gl_cv_func_free=yes],
-       [gl_cv_func_free=no])])
+    [case "$host_os" in
+       mingw*) gl_cv_func_free=yes ;;
+       *)
+         AC_COMPILE_IFELSE(
+           [AC_LANG_PROGRAM(
+              [[@%:@include <unistd.h>]],
+              [[@%:@if _POSIX_VERSION < 199009L && \
+                    (defined unix || defined _unix || defined _unix_ \
+                     || defined __unix || defined __unix__)
+                  @%:@error "'free (NULL)' is not known to work"
+                @%:@endif]])],
+           [gl_cv_func_free=yes],
+           [gl_cv_func_free=no])
+     esac
+    ])
 
   if test $gl_cv_func_free = no; then
     AC_DEFINE([free], [rpl_free],
index fd6820d..0d3f34f 100644 (file)
@@ -1,4 +1,4 @@
-# serial 4
+# serial 5
 # Determine whether getcwd aborts when the length of the working directory
 # name is unusually large.  Any length between 4k and 16k trigger the bug
 # when using glibc-2.4.90-9 or older.
@@ -14,6 +14,7 @@
 AC_DEFUN([gl_FUNC_GETCWD_ABORT_BUG],
 [
   AC_CHECK_DECLS_ONCE([getcwd])
+  AC_CHECK_HEADERS_ONCE([unistd.h])
   AC_CHECK_FUNCS([getpagesize])
   AC_CACHE_CHECK([whether getcwd aborts when 4k < cwd_length < 16k],
     gl_cv_func_getcwd_abort_bug,
@@ -26,7 +27,11 @@ AC_DEFUN([gl_FUNC_GETCWD_ABORT_BUG],
        [AC_LANG_SOURCE(
           [[
 #include <stdlib.h>
-#include <unistd.h>
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#else /* on Windows with MSVC */
+# include <direct.h>
+#endif
 #include <limits.h>
 #include <string.h>
 #include <sys/stat.h>
index 475ae96..65ee491 100644 (file)
@@ -1,4 +1,4 @@
-# serial 16
+# serial 17
 # Check for several getcwd bugs with long file names.
 # If so, arrange to compile the wrapper function.
 
@@ -17,6 +17,7 @@ AC_DEFUN([gl_FUNC_GETCWD_PATH_MAX],
 [
   AC_CHECK_DECLS_ONCE([getcwd])
   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
+  AC_CHECK_HEADERS_ONCE([unistd.h])
   AC_CACHE_CHECK([whether getcwd handles long file names properly],
     gl_cv_func_getcwd_path_max,
     [# Arrange for deletion of the temporary directory this test creates.
@@ -27,7 +28,11 @@ AC_DEFUN([gl_FUNC_GETCWD_PATH_MAX],
           [[
 #include <errno.h>
 #include <stdlib.h>
-#include <unistd.h>
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#else
+# include <direct.h>
+#endif
 #include <string.h>
 #include <limits.h>
 #include <sys/stat.h>
index d3bbe59..f828cf3 100644 (file)
@@ -6,15 +6,20 @@
 # with or without modifications, as long as this notice is preserved.
 
 # Written by Paul Eggert.
-# serial 8
+# serial 9
 
 AC_DEFUN([gl_FUNC_GETCWD_NULL],
   [
    AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
+   AC_CHECK_HEADERS_ONCE([unistd.h])
    AC_CACHE_CHECK([whether getcwd (NULL, 0) allocates memory for result],
      [gl_cv_func_getcwd_null],
      [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
-#        include <unistd.h>
+#        if HAVE_UNISTD_H
+#         include <unistd.h>
+#        else /* on Windows with MSVC */
+#         include <direct.h>
+#        endif
 #        ifndef getcwd
          char *getcwd ();
 #        endif
index b454884..73c4069 100644 (file)
@@ -1,4 +1,4 @@
-# lseek.m4 serial 7
+# lseek.m4 serial 8
 dnl Copyright (C) 2007, 2009-2011 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -8,12 +8,18 @@ AC_DEFUN([gl_FUNC_LSEEK],
 [
   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
   AC_REQUIRE([AC_PROG_CC])
+  AC_CHECK_HEADERS_ONCE([unistd.h])
   AC_CACHE_CHECK([whether lseek detects pipes], [gl_cv_func_lseek_pipe],
     [if test $cross_compiling = no; then
        AC_LINK_IFELSE([AC_LANG_PROGRAM([[
 #include <sys/types.h> /* for off_t */
 #include <stdio.h> /* for SEEK_CUR */
-#include <unistd.h>]], [[
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#else /* on Windows with MSVC */
+# include <io.h>
+#endif
+]], [[
   /* Exit with success only if stdin is seekable.  */
   return lseek (0, (off_t)0, SEEK_CUR) < 0;
 ]])],
index c938b0d..af8d584 100644 (file)
@@ -1,4 +1,4 @@
-# serial 24
+# serial 25
 
 # Copyright (C) 2001, 2003, 2005-2006, 2009-2011 Free Software Foundation, Inc.
 # This file is free software; the Free Software Foundation
@@ -110,37 +110,42 @@ AC_DEFUN([gl_FUNC_RENAME],
   dnl NetBSD 1.6 and cygwin 1.5.x mistakenly reduce hard link count
   dnl on rename("h1","h2").
   dnl This bug requires stat'ting targets prior to attempting rename.
+  AC_CHECK_FUNCS_ONCE([link])
   AC_CACHE_CHECK([whether rename manages hard links correctly],
     [gl_cv_func_rename_link_works],
-    [rm -rf conftest.f conftest.f1
-    if touch conftest.f && ln conftest.f conftest.f1 &&
-        set x `ls -i conftest.f conftest.f1` && test "$2" = "$4"; then
-      AC_RUN_IFELSE(
-        [AC_LANG_PROGRAM([[
-#          include <stdio.h>
-#          include <stdlib.h>
-#          include <unistd.h>
-           ]],
-           [[int result = 0;
-             if (rename ("conftest.f", "conftest.f1"))
-               result |= 1;
-             if (unlink ("conftest.f1"))
-               result |= 2;
-             if (rename ("conftest.f", "conftest.f"))
-               result |= 4;
-             if (rename ("conftest.f1", "conftest.f1") == 0)
-               result |= 8;
-             return result;
-           ]])],
-        [gl_cv_func_rename_link_works=yes],
-        [gl_cv_func_rename_link_works=no],
-        dnl When crosscompiling, assume rename is broken.
-        [gl_cv_func_rename_link_works="guessing no"])
-    else
-      gl_cv_func_rename_link_works="guessing no"
-    fi
-    rm -rf conftest.f conftest.f1
-  ])
+    [if test $ac_cv_func_link = yes; then
+       rm -rf conftest.f conftest.f1
+       if touch conftest.f && ln conftest.f conftest.f1 &&
+           set x `ls -i conftest.f conftest.f1` && test "$2" = "$4"; then
+         AC_RUN_IFELSE(
+           [AC_LANG_PROGRAM([[
+#             include <stdio.h>
+#             include <stdlib.h>
+#             include <unistd.h>
+              ]],
+              [[int result = 0;
+                if (rename ("conftest.f", "conftest.f1"))
+                  result |= 1;
+                if (unlink ("conftest.f1"))
+                  result |= 2;
+                if (rename ("conftest.f", "conftest.f"))
+                  result |= 4;
+                if (rename ("conftest.f1", "conftest.f1") == 0)
+                  result |= 8;
+                return result;
+              ]])],
+           [gl_cv_func_rename_link_works=yes],
+           [gl_cv_func_rename_link_works=no],
+           dnl When crosscompiling, assume rename is broken.
+           [gl_cv_func_rename_link_works="guessing no"])
+       else
+         gl_cv_func_rename_link_works="guessing no"
+       fi
+       rm -rf conftest.f conftest.f1
+     else
+       gl_cv_func_rename_link_works=yes
+     fi
+    ])
   if test "x$gl_cv_func_rename_link_works" != xyes; then
     REPLACE_RENAME=1
     AC_DEFINE([RENAME_HARD_LINK_BUG], [1],
index 5284c7e..0b1a65c 100644 (file)
@@ -1,4 +1,4 @@
-# rmdir.m4 serial 11
+# rmdir.m4 serial 12
 dnl Copyright (C) 2002, 2005, 2009-2011 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -8,6 +8,7 @@ AC_DEFUN([gl_FUNC_RMDIR],
 [
   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
   dnl Detect cygwin 1.5.x bug.
+  AC_CHECK_HEADERS_ONCE([unistd.h])
   AC_CACHE_CHECK([whether rmdir works], [gl_cv_func_rmdir_works],
     [mkdir conftest.dir
      touch conftest.file
@@ -15,7 +16,11 @@ AC_DEFUN([gl_FUNC_RMDIR],
        [AC_LANG_PROGRAM(
          [[#include <stdio.h>
            #include <errno.h>
-           #include <unistd.h>
+           #if HAVE_UNISTD_H
+           # include <unistd.h>
+           #else /* on Windows with MSVC */
+           # include <direct.h>
+           #endif
 ]], [[int result = 0;
       if (!rmdir ("conftest.file/"))
         result |= 1;
index 6483b81..33897ae 100644 (file)
@@ -1,4 +1,4 @@
-# unistd_h.m4 serial 57
+# unistd_h.m4 serial 58
 dnl Copyright (C) 2006-2011 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -26,7 +26,10 @@ AC_DEFUN([gl_UNISTD_H],
 
   dnl Check for declarations of anything we want to poison if the
   dnl corresponding gnulib module is not in use.
-  gl_WARN_ON_USE_PREPARE([[#include <unistd.h>
+  gl_WARN_ON_USE_PREPARE([[
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#endif
 /* Some systems declare various items in the wrong headers.  */
 #if !(defined __GLIBC__ && !defined __UCLIBC__)
 # include <fcntl.h>
index 49cd3bb..d6865a9 100644 (file)
@@ -1,4 +1,4 @@
-# unlink.m4 serial 8
+# unlink.m4 serial 9
 dnl Copyright (C) 2009-2011 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -8,6 +8,7 @@ AC_DEFUN([gl_FUNC_UNLINK],
 [
   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
   AC_REQUIRE([AC_CANONICAL_HOST])
+  AC_CHECK_HEADERS_ONCE([unistd.h])
   dnl Detect FreeBSD 7.2, AIX 7.1, Solaris 9 bug.
   AC_CACHE_CHECK([whether unlink honors trailing slashes],
     [gl_cv_func_unlink_honors_slashes],
@@ -18,7 +19,11 @@ AC_DEFUN([gl_FUNC_UNLINK],
      fi
      AC_RUN_IFELSE(
        [AC_LANG_PROGRAM(
-         [[#include <unistd.h>
+         [[#if HAVE_UNISTD_H
+           # include <unistd.h>
+           #else /* on Windows with MSVC */
+           # include <io.h>
+           #endif
            #include <errno.h>
          ]],
          [[int result = 0;
@@ -70,7 +75,12 @@ AC_DEFUN([gl_FUNC_UNLINK],
            AC_RUN_IFELSE(
              [AC_LANG_SOURCE([[
                 #include <stdlib.h>
-                #include <unistd.h>
+                #if HAVE_UNISTD_H
+                # include <unistd.h>
+                #else /* on Windows with MSVC */
+                # include <direct.h>
+                # include <io.h>
+                #endif
                 int main ()
                 {
                   int result = 0;