105436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Duplicate a file descriptor result, avoiding clobbering
205436638acc7c010349a69c3395f1a57c642dc62Ying Wang   STD{IN,OUT,ERR}_FILENO, with specific flags.
305436638acc7c010349a69c3395f1a57c642dc62Ying Wang
405436638acc7c010349a69c3395f1a57c642dc62Ying Wang   Copyright (C) 2001, 2004-2006, 2009-2012 Free Software Foundation, Inc.
505436638acc7c010349a69c3395f1a57c642dc62Ying Wang
605436638acc7c010349a69c3395f1a57c642dc62Ying Wang   This program is free software: you can redistribute it and/or modify
705436638acc7c010349a69c3395f1a57c642dc62Ying Wang   it under the terms of the GNU General Public License as published by
805436638acc7c010349a69c3395f1a57c642dc62Ying Wang   the Free Software Foundation; either version 3 of the License, or
905436638acc7c010349a69c3395f1a57c642dc62Ying Wang   (at your option) any later version.
1005436638acc7c010349a69c3395f1a57c642dc62Ying Wang
1105436638acc7c010349a69c3395f1a57c642dc62Ying Wang   This program is distributed in the hope that it will be useful,
1205436638acc7c010349a69c3395f1a57c642dc62Ying Wang   but WITHOUT ANY WARRANTY; without even the implied warranty of
1305436638acc7c010349a69c3395f1a57c642dc62Ying Wang   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1405436638acc7c010349a69c3395f1a57c642dc62Ying Wang   GNU General Public License for more details.
1505436638acc7c010349a69c3395f1a57c642dc62Ying Wang
1605436638acc7c010349a69c3395f1a57c642dc62Ying Wang   You should have received a copy of the GNU General Public License
1705436638acc7c010349a69c3395f1a57c642dc62Ying Wang   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
1805436638acc7c010349a69c3395f1a57c642dc62Ying Wang
1905436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Written by Paul Eggert and Eric Blake.  */
2005436638acc7c010349a69c3395f1a57c642dc62Ying Wang
2105436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include <config.h>
2205436638acc7c010349a69c3395f1a57c642dc62Ying Wang
2305436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Specification.  */
2405436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include "unistd-safer.h"
2505436638acc7c010349a69c3395f1a57c642dc62Ying Wang
2605436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include <fcntl.h>
2705436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include <unistd.h>
2805436638acc7c010349a69c3395f1a57c642dc62Ying Wang
2905436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Like dup, but do not return STDIN_FILENO, STDOUT_FILENO, or
3005436638acc7c010349a69c3395f1a57c642dc62Ying Wang   STDERR_FILENO.  If FLAG contains O_CLOEXEC, behave like
3105436638acc7c010349a69c3395f1a57c642dc62Ying Wang   fcntl(F_DUPFD_CLOEXEC) rather than fcntl(F_DUPFD).  */
3205436638acc7c010349a69c3395f1a57c642dc62Ying Wang
3305436638acc7c010349a69c3395f1a57c642dc62Ying Wangint
3405436638acc7c010349a69c3395f1a57c642dc62Ying Wangdup_safer_flag (int fd, int flag)
3505436638acc7c010349a69c3395f1a57c642dc62Ying Wang{
3605436638acc7c010349a69c3395f1a57c642dc62Ying Wang  return fcntl (fd, (flag & O_CLOEXEC) ? F_DUPFD_CLOEXEC : F_DUPFD,
3705436638acc7c010349a69c3395f1a57c642dc62Ying Wang                STDERR_FILENO + 1);
3805436638acc7c010349a69c3395f1a57c642dc62Ying Wang}
39