changeset 8199:51d32a83a7df

Move more declarations into <unistd.h>.
author Bruno Haible <bruno@clisp.org>
date Mon, 19 Feb 2007 02:23:32 +0000
parents d84ab7c183f4
children 82bf117aac81
files ChangeLog lib/chown.c lib/dirent_.h lib/dup2.c lib/fchdir.c lib/ftruncate.c lib/getcwd.c lib/getlogin_r.c lib/readlink.c lib/unistd_.h lib/xgetcwd.c m4/chown.m4 m4/dup2.m4 m4/fchdir.m4 m4/ftruncate.m4 m4/getcwd.m4 m4/getlogin_r.m4 m4/readlink.m4 m4/unistd_h.m4 modules/chown modules/dup2 modules/fchdir modules/ftruncate modules/getcwd modules/getlogin_r modules/readlink modules/unistd
diffstat 27 files changed, 355 insertions(+), 83 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,77 @@
+2007-02-18  Bruno Haible  <bruno@clisp.org>
+
+	* lib/readlink.c: Include <unistd.h>.
+	* m4/readlink.m4 (gl_FUNC_READLINK): Require gl_UNISTD_H_DEFAULTS. Set
+	HAVE_READLINK.
+	* modules/readlink (Depends-on): Add unistd.
+	(configure.ac): Invoke gl_UNISTD_MODULE_INDICATOR.
+	(Include): Add <unistd.h>.
+
+	* lib/getlogin_r.h: Remove file.
+	* lib/getlogin_r.c: Include <unistd.h> instead of getlogin_r.h.
+	* m4/getlogin_r.m4 (gl_GETLOGIN_R_SUBSTITUTE): Remove macro.
+	(gl_GETLOGIN_R): Inline it here. Require gl_UNISTD_H_DEFAULTS. Set
+	HAVE_DECL_GETLOGIN_R.
+	* modules/getlogin_r (Files): Remove lib/getlogin_r.h.
+	(configure.ac): Invoke gl_UNISTD_MODULE_INDICATOR.
+	(Include): Use <unistd.h> instead of getlogin_r.h.
+
+	* lib/getcwd.h: Remove file.
+	* lib/getcwd.c: Include <unistd.h> instead of getcwd.h.
+	* lib/xgetcwd.c: Likewise.
+	* m4/getcwd.m4 (gl_FUNC_GETCWD): Require gl_UNISTD_H_DEFAULTS. Set
+	REPLACE_GETCWD. Don't define __GETCWD_PREFIX.
+	* modules/getcwd (Files): Remove lib/getcwd.h.
+	(Depends-on): Add unistd.
+	(configure.ac): Invoke gl_UNISTD_MODULE_INDICATOR.
+	(Include): Use <unistd.h> instad of getcwd.h.
+
+	* lib/ftruncate.c: Include <unistd.h> first.
+	* m4/ftruncate.m4 (gl_FUNC_FTRUNCATE): Require gl_UNISTD_H_DEFAULTS.
+	Set HAVE_FTRUNCATE.
+	* modules/ftruncate (Depends-on): Add unistd.
+	(configure.ac): Invoke gl_UNISTD_MODULE_INDICATOR.
+
+	* lib/fchdir.c: Include <unistd.h> first.
+	* lib/dirent_.h: Test REPLACE_FCHDIR, not FCHDIR_REPLACEMENT.
+	* m4/fchdir.m4 (gl_FUNC_FCHDIR): Require gl_UNISTD_H_DEFAULTS instead
+	of gl_HEADER_UNISTD_DEFAULTS. Set REPLACE_FCHDIR. Don't set UNISTD_H.
+	* modules/fchdir (configure.ac): Invoke gl_UNISTD_MODULE_INDICATOR.
+	(Makefile.am): Substitute also REPLACE_FCHDIR into dirent.h.
+
+	* lib/dup2.c: Include <unistd.h> first.
+	* m4/dup2.m4 (gl_FUNC_DUP2): Require gl_UNISTD_H_DEFAULTS. Set
+	HAVE_DUP2.
+	* modules/dup2 (Depends-on): Add unistd.
+	(configure.ac): Invoke gl_UNISTD_MODULE_INDICATOR.
+
+	* lib/chown.c: Include <unistd.h> first. Undefine chown later.
+	* m4/chown.m4 (gl_FUNC_CHOWN): Require gl_UNISTD_H_DEFAULTS. Set
+	REPLACE_CHOWN. Don't define chown as a macro here.
+	* modules/chown (Depends-on): Add unistd.
+	(configure.ac): Invoke gl_UNISTD_MODULE_INDICATOR.
+
+	* lib/unistd_.h: Test HAVE_UNISTD_H determined at configure time.
+	Add definition for GL_LINK_WARNING.
+	(chown, dup2): New declarations.
+	(fchdir): Test REPLACE_FCHDIR, not FCHDIR_REPLACEMENT. Provide optional
+	link warning.
+	(ftruncate): New declaration.
+	(getcwd): New declaration, taken from old getcwd.h.
+	(getlogin_r): New declaration, taken from old getlogin_r.h.
+	(readlink): New declaration.
+	* m4/unistd_h.m4 (gl_UNISTD_H): Renamed from gl_HEADER_UNISTD. Don't
+	set UNISTD_H. Inline gl_PREREQ_UNISTD. Set HAVE_UNISTD_H.
+	(gl_PREREQ_UNISTD): Remove macro.
+	(gl_UNISTD_MODULE_INDICATOR): New macro.
+	(gl_UNISTD_H_DEFAULTS): Renamed from gl_HEADER_UNISTD_DEFAULTS. Set
+	many new variables. Don't set UNISTD_H.
+	* modules/unistd (Description): Change.
+	(Depends-on): Add link-warning.
+	(configure.ac): Update.
+	(Makefile.am): Create unistd.h always. Substitute many new variables
+	into it.
+
 2007-02-18  Bruno Haible  <bruno@clisp.org>
 
 	* lib/stdlib_.h (getsubopt): New declaration, copied from getsubopt.h.
@@ -61,7 +135,6 @@
 	* lib/savewd.c: Likewise.
 	* lib/xsetenv.c: Likewise.
 
-
 	* modules/stdlib: New file.
 	* lib/stdlib_.h: New file, incorporating exit.h, mkdtemp.h, mkstemp.h
 	and extra comments about mkstemp().
--- a/lib/chown.c
+++ b/lib/chown.c
@@ -1,7 +1,7 @@
 /* provide consistent interface to chown for systems that don't interpret
    an ID of -1 as meaning `don't change the corresponding ID'.
 
-   Copyright (C) 1997, 2004, 2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 1997, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -21,18 +21,18 @@
 
 #include <config.h>
 
-/* Disable the definition of chown to rpl_chown (from config.h) in this
-   file.  Otherwise, we'd get conflicting prototypes for rpl_chown on
-   most systems.  */
-#undef chown
+/* Specification.  */
+#include <unistd.h>
 
 #include <stdbool.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
 
+/* Below we refer to the system's chown().  */
+#undef chown
+
 /* The results of open() in this file are not used with fchdir,
    therefore save some unnecessary work in fchdir.c.  */
 #undef open
--- a/lib/dirent_.h
+++ b/lib/dirent_.h
@@ -1,5 +1,5 @@
 /* Wrapper around <dirent.h>.
-   Copyright (C) 2006 Free Software Foundation, Inc.
+   Copyright (C) 2006-2007 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -27,7 +27,7 @@
 extern "C" {
 #endif
 
-#ifdef FCHDIR_REPLACEMENT
+#if @REPLACE_FCHDIR@
 # define opendir rpl_opendir
 extern DIR * opendir (const char *);
 # define closedir rpl_closedir
--- a/lib/dup2.c
+++ b/lib/dup2.c
@@ -1,6 +1,6 @@
 /* Duplicate an open file descriptor to a specified file descriptor.
 
-   Copyright (C) 1999, 2004, 2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -20,9 +20,11 @@
 
 #include <config.h>
 
+/* Specification.  */
+#include <unistd.h>
+
 #include <errno.h>
 #include <fcntl.h>
-#include <unistd.h>
 
 #ifndef F_DUPFD
 static int
--- a/lib/fchdir.c
+++ b/lib/fchdir.c
@@ -17,12 +17,14 @@
 
 #include <config.h>
 
+/* Specification.  */
+#include <unistd.h>
+
 #include <errno.h>
 #include <fcntl.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <dirent.h>
--- a/lib/ftruncate.c
+++ b/lib/ftruncate.c
@@ -3,6 +3,9 @@
 
 #include <config.h>
 
+/* Specification.  */
+#include <unistd.h>
+
 #include <sys/types.h>
 #include <fcntl.h>
 
@@ -21,7 +24,6 @@
 
 #  include <sys/stat.h>
 #  include <errno.h>
-#  include <unistd.h>
 
 int
 ftruncate (int fd, off_t length)
--- a/lib/getcwd.c
+++ b/lib/getcwd.c
@@ -18,7 +18,7 @@
 
 #if !_LIBC
 # include <config.h>
-# include "getcwd.h"
+# include <unistd.h>
 #endif
 
 #include <errno.h>
--- a/lib/getlogin_r.c
+++ b/lib/getlogin_r.c
@@ -1,6 +1,6 @@
 /* Provide a working getlogin_r for systems which lack it.
 
-   Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -20,11 +20,11 @@
 
 #include <config.h>
 
-#include "getlogin_r.h"
+/* Specification.  */
+#include <unistd.h>
 
 #include <errno.h>
 #include <string.h>
-#include <unistd.h>
 
 #if !HAVE_DECL_GETLOGIN
 char *getlogin (void);
--- a/lib/readlink.c
+++ b/lib/readlink.c
@@ -1,5 +1,5 @@
 /* Stub for readlink().
-   Copyright (C) 2003-2006 Free Software Foundation, Inc.
+   Copyright (C) 2003-2007 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -17,6 +17,9 @@
 
 #include <config.h>
 
+/* Specification.  */
+#include <unistd.h>
+
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/stat.h>
--- a/lib/unistd_.h
+++ b/lib/unistd_.h
@@ -1,5 +1,5 @@
 /* Substitute for and wrapper around <unistd.h>.
-   Copyright (C) 2006 Free Software Foundation, Inc.
+   Copyright (C) 2004-2007 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -18,32 +18,172 @@
 #ifndef _GL_UNISTD_H
 #define _GL_UNISTD_H
 
-#if HAVE_UNISTD_H
+#if @HAVE_UNISTD_H@
 # include @ABSOLUTE_UNISTD_H@
 #endif
 
 
+/* The definition of GL_LINK_WARNING is copied here.  */
+
+
 /* Declare overridden functions.  */
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-#ifdef FCHDIR_REPLACEMENT
+
+#if @GNULIB_CHOWN@
+# if @REPLACE_CHOWN@
+/* Change the owner of FILE to UID (if UID is not -1) and the group of FILE
+   to GID (if GID is not -1).
+   Return 0 if successful, otherwise -1 and errno set.
+   See the POSIX:2001 specification
+   <http://www.opengroup.org/susv3xsh/chown.html>.  */
+#  define chown rpl_chown
+extern int chown (const char *file, uid_t uid, gid_t gid);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef chown
+# define chown(f,u,g) \
+    (GL_LINK_WARNING ("chown fails to follow symlinks on some systems and " \
+                      "doesn't treat a uid or gid of -1 on some systems - " \
+                      "use gnulib module chown for portability"), \
+     chown (f, u, g))
+#endif
+
+
+#if @GNULIB_DUP2@
+# if !@HAVE_DUP2@
+/* Copy the file descriptor OLDFD into file descriptor NEWFD.  Do nothing if
+   NEWFD = OLDFD, otherwise close NEWFD first if it is open.
+   Return 0 if successful, otherwise -1 and errno set.
+   See the POSIX:2001 specification
+   <http://www.opengroup.org/susv3xsh/dup2.html>.  */
+extern int dup2 (int oldfd, int newfd);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef dup2
+# define dup2(o,n) \
+    (GL_LINK_WARNING ("dup2 is unportable - " \
+                      "use gnulib module dup2 for portability"), \
+     dup2 (o, n))
+#endif
+
+
+#if @GNULIB_FCHDIR@
+# if @REPLACE_FCHDIR@
 
 /* Change the process' current working directory to the directory on which
-   the given file descriptor is open.  */
+   the given file descriptor is open.
+   Return 0 if successful, otherwise -1 and errno set.
+   See the POSIX:2001 specification
+   <http://www.opengroup.org/susv3xsh/fchdir.html>.  */
 extern int fchdir (int /*fd*/);
 
-# define close rpl_close
+#  define close rpl_close
 extern int close (int);
-# define dup rpl_dup
+#  define dup rpl_dup
 extern int dup (int);
-# define dup2 rpl_dup2
+#  define dup2 rpl_dup2
 extern int dup2 (int, int);
 
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef fchdir
+# define fchdir(f) \
+    (GL_LINK_WARNING ("fchdir is unportable - " \
+                      "use gnulib module fchdir for portability"), \
+     fchdir (f))
 #endif
 
+
+#if @GNULIB_FTRUNCATE@
+# if !@HAVE_FTRUNCATE@
+/* Change the size of the file to which FD is opened to become equal to LENGTH.
+   Return 0 if successful, otherwise -1 and errno set.
+   See the POSIX:2001 specification
+   <http://www.opengroup.org/susv3xsh/ftruncate.html>.  */
+extern int ftruncate (int fd, off_t length);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef ftruncate
+# define ftruncate(f,l) \
+    (GL_LINK_WARNING ("ftruncate is unportable - " \
+                      "use gnulib module ftruncate for portability"), \
+     ftruncate (f, l))
+#endif
+
+
+#if @GNULIB_GETCWD@
+/* Include the headers that might declare getcwd so that they will not
+   cause confusion if included after this file.  */
+# include <stdlib.h>
+# if @REPLACE_GETCWD@
+/* Get the name of the current working directory, and put it in SIZE bytes
+   of BUF.
+   Return BUF if successful, or NULL if the directory couldn't be determined
+   or SIZE was too small.
+   See the POSIX:2001 specification
+   <http://www.opengroup.org/susv3xsh/getcwd.html>.
+   Additionally, the gnulib module 'getcwd' guarantees the following GNU
+   extension: If BUF is NULL, an array is allocated with 'malloc'; the array
+   is SIZE bytes long, unless SIZE == 0, in which case it is as big as
+   necessary.  */
+#  define getcwd rpl_getcwd
+extern char * getcwd (char *buf, size_t size);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef getcwd
+# define getcwd(b,s) \
+    (GL_LINK_WARNING ("getcwd is unportable - " \
+                      "use gnulib module getcwd for portability"), \
+     getcwd (b, s))
+#endif
+
+
+#if @GNULIB_GETLOGIN_R@
+/* Copies the user's login name to NAME.
+   The array pointed to by NAME has room for SIZE bytes.
+
+   Returns 0 if successful.  Upon error, an error number is returned, or -1 in
+   the case that the login name cannot be found but no specific error is
+   provided (this case is hopefully rare but is left open by the POSIX spec).
+
+   See <http://www.opengroup.org/susv3xsh/getlogin.html>.
+ */
+# if !@HAVE_DECL_GETLOGIN_R@
+#  include <stddef.h>
+extern int getlogin_r (char *name, size_t size);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef getlogin_r
+# define getlogin_r(n,s) \
+    (GL_LINK_WARNING ("getlogin_r is unportable - " \
+                      "use gnulib module getlogin_r for portability"), \
+     getlogin_r (n, s))
+#endif
+
+
+#if @GNULIB_READLINK@
+/* Read the contents of the symbolic link FILE and place the first BUFSIZE
+   bytes of it into BUF.  Return the number of bytes placed into BUF if
+   successful, otherwise -1 and errno set.
+   See the POSIX:2001 specification
+   <http://www.opengroup.org/susv3xsh/readlink.html>.  */
+# if !@HAVE_READLINK@
+#  include <stddef.h>
+extern int readlink (const char *file, char *buf, size_t bufsize);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef readlink
+# define readlink(f,b,s) \
+    (GL_LINK_WARNING ("readlink is unportable - " \
+                      "use gnulib module readlink for portability"), \
+     readlink (f, b, s))
+#endif
+
+
 #ifdef __cplusplus
 }
 #endif
--- a/lib/xgetcwd.c
+++ b/lib/xgetcwd.c
@@ -1,6 +1,6 @@
 /* xgetcwd.c -- return current directory with unlimited length
 
-   Copyright (C) 2001, 2003, 2004, 2006 Free Software Foundation, Inc.
+   Copyright (C) 2001, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -23,8 +23,8 @@
 #include "xgetcwd.h"
 
 #include <errno.h>
+#include <unistd.h>
 
-#include "getcwd.h"
 #include "xalloc.h"
 
 /* Return the current directory, newly allocated.
--- a/m4/chown.m4
+++ b/m4/chown.m4
@@ -1,7 +1,7 @@
-#serial 16
+#serial 17
 # Determine whether we need the chown wrapper.
 
-dnl Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005 Free
+dnl Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2007 Free
 dnl Software Foundation, Inc.
 
 dnl This file is free software; the Free Software Foundation
@@ -16,6 +16,7 @@
 
 AC_DEFUN([gl_FUNC_CHOWN],
 [
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
   AC_REQUIRE([AC_TYPE_UID_T])
   AC_REQUIRE([AC_FUNC_CHOWN])
   AC_REQUIRE([gl_FUNC_CHOWN_FOLLOWS_SYMLINK])
@@ -29,9 +30,8 @@
   if test $ac_cv_func_chown_works$gl_cv_func_chown_follows_symlink = yesyes; then
     : # no wrapper needed
   else
+    REPLACE_CHOWN=1
     AC_LIBOBJ(chown)
-    AC_DEFINE(chown, rpl_chown,
-      [Define to rpl_chown if the replacement function should be used.])
     gl_PREREQ_CHOWN
   fi
 ])
--- a/m4/dup2.m4
+++ b/m4/dup2.m4
@@ -1,10 +1,15 @@
-#serial 4
-dnl Copyright (C) 2002, 2005 Free Software Foundation, Inc.
+#serial 5
+dnl Copyright (C) 2002, 2005, 2007 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
 
 AC_DEFUN([gl_FUNC_DUP2],
 [
-  AC_REPLACE_FUNCS(dup2)
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+  AC_CHECK_FUNCS_ONCE([dup2])
+  if test $ac_cv_func_dup2 = no; then
+    HAVE_DUP2=0
+    AC_LIBOBJ([dup2])
+  fi
 ])
--- a/m4/fchdir.m4
+++ b/m4/fchdir.m4
@@ -1,4 +1,4 @@
-# fchdir.m4 serial 2
+# fchdir.m4 serial 3
 dnl Copyright (C) 2006-2007 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -6,9 +6,10 @@
 
 AC_DEFUN([gl_FUNC_FCHDIR],
 [
-  AC_REQUIRE([gl_HEADER_UNISTD_DEFAULTS])
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
   AC_CHECK_FUNCS_ONCE([fchdir])
   if test $ac_cv_func_fchdir = no; then
+    REPLACE_FCHDIR=1
     AC_LIBOBJ([fchdir])
     gl_PREREQ_FCHDIR
     AC_DEFINE([FCHDIR_REPLACEMENT], 1,
@@ -16,7 +17,6 @@
     gl_ABSOLUTE_HEADER([dirent.h])
     ABSOLUTE_DIRENT_H=\"$gl_cv_absolute_dirent_h\"
     DIRENT_H='dirent.h'
-    UNISTD_H='unistd.h'
   else
     DIRENT_H=
   fi
--- a/m4/ftruncate.m4
+++ b/m4/ftruncate.m4
@@ -1,8 +1,8 @@
-#serial 10
+#serial 11
 
 # See if we need to emulate a missing ftruncate function using fcntl or chsize.
 
-# Copyright (C) 2000, 2001, 2003-2006 Free Software Foundation, Inc.
+# Copyright (C) 2000, 2001, 2003-2007 Free Software Foundation, Inc.
 # This file is free software; the Free Software Foundation
 # gives unlimited permission to copy and/or distribute it,
 # with or without modifications, as long as this notice is preserved.
@@ -12,8 +12,11 @@
 
 AC_DEFUN([gl_FUNC_FTRUNCATE],
 [
-  AC_REPLACE_FUNCS(ftruncate)
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+  AC_CHECK_FUNCS_ONCE([ftruncate])
   if test $ac_cv_func_ftruncate = no; then
+    HAVE_FTRUNCATE=0
+    AC_LIBOBJ([ftruncate])
     gl_PREREQ_FTRUNCATE
     # If someone lacks ftruncate, make configure fail, and request
     # a bug report to inform us about it.
--- a/m4/getcwd.m4
+++ b/m4/getcwd.m4
@@ -1,6 +1,6 @@
 # getcwd.m4 - check for working getcwd that is compatible with glibc
 
-# Copyright (C) 2001, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
+# Copyright (C) 2001, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
 # This file is free software; the Free Software Foundation
 # gives unlimited permission to copy and/or distribute it,
 # with or without modifications, as long as this notice is preserved.
@@ -36,6 +36,7 @@
 
 AC_DEFUN([gl_FUNC_GETCWD],
 [
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
   AC_REQUIRE([gl_FUNC_GETCWD_NULL])
 
   gl_abort_bug=no
@@ -48,9 +49,8 @@
   case $gl_cv_func_getcwd_null,$gl_cv_func_getcwd_path_max,$gl_abort_bug in
   yes,yes,no) ;;
   *)
+    REPLACE_GETCWD=1
     AC_LIBOBJ([getcwd])
-    AC_DEFINE([__GETCWD_PREFIX], [[rpl_]],
-      [Define to rpl_ if the getcwd replacement function should be used.])
     gl_PREREQ_GETCWD;;
   esac
 ])
--- a/m4/getlogin_r.m4
+++ b/m4/getlogin_r.m4
@@ -1,6 +1,6 @@
-#serial 3
+#serial 4
 
-# Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+# Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
 #
 # This file is free software; the Free Software Foundation
 # gives unlimited permission to copy and/or distribute it,
@@ -11,23 +11,23 @@
 dnl Provide getlogin_r when the system lacks it.
 dnl
 
-AC_DEFUN([gl_GETLOGIN_R_SUBSTITUTE],
-[
-  gl_PREREQ_GETLOGIN_R
-  AC_LIBOBJ([getlogin_r])
-])
-
 AC_DEFUN([gl_GETLOGIN_R],
 [
-  AC_REPLACE_FUNCS([getlogin_r])
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+  AC_CHECK_FUNCS_ONCE([getlogin_r])
   if test $ac_cv_func_getlogin_r = no; then
-    gl_GETLOGIN_R_SUBSTITUTE
+    AC_LIBOBJ([getlogin_r])
+    gl_PREREQ_GETLOGIN_R
+    if test $ac_cv_have_decl_getlogin_r = yes; then
+      HAVE_DECL_GETLOGIN_R=1
+    else
+      HAVE_DECL_GETLOGIN_R=0
+    fi
   fi
 ])
 
 AC_DEFUN([gl_PREREQ_GETLOGIN_R],
 [
-  AC_CHECK_HEADERS_ONCE([unistd.h])
   AC_CHECK_DECLS_ONCE([getlogin])
   AC_CHECK_DECLS_ONCE([getlogin_r])
 ])
--- a/m4/readlink.m4
+++ b/m4/readlink.m4
@@ -1,13 +1,15 @@
-# readlink.m4 serial 2
-dnl Copyright (C) 2003 Free Software Foundation, Inc.
+# readlink.m4 serial 3
+dnl Copyright (C) 2003, 2007 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
 
 AC_DEFUN([gl_FUNC_READLINK],
 [
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
   AC_CHECK_FUNCS(readlink)
   if test $ac_cv_func_readlink = no; then
+    HAVE_READLINK=0
     AC_LIBOBJ(readlink)
     gl_PREREQ_READLINK
   fi
--- a/m4/unistd_h.m4
+++ b/m4/unistd_h.m4
@@ -1,39 +1,51 @@
-# unistd_h.m4 serial 4
+# unistd_h.m4 serial 5
 dnl Copyright (C) 2006-2007 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
 dnl with or without modifications, as long as this notice is preserved.
 
-dnl Written by Simon Josefsson
+dnl Written by Simon Josefsson, Bruno Haible.
 
-AC_DEFUN([gl_HEADER_UNISTD],
+AC_DEFUN([gl_UNISTD_H],
 [
   dnl Use AC_REQUIRE here, so that the default behavior below is expanded
   dnl once only, before all statements that occur in other macros.
-  AC_REQUIRE([gl_HEADER_UNISTD_DEFAULTS])
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
 
-  AC_CHECK_HEADERS([unistd.h], [], [
-    UNISTD_H='unistd.h'
-  ])
-  dnl This module decides to build unistd.h if it is missing.
-  dnl The fchdir module decides to build unistd.h if fchdir() is missing.
-  dnl Therefore check for the prerequisites of lib/unistd.h always.
-  gl_PREREQ_UNISTD
-])
-
-dnl Prerequisites of lib/unistd.h.
-AC_DEFUN([gl_PREREQ_UNISTD],
-[
   AC_CHECK_HEADERS_ONCE([unistd.h])
   if test $ac_cv_header_unistd_h = yes; then
+    HAVE_UNISTD_H=1
     gl_ABSOLUTE_HEADER([unistd.h])
     ABSOLUTE_UNISTD_H=\"$gl_cv_absolute_unistd_h\"
+  else
+    HAVE_UNISTD_H=0
   fi
+  AC_SUBST([HAVE_UNISTD_H])
   AC_SUBST([ABSOLUTE_UNISTD_H])
 ])
 
-AC_DEFUN([gl_HEADER_UNISTD_DEFAULTS],
+AC_DEFUN([gl_UNISTD_MODULE_INDICATOR],
+[
+  dnl Use AC_REQUIRE here, so that the default settings are expanded once only.
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+  GNULIB_[]m4_translit([$1],[abcdefghijklmnopqrstuvwxyz./-],[ABCDEFGHIJKLMNOPQRSTUVWXYZ___])=1
+])
+
+AC_DEFUN([gl_UNISTD_H_DEFAULTS],
 [
-  UNISTD_H=
-  AC_SUBST(UNISTD_H)
+  GNULIB_CHOWN=0;         AC_SUBST([GNULIB_CHOWN])
+  GNULIB_DUP2=0;          AC_SUBST([GNULIB_DUP2])
+  GNULIB_FCHDIR=0;        AC_SUBST([GNULIB_FCHDIR])
+  GNULIB_FTRUNCATE=0;     AC_SUBST([GNULIB_FTRUNCATE])
+  GNULIB_GETCWD=0;        AC_SUBST([GNULIB_GETCWD])
+  GNULIB_GETLOGIN_R=0;    AC_SUBST([GNULIB_GETLOGIN_R])
+  GNULIB_READLINK=0;      AC_SUBST([GNULIB_READLINK])
+  dnl Assume proper GNU behavior unless another module says otherwise.
+  HAVE_DUP2=1;            AC_SUBST([HAVE_DUP2])
+  HAVE_FTRUNCATE=1;       AC_SUBST([HAVE_FTRUNCATE])
+  HAVE_READLINK=1;        AC_SUBST([HAVE_READLINK])
+  HAVE_DECL_GETLOGIN_R=1; AC_SUBST([HAVE_DECL_GETLOGIN_R])
+  REPLACE_CHOWN=0;        AC_SUBST([REPLACE_CHOWN])
+  REPLACE_FCHDIR=0;       AC_SUBST([REPLACE_FCHDIR])
+  REPLACE_GETCWD=0;       AC_SUBST([REPLACE_GETCWD])
 ])
--- a/modules/chown
+++ b/modules/chown
@@ -7,10 +7,12 @@
 m4/chown.m4
 
 Depends-on:
+unistd
 sys_stat
 
 configure.ac:
 gl_FUNC_CHOWN
+gl_UNISTD_MODULE_INDICATOR([chown])
 
 Makefile.am:
 
--- a/modules/dup2
+++ b/modules/dup2
@@ -6,9 +6,11 @@
 m4/dup2.m4
 
 Depends-on:
+unistd
 
 configure.ac:
 gl_FUNC_DUP2
+gl_UNISTD_MODULE_INDICATOR([dup2])
 
 Makefile.am:
 
--- a/modules/fchdir
+++ b/modules/fchdir
@@ -17,6 +17,7 @@
 
 configure.ac:
 gl_FUNC_FCHDIR
+gl_UNISTD_MODULE_INDICATOR([fchdir])
 
 Makefile.am:
 BUILT_SOURCES += $(DIRENT_H)
@@ -27,6 +28,7 @@
 	rm -f $@-t $@
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''ABSOLUTE_DIRENT_H''@|$(ABSOLUTE_DIRENT_H)|g' \
+	      -e 's|@''REPLACE_FCHDIR''@|$(REPLACE_FCHDIR)|g' \
 	      < $(srcdir)/dirent_.h; \
 	} > $@-t
 	mv $@-t $@
--- a/modules/ftruncate
+++ b/modules/ftruncate
@@ -6,9 +6,11 @@
 m4/ftruncate.m4
 
 Depends-on:
+unistd
 
 configure.ac:
 gl_FUNC_FTRUNCATE
+gl_UNISTD_MODULE_INDICATOR([ftruncate])
 
 Makefile.am:
 
--- a/modules/getcwd
+++ b/modules/getcwd
@@ -2,7 +2,6 @@
 Return the current working directory.
 
 Files:
-lib/getcwd.h
 lib/getcwd.c
 m4/getcwd-abort-bug.m4
 m4/getcwd-path-max.m4
@@ -13,14 +12,16 @@
 d-ino
 extensions
 stdbool
+unistd
 
 configure.ac:
 gl_FUNC_GETCWD
+gl_UNISTD_MODULE_INDICATOR([getcwd])
 
 Makefile.am:
 
 Include:
-"getcwd.h"
+<unistd.h>
 
 License:
 GPL
--- a/modules/getlogin_r
+++ b/modules/getlogin_r
@@ -2,7 +2,6 @@
 getlogin_r() function: Get user name to a buffer allocated by the caller.
 
 Files:
-lib/getlogin_r.h
 lib/getlogin_r.c
 m4/getlogin_r.m4
 
@@ -12,11 +11,12 @@
 
 configure.ac:
 gl_GETLOGIN_R
+gl_UNISTD_MODULE_INDICATOR([getlogin_r])
 
 Makefile.am:
 
 Include:
-"getlogin_r.h"
+<unistd.h>
 
 License:
 LGPL
--- a/modules/readlink
+++ b/modules/readlink
@@ -6,13 +6,16 @@
 m4/readlink.m4
 
 Depends-on:
+unistd
 
 configure.ac:
 gl_FUNC_READLINK
+gl_UNISTD_MODULE_INDICATOR([readlink])
 
 Makefile.am:
 
 Include:
+<unistd.h>
 
 License:
 LGPL
--- a/modules/unistd
+++ b/modules/unistd
@@ -1,5 +1,5 @@
 Description:
-A <unistd.h> for systems lacking it.
+A GNU-like <unistd.h>.
 
 Files:
 m4/unistd_h.m4
@@ -7,19 +7,35 @@
 
 Depends-on:
 absolute-header
+link-warning
 
 configure.ac:
-gl_HEADER_UNISTD
+gl_UNISTD_H
 
 Makefile.am:
-BUILT_SOURCES += $(UNISTD_H)
+BUILT_SOURCES += unistd.h
 
 # We need the following in order to create an empty placeholder for
 # <unistd.h> when the system doesn't have one.
 unistd.h: unistd_.h
 	rm -f $@-t $@
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
-	  sed -e 's|@''ABSOLUTE_UNISTD_H''@|$(ABSOLUTE_UNISTD_H)|g' \
+	  sed -e 's|@''HAVE_UNISTD_H''@|$(HAVE_UNISTD_H)|g' \
+	      -e 's|@''ABSOLUTE_UNISTD_H''@|$(ABSOLUTE_UNISTD_H)|g' \
+	      -e 's|@''GNULIB_CHOWN''@|$(GNULIB_CHOWN)|g' \
+	      -e 's|@''GNULIB_DUP2''@|$(GNULIB_DUP2)|g' \
+	      -e 's|@''GNULIB_FCHDIR''@|$(GNULIB_FCHDIR)|g' \
+	      -e 's|@''GNULIB_FTRUNCATE''@|$(GNULIB_FTRUNCATE)|g' \
+	      -e 's|@''GNULIB_GETCWD''@|$(GNULIB_GETCWD)|g' \
+	      -e 's|@''GNULIB_GETLOGIN_R''@|$(GNULIB_GETLOGIN_R)|g' \
+	      -e 's|@''GNULIB_READLINK''@|$(GNULIB_READLINK)|g' \
+	      -e 's|@''HAVE_DUP2''@|$(HAVE_DUP2)|g' \
+	      -e 's|@''HAVE_FTRUNCATE''@|$(HAVE_FTRUNCATE)|g' \
+	      -e 's|@''HAVE_READLINK''@|$(HAVE_READLINK)|g' \
+	      -e 's|@''HAVE_DECL_GETLOGIN_R''@|$(HAVE_DECL_GETLOGIN_R)|g' \
+	      -e 's|@''REPLACE_CHOWN''@|$(REPLACE_CHOWN)|g' \
+	      -e 's|@''REPLACE_FCHDIR''@|$(REPLACE_FCHDIR)|g' \
+	      -e 's|@''REPLACE_GETCWD''@|$(REPLACE_GETCWD)|g' \
 	      < $(srcdir)/unistd_.h; \
 	} > $@-t
 	mv $@-t $@