summaryrefslogtreecommitdiffstats
path: root/gl/dup-safer.c
diff options
context:
space:
mode:
Diffstat (limited to 'gl/dup-safer.c')
-rw-r--r--gl/dup-safer.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/gl/dup-safer.c b/gl/dup-safer.c
new file mode 100644
index 00000000..7b12b615
--- /dev/null
+++ b/gl/dup-safer.c
@@ -0,0 +1,45 @@
1/* Invoke dup, but avoid some glitches.
2
3 Copyright (C) 2001, 2004, 2005, 2006 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19/* Written by Paul Eggert. */
20
21#include <config.h>
22
23#include "unistd-safer.h"
24
25#include <fcntl.h>
26
27#include <unistd.h>
28#ifndef STDERR_FILENO
29# define STDERR_FILENO 2
30#endif
31
32/* Like dup, but do not return STDIN_FILENO, STDOUT_FILENO, or
33 STDERR_FILENO. */
34
35int
36dup_safer (int fd)
37{
38#if defined F_DUPFD && !defined FCHDIR_REPLACEMENT
39 return fcntl (fd, F_DUPFD, STDERR_FILENO + 1);
40#else
41 /* fd_safer calls us back, but eventually the recursion unwinds and
42 does the right thing. */
43 return fd_safer (dup (fd));
44#endif
45}