summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2009-12-11 20:59:01 +0100
committerBruno Haible <bruno@clisp.org>2009-12-11 20:59:01 +0100
commita3255b1b9848874572e0df9d0609e263da50de4b (patch)
tree11f2295f706870686445acedc714ef01eed737cd
parent9a9d32fbb36e8363c32de6068765829d8232949e (diff)
downloadgnulib-a3255b1b9848874572e0df9d0609e263da50de4b.tar.gz
New module 'fd-safer-flag'.
-rw-r--r--ChangeLog14
-rw-r--r--lib/dup-safer-flag.c54
-rw-r--r--lib/dup-safer.c31
-rw-r--r--lib/fd-safer-flag.c52
-rw-r--r--lib/fd-safer.c30
-rw-r--r--lib/unistd-safer.h2
-rw-r--r--modules/cloexec1
-rw-r--r--modules/fd-safer-flag26
-rw-r--r--modules/pipe2-safer2
-rw-r--r--modules/stdlib-safer1
-rw-r--r--modules/unistd-safer-tests1
11 files changed, 150 insertions, 64 deletions
diff --git a/ChangeLog b/ChangeLog
index 66219d604f..99a631c916 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,19 @@
2009-12-11 Bruno Haible <bruno@clisp.org>
+ New module 'fd-safer-flag'.
+ * lib/dup-safer-flag.c: New file, extracted from lib/dup-safer.c.
+ * lib/dup-safer.c (dup_safer_flag): Remove function.
+ * lib/fd-safer-flag.c: New file, extracted from lib/fd-safer.c.
+ * lib/fd-safer.c (fd_safer_flag): Remove function.
+ * lib/unistd-safer.h (dup_safer_flag, fd_safer_flag): Update condition.
+ * modules/cloexec (configure.ac): Drop indicator macro.
+ * modules/fd-safer-flag: New file.
+ * modules/pipe2-safer (Depends-on): Add fd-safer-flag. Remove cloexec.
+ * modules/stdlib-safer (Depends-on): Add fd-safer-flag.
+ * modules/unistd-safer-tests (Depends-on): Add fd-safer-flag.
+
+2009-12-11 Bruno Haible <bruno@clisp.org>
+
Tests for module 'nl_langinfo'.
* modules/nl_langinfo-tests: New file.
* tests/test-nl_langinfo.sh: New file.
diff --git a/lib/dup-safer-flag.c b/lib/dup-safer-flag.c
new file mode 100644
index 0000000000..3549d0d12f
--- /dev/null
+++ b/lib/dup-safer-flag.c
@@ -0,0 +1,54 @@
+/* Duplicate a file descriptor result, avoiding clobbering
+ STD{IN,OUT,ERR}_FILENO, with specific flags.
+
+ Copyright (C) 2001, 2004-2006, 2009 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Paul Eggert and Eric Blake. */
+
+#include <config.h>
+
+/* Specification. */
+#include "unistd-safer.h"
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "cloexec.h"
+
+#ifndef O_CLOEXEC
+# define O_CLOEXEC 0
+#endif
+
+/* Like dup, but do not return STDIN_FILENO, STDOUT_FILENO, or
+ STDERR_FILENO. If FLAG contains O_CLOEXEC, behave like
+ fcntl(F_DUPFD_CLOEXEC) rather than fcntl(F_DUPFD). */
+
+int
+dup_safer_flag (int fd, int flag)
+{
+ if (flag & O_CLOEXEC)
+ {
+#if defined F_DUPFD_CLOEXEC && !REPLACE_FCHDIR
+ return fcntl (fd, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
+#else
+ /* fd_safer_flag calls us back, but eventually the recursion
+ unwinds and does the right thing. */
+ fd = dup_cloexec (fd);
+ return fd_safer_flag (fd, flag);
+#endif
+ }
+ return dup_safer (fd);
+}
diff --git a/lib/dup-safer.c b/lib/dup-safer.c
index 2af8b6a478..bb11ba54a7 100644
--- a/lib/dup-safer.c
+++ b/lib/dup-safer.c
@@ -39,34 +39,3 @@ dup_safer (int fd)
return fd_safer (dup (fd));
#endif
}
-
-#if GNULIB_CLOEXEC
-
-# include "cloexec.h"
-
-# ifndef O_CLOEXEC
-# define O_CLOEXEC 0
-# endif
-
-/* Like dup, but do not return STDIN_FILENO, STDOUT_FILENO, or
- STDERR_FILENO. If FLAG contains O_CLOEXEC, behave like
- fcntl(F_DUPFD_CLOEXEC) rather than fcntl(F_DUPFD). */
-
-int
-dup_safer_flag (int fd, int flag)
-{
- if (flag & O_CLOEXEC)
- {
-# if defined F_DUPFD_CLOEXEC && !REPLACE_FCHDIR
- return fcntl (fd, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
-# else
- /* fd_safer_flag calls us back, but eventually the recursion
- unwinds and does the right thing. */
- fd = dup_cloexec (fd);
- return fd_safer_flag (fd, flag);
-# endif
- }
- return dup_safer (fd);
-}
-
-#endif
diff --git a/lib/fd-safer-flag.c b/lib/fd-safer-flag.c
new file mode 100644
index 0000000000..3cb546b249
--- /dev/null
+++ b/lib/fd-safer-flag.c
@@ -0,0 +1,52 @@
+/* Adjust a file descriptor result so that it avoids clobbering
+ STD{IN,OUT,ERR}_FILENO, with specific flags.
+
+ Copyright (C) 2005-2006, 2009 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 3 of the License, 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, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Paul Eggert and Eric Blake. */
+
+#include <config.h>
+
+/* Specification. */
+#include "unistd-safer.h"
+
+#include <errno.h>
+#include <unistd.h>
+
+/* Return FD, unless FD would be a copy of standard input, output, or
+ error; in that case, return a duplicate of FD, closing FD. If FLAG
+ contains O_CLOEXEC, the returned FD will have close-on-exec
+ semantics. On failure to duplicate, close FD, set errno, and
+ return -1. Preserve errno if FD is negative, so that the caller
+ can always inspect errno when the returned value is negative.
+
+ This function is usefully wrapped around functions that return file
+ descriptors, e.g., fd_safer_flag (open ("file", O_RDONLY | flag), flag). */
+
+int
+fd_safer_flag (int fd, int flag)
+{
+ if (STDIN_FILENO <= fd && fd <= STDERR_FILENO)
+ {
+ int f = dup_safer_flag (fd, flag);
+ int e = errno;
+ close (fd);
+ errno = e;
+ fd = f;
+ }
+
+ return fd;
+}
diff --git a/lib/fd-safer.c b/lib/fd-safer.c
index d2e1309c84..d2aa561771 100644
--- a/lib/fd-safer.c
+++ b/lib/fd-safer.c
@@ -22,7 +22,6 @@
#include "unistd-safer.h"
#include <errno.h>
-
#include <unistd.h>
/* Return FD, unless FD would be a copy of standard input, output, or
@@ -48,32 +47,3 @@ fd_safer (int fd)
return fd;
}
-
-#if GNULIB_CLOEXEC
-
-/* Return FD, unless FD would be a copy of standard input, output, or
- error; in that case, return a duplicate of FD, closing FD. If FLAG
- contains O_CLOEXEC, the returned FD will have close-on-exec
- semantics. On failure to duplicate, close FD, set errno, and
- return -1. Preserve errno if FD is negative, so that the caller
- can always inspect errno when the returned value is negative.
-
- This function is usefully wrapped around functions that return file
- descriptors, e.g., fd_safer_flag (open ("file", O_RDONLY | flag), flag). */
-
-int
-fd_safer_flag (int fd, int flag)
-{
- if (STDIN_FILENO <= fd && fd <= STDERR_FILENO)
- {
- int f = dup_safer_flag (fd, flag);
- int e = errno;
- close (fd);
- errno = e;
- fd = f;
- }
-
- return fd;
-}
-
-#endif /* GNULIB_CLOEXEC */
diff --git a/lib/unistd-safer.h b/lib/unistd-safer.h
index 70cc6998a5..6efe0b2bbd 100644
--- a/lib/unistd-safer.h
+++ b/lib/unistd-safer.h
@@ -21,7 +21,7 @@ int dup_safer (int);
int fd_safer (int);
int pipe_safer (int[2]);
-#if GNULIB_CLOEXEC
+#if GNULIB_FD_SAFER_FLAG
int dup_safer_flag (int, int);
int fd_safer_flag (int, int);
#endif
diff --git a/modules/cloexec b/modules/cloexec
index 93f518eca5..bb2767d888 100644
--- a/modules/cloexec
+++ b/modules/cloexec
@@ -12,7 +12,6 @@ stdbool
configure.ac:
gl_CLOEXEC
-gl_MODULE_INDICATOR([cloexec])
Makefile.am:
diff --git a/modules/fd-safer-flag b/modules/fd-safer-flag
new file mode 100644
index 0000000000..887b797477
--- /dev/null
+++ b/modules/fd-safer-flag
@@ -0,0 +1,26 @@
+Description:
+fd_safer_flag() function: adjust a file descriptor result so that it avoids
+clobbering STD{IN,OUT,ERR}_FILENO, with specific flags.
+
+Files:
+lib/fd-safer-flag.c
+lib/dup-safer-flag.c
+
+Depends-on:
+unistd-safer
+cloexec
+
+configure.ac:
+gl_MODULE_INDICATOR([fd-safer-flag])
+
+Makefile.am:
+lib_SOURCES += fd-safer-flag.c dup-safer-flag.c
+
+Include:
+"unistd-safer.h"
+
+License:
+GPL
+
+Maintainer:
+Eric Blake
diff --git a/modules/pipe2-safer b/modules/pipe2-safer
index 7297c4ce62..e01a932f32 100644
--- a/modules/pipe2-safer
+++ b/modules/pipe2-safer
@@ -6,7 +6,7 @@ Files:
lib/pipe2-safer.c
Depends-on:
-cloexec
+fd-safer-flag
pipe2
unistd-safer
diff --git a/modules/stdlib-safer b/modules/stdlib-safer
index ddeb865ac6..43e6a2db90 100644
--- a/modules/stdlib-safer
+++ b/modules/stdlib-safer
@@ -8,6 +8,7 @@ lib/mkstemp-safer.c
m4/stdlib-safer.m4
Depends-on:
+fd-safer-flag
mkstemp
stdlib
unistd-safer
diff --git a/modules/unistd-safer-tests b/modules/unistd-safer-tests
index 17b17a035b..a6da5a9bd5 100644
--- a/modules/unistd-safer-tests
+++ b/modules/unistd-safer-tests
@@ -4,6 +4,7 @@ tests/test-dup-safer.c
Depends-on:
binary-io
cloexec
+fd-safer-flag
stdbool
configure.ac: