New module 'open'.
authorBruno Haible <bruno@clisp.org>
Sun, 7 Oct 2007 02:39:17 +0000 (04:39 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 7 Oct 2007 02:39:17 +0000 (04:39 +0200)
ChangeLog
doc/functions/open.texi
lib/fchdir.c
lib/fcntl.in.h
lib/open.c [new file with mode: 0644]
m4/fcntl_h.m4
m4/open.m4 [new file with mode: 0644]
modules/fcntl
modules/open [new file with mode: 0644]

index c2f6955..af7e653 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2007-10-06  Bruno Haible  <bruno@clisp.org>
+
+       * modules/open: New file.
+       * lib/open.c: New file.
+       * m4/open.m4: New file.
+       * lib/fchdir.c (open): If the gnulib module 'open' is used, do what
+       lib/open.c does.
+       * lib/fcntl.in.h (open): Declare also if replaced by the 'open' module.
+       * m4/fcntl_h.m4 (gl_FCNTL_MODULE_INDICATOR, gl_FCNTL_H_DEFAULTS): New
+       macros.
+       (gl_FCNTL_H): Require gl_FCNTL_H_DEFAULTS.
+       * modules/fcntl (Makefile.am): Also substitute GNULIB_OPEN and
+       REPLACE_OPEN.
+       * doc/functions/open.texi: Mention the 'open' module.
+
 2007-10-04  Bruno Haible  <bruno@clisp.org>
 
        * modules/ceill-tests: New file.
index 78931e9..7a6d5fc 100644 (file)
@@ -4,10 +4,13 @@
 
 POSIX specification: @url{http://www.opengroup.org/susv3xsh/open.html}
 
-Gnulib module: ---
+Gnulib module: open
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+On Windows platforms (excluding Cygwin), this function does usually not
+recognize the @file{/dev/null} filename.
 @end itemize
 
 Portability problems not fixed by Gnulib:
index e3ec2fc..1f7cf99 100644 (file)
@@ -117,6 +117,10 @@ open (const char *filename, int flags, ...)
 
       va_end (arg);
     }
+#if defined GNULIB_OPEN && ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
+  if (strcmp (filename, "/dev/null") == 0)
+    filename = "NUL";
+#endif
   fd = open (filename, flags, mode);
   if (fd >= 0)
     {
index 21f73d7..acf9de3 100644 (file)
@@ -36,9 +36,9 @@
 extern "C" {
 #endif
 
-#ifdef FCHDIR_REPLACEMENT
+#if (@GNULIB_OPEN@ && @REPLACE_OPEN@) || defined FCHDIR_REPLACEMENT
 # define open rpl_open
-extern int open (const char *, int, ...);
+extern int open (const char *filename, int flags, ...);
 #endif
 
 #ifdef __cplusplus
diff --git a/lib/open.c b/lib/open.c
new file mode 100644 (file)
index 0000000..7f7134e
--- /dev/null
@@ -0,0 +1,62 @@
+/* Open a descriptor to a file.
+   Copyright (C) 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
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License along
+   with this program; if not, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <fcntl.h>
+
+/* If the fchdir replacement is used, open() is defined in fchdir.c.  */
+#ifndef FCHDIR_REPLACEMENT
+
+# include <stdarg.h>
+# include <string.h>
+# include <sys/types.h>
+# include <sys/stat.h>
+
+int
+open (const char *filename, int flags, ...)
+# undef open
+{
+  mode_t mode;
+
+  mode = 0;
+  if (flags & O_CREAT)
+    {
+      va_list arg;
+      va_start (arg, flags);
+
+      /* If mode_t is narrower than int, use the promoted type (int),
+        not mode_t.  Use sizeof to guess whether mode_t is narrower;
+        we don't know of any practical counterexamples.  */
+      mode = (sizeof (mode_t) < sizeof (int)
+             ? va_arg (arg, int)
+             : va_arg (arg, mode_t));
+
+      va_end (arg);
+    }
+
+# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+  if (strcmp (filename, "/dev/null") == 0)
+    filename = "NUL";
+# endif
+
+  return open (filename, flags, mode);
+}
+#endif
index 3ae7efe..e3b9aa5 100644 (file)
@@ -8,6 +8,7 @@ dnl Written by Paul Eggert.
 
 AC_DEFUN([gl_FCNTL_H],
 [
+  AC_REQUIRE([gl_FCNTL_H_DEFAULTS])
   AC_CACHE_CHECK([for working fcntl.h], gl_cv_header_working_fcntl_h,
     [AC_RUN_IFELSE(
        [AC_LANG_PROGRAM(
@@ -77,3 +78,17 @@ AC_DEFUN([gl_FCNTL_H],
   FCNTL_H='fcntl.h'
   AC_SUBST([FCNTL_H])
 ])
+
+AC_DEFUN([gl_FCNTL_MODULE_INDICATOR],
+[
+  dnl Use AC_REQUIRE here, so that the default settings are expanded once only.
+  AC_REQUIRE([gl_FCNTL_H_DEFAULTS])
+  GNULIB_[]m4_translit([$1],[abcdefghijklmnopqrstuvwxyz./-],[ABCDEFGHIJKLMNOPQRSTUVWXYZ___])=1
+])
+
+AC_DEFUN([gl_FCNTL_H_DEFAULTS],
+[
+  GNULIB_OPEN=0;  AC_SUBST([GNULIB_OPEN])
+  dnl Assume proper GNU behavior unless another module says otherwise.
+  REPLACE_OPEN=0; AC_SUBST([REPLACE_OPEN])
+])
diff --git a/m4/open.m4 b/m4/open.m4
new file mode 100644 (file)
index 0000000..90d528a
--- /dev/null
@@ -0,0 +1,17 @@
+# open.m4 serial 1
+dnl Copyright (C) 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_OPEN],
+[
+  AC_REQUIRE([gl_FCNTL_H_DEFAULTS])
+  AC_REQUIRE([AC_CANONICAL_HOST])
+  case "$host_os" in
+    mingw* | pw*)
+      REPLACE_OPEN=1
+      AC_LIBOBJ([open])
+      ;;
+  esac
+])
index 0fafba3..8e1f327 100644 (file)
@@ -22,6 +22,8 @@ fcntl.h: fcntl.in.h
        { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
          sed -e 's/@''INCLUDE_NEXT''@/$(INCLUDE_NEXT)/g' \
              -e 's|@''NEXT_FCNTL_H''@|$(NEXT_FCNTL_H)|g' \
+             -e 's|@''GNULIB_OPEN''@|$(GNULIB_OPEN)|g' \
+             -e 's|@''REPLACE_OPEN''@|$(REPLACE_OPEN)|g' \
              < $(srcdir)/fcntl.in.h; \
        } > $@-t
        mv $@-t $@
diff --git a/modules/open b/modules/open
new file mode 100644 (file)
index 0000000..f792adf
--- /dev/null
@@ -0,0 +1,26 @@
+Description:
+open() function: open a descriptor to a file.
+
+Files:
+lib/open.c
+m4/open.m4
+
+Depends-on:
+fcntl
+
+configure.ac:
+gl_FUNC_OPEN
+gl_MODULE_INDICATOR([open])
+gl_FCNTL_MODULE_INDICATOR([open])
+
+Makefile.am:
+
+Include:
+<fcntl.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
+