From 5e6b92bb993a0e913877c495687556df918d7d86 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 7 Oct 2007 04:39:17 +0200 Subject: [PATCH] New module 'open'. --- ChangeLog | 15 ++++++++++++ doc/functions/open.texi | 5 +++- lib/fchdir.c | 4 ++++ lib/fcntl.in.h | 4 ++-- lib/open.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ m4/fcntl_h.m4 | 15 ++++++++++++ m4/open.m4 | 17 ++++++++++++++ modules/fcntl | 2 ++ modules/open | 26 +++++++++++++++++++++ 9 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 lib/open.c create mode 100644 m4/open.m4 create mode 100644 modules/open diff --git a/ChangeLog b/ChangeLog index c2f695551..af7e6537d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2007-10-06 Bruno Haible + + * 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 * modules/ceill-tests: New file. diff --git a/doc/functions/open.texi b/doc/functions/open.texi index 78931e960..7a6d5fc76 100644 --- a/doc/functions/open.texi +++ b/doc/functions/open.texi @@ -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: diff --git a/lib/fchdir.c b/lib/fchdir.c index e3ec2fcf6..1f7cf994c 100644 --- a/lib/fchdir.c +++ b/lib/fchdir.c @@ -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) { diff --git a/lib/fcntl.in.h b/lib/fcntl.in.h index 21f73d7e2..acf9de339 100644 --- a/lib/fcntl.in.h +++ b/lib/fcntl.in.h @@ -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 index 000000000..7f7134e59 --- /dev/null +++ b/lib/open.c @@ -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 , 2007. */ + +#include + +/* Specification. */ +#include + +/* If the fchdir replacement is used, open() is defined in fchdir.c. */ +#ifndef FCHDIR_REPLACEMENT + +# include +# include +# include +# include + +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 diff --git a/m4/fcntl_h.m4 b/m4/fcntl_h.m4 index 3ae7efe8e..e3b9aa526 100644 --- a/m4/fcntl_h.m4 +++ b/m4/fcntl_h.m4 @@ -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 index 000000000..90d528ac1 --- /dev/null +++ b/m4/open.m4 @@ -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 +]) diff --git a/modules/fcntl b/modules/fcntl index 0fafba33e..8e1f327a9 100644 --- a/modules/fcntl +++ b/modules/fcntl @@ -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 index 000000000..f792adf1a --- /dev/null +++ b/modules/open @@ -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: + + +License: +LGPL + +Maintainer: +Bruno Haible + -- 2.11.0