summaryrefslogtreecommitdiff
path: root/lib/dup2.c
diff options
context:
space:
mode:
authorEric Blake <ebb9@byu.net>2009-07-21 06:48:26 -0600
committerEric Blake <ebb9@byu.net>2009-07-21 07:08:06 -0600
commitddd625ddd088a87d5598256d09b2036c63063386 (patch)
tree57eb630aa1bdf55c46b7b1008aa59619e49d67d4 /lib/dup2.c
parent77ef628397e3c8951051305e9601a7800c895b73 (diff)
downloadgnulib-ddd625ddd088a87d5598256d09b2036c63063386.tar.gz
dup2: work around mingw and cygwin 1.5 bug
* m4/dup2.m4 (gl_FUNC_DUP2): Detect mingw bug. * m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Add witness. * modules/unistd (Makefile.am): Substitute it. * lib/unistd.in.h (dup2): Declare the replacement. * lib/dup2.c (dup2) [REPLACE_DUP2]: Implement it. * doc/posix-functions/dup2.texi (dup2): Document the bugs. * lib/fchdir.c (rpl_dup2): Don't collide with mingw replacement. * modules/execute (Depends-on): Add dup2. * modules/fseterr (Depends-on): Likewise. * modules/pipe (Depends-on): Likewise. * modules/posix_spawn-internal (Depends-on): Likewise. Signed-off-by: Eric Blake <ebb9@byu.net>
Diffstat (limited to 'lib/dup2.c')
-rw-r--r--lib/dup2.c29
1 files changed, 23 insertions, 6 deletions
diff --git a/lib/dup2.c b/lib/dup2.c
index 09990823be..e5d3de47b5 100644
--- a/lib/dup2.c
+++ b/lib/dup2.c
@@ -1,6 +1,7 @@
/* Duplicate an open file descriptor to a specified file descriptor.
- Copyright (C) 1999, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
+ Copyright (C) 1999, 2004, 2005, 2006, 2007, 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
@@ -25,7 +26,22 @@
#include <errno.h>
#include <fcntl.h>
-#ifndef F_DUPFD
+#if REPLACE_DUP2
+/* On mingw, dup2 exists, but always returns 0 for success. */
+int
+dup2 (int fd, int desired_fd)
+#undef dup2
+{
+ int result = dup2 (fd, desired_fd);
+ if (result == 0)
+ result = desired_fd;
+ return result;
+}
+
+#else /* !REPLACE_DUP2 */
+/* On older platforms, dup2 did not exist. */
+
+# ifndef F_DUPFD
static int
dupfd (int fd, int desired_fd)
{
@@ -41,7 +57,7 @@ dupfd (int fd, int desired_fd)
return r;
}
}
-#endif
+# endif
int
dup2 (int fd, int desired_fd)
@@ -49,9 +65,10 @@ dup2 (int fd, int desired_fd)
if (fd == desired_fd)
return fd;
close (desired_fd);
-#ifdef F_DUPFD
+# ifdef F_DUPFD
return fcntl (fd, F_DUPFD, desired_fd);
-#else
+# else
return dupfd (fd, desired_fd);
-#endif
+# endif
}
+#endif /* !REPLACE_DUP2 */