dup-safer-flag.c revision 1.1 1 1.1 christos /* Duplicate a file descriptor result, avoiding clobbering
2 1.1 christos STD{IN,OUT,ERR}_FILENO, with specific flags.
3 1.1 christos
4 1.1 christos Copyright (C) 2001, 2004-2006, 2009-2020 Free Software Foundation, Inc.
5 1.1 christos
6 1.1 christos This program is free software: you can redistribute it and/or modify
7 1.1 christos it under the terms of the GNU General Public License as published by
8 1.1 christos the Free Software Foundation; either version 3 of the License, or
9 1.1 christos (at your option) any later version.
10 1.1 christos
11 1.1 christos This program is distributed in the hope that it will be useful,
12 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
13 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 1.1 christos GNU General Public License for more details.
15 1.1 christos
16 1.1 christos You should have received a copy of the GNU General Public License
17 1.1 christos along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 1.1 christos
19 1.1 christos /* Written by Paul Eggert and Eric Blake. */
20 1.1 christos
21 1.1 christos #include <config.h>
22 1.1 christos
23 1.1 christos /* Specification. */
24 1.1 christos #include "unistd-safer.h"
25 1.1 christos
26 1.1 christos #include <fcntl.h>
27 1.1 christos #include <unistd.h>
28 1.1 christos
29 1.1 christos /* Like dup, but do not return STDIN_FILENO, STDOUT_FILENO, or
30 1.1 christos STDERR_FILENO. If FLAG contains O_CLOEXEC, behave like
31 1.1 christos fcntl(F_DUPFD_CLOEXEC) rather than fcntl(F_DUPFD). */
32 1.1 christos
33 1.1 christos int
34 1.1 christos dup_safer_flag (int fd, int flag)
35 1.1 christos {
36 1.1 christos return fcntl (fd, (flag & O_CLOEXEC) ? F_DUPFD_CLOEXEC : F_DUPFD,
37 1.1 christos STDERR_FILENO + 1);
38 1.1 christos }
39